Deploying a Spring Boot application on AWS Elastic Kubernetes Service (EKS)
Deploying a Spring Boot application on AWS Elastic Kubernetes Service (EKS) involves several steps, including preparing the application, containerizing it, and configuring Kubernetes resources. Below is a step-by-step guide:
1. Prepare Your Spring Boot Application
Ensure your Spring Boot application is production-ready:
- Externalize configurations using application.properties or application.yml.
- Use a Dockerfile to containerize your application.
Sample Dockerfile:
FROM openjdk:17-jdk-slimVOLUME /tmpCOPY target/my-spring-boot-app.jar app.jarENTRYPOINT ["java","-jar","/app.jar"]
2. Containerize the Application
mvn clean package
docker build -t my-spring-boot-app:latest .
docker run -p 8080:8080 my-spring-boot-app:latest
3. Push the Docker Image to ECR
1. Create an AWS Elastic Container Registry (ECR) repository:
aws ecr create-repository --repository-name my-spring-boot-app
2. Authenticate Docker with ECR:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com
3. Tag the image and push it to ECR:
docker tag my-spring-boot-app:latest <account_id>.dkr.ecr.<region>.amazonaws.com/my-spring-boot-app:latestdocker push <account_id>.dkr.ecr.<region>.amazonaws.com/my-spring-boot-app:latest
4. Set Up AWS EKS
1. Create an EKS Cluster:
eksctl create cluster --name my-eks-cluster --region <region> --nodes 2
- Install kubectl and eksctl if not already installed.
2. Configure kubectl for EKS:
aws eks update-kubeconfig --region <region> --name my-eks-cluster
3. Verify the connection:
kubectl get nodes
5. Deploy Spring Boot on EKS
1. Create Kubernetes Deployment YAML: Save this file as deployment.yaml:
apiVersion: apps/v1kind: Deploymentmetadata: name: spring-boot-appspec: replicas: 2 selector: matchLabels: app: spring-boot-app template: metadata: labels: app: spring-boot-app spec: containers: - name: spring-boot-app image: <account_id>.dkr.ecr.<region>.amazonaws.com/my-spring-boot-app:latest ports: - containerPort: 8080---apiVersion: v1kind: Servicemetadata: name: spring-boot-servicespec: type: LoadBalancer ports: - port: 80 targetPort: 8080 selector: app: spring-boot-app
2. Apply the Deployment and Service:
kubectl apply -f deployment.yaml
3. Verify Deployment:
kubectl get podskubectl get svc
The EXTERNAL-IP of the LoadBalancer service will provide access to your application.
6. Optional Enhancements
- Use HorizontalPodAutoscaler:
kubectl autoscale deployment spring-boot-app --cpu-percent=50 --min=2 --max=5
2. Secure with IAM Roles: Attach IAM roles to your EKS nodes for secure access to AWS resources.
3. Configure Secrets: Use Secrets for sensitive configurations (e.g., database credentials).
Troubleshooting Tips
1. Logs:
kubectl logs <pod-name>
2. Debugging Failed Pods:
kubectl describe pod <pod-name>
3. Cluster Insights: Enable Amazon CloudWatch Container Insights for better monitoring.
By following these steps, your Spring Boot application should be successfully deployed on AWS EKS and accessible via the LoadBalancer service.
Comments
Post a Comment