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-slim
VOLUME /tmp
COPY target/my-spring-boot-app.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]


2. Containerize the Application

1. Build the JAR file:

 mvn clean package


2. Build the Docker image:

docker build -t my-spring-boot-app:latest .


3. Test the Docker image locally:

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:latest
docker 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/v1
kind: Deployment
metadata:
  name: spring-boot-app
spec:
  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: v1
kind: Service
metadata:
  name: spring-boot-service
spec:
  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 pods
kubectl get svc

The EXTERNAL-IP of the LoadBalancer service will provide access to your application.


6. Optional Enhancements

1. Monitor and Scale:

  • 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

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example