Elastic Kubernetes Service - amresh087/newronaRepos GitHub Wiki

Elastic Kubernetes Service Answer

Spring Boot Docker Application Deployment on AWS EKS

This guide contains all important Kubernetes YAML files required to deploy a Spring Boot Docker application into Amazon EKS.

Assumptions:

  • Docker image already created
  • Docker image pushed to ECR
  • EKS cluster already created

Example Docker Image:

 123456789.dkr.ecr.ap-south-1.amazonaws.com/payment-service:latest

1. Namespace YAML

File: namespace.yaml

 apiVersion: v1
 kind: Namespace
 metadata:
   name: banking-app

2. Deployment YAML

File: deployment.yaml

 apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: payment-service
   namespace: banking-app
   labels:
     app: payment-service

 spec:
   replicas: 3

   selector:
     matchLabels:
       app: payment-service

   template:
     metadata:
       labels:
         app: payment-service

     spec:
       containers:
         - name: payment-service
           image: 123456789.dkr.ecr.ap-south-1.amazonaws.com/payment-service:latest

           ports:
             - containerPort: 8080

           resources:
             requests:
               memory: "512Mi"
               cpu: "250m"

             limits:
               memory: "1Gi"
               cpu: "500m"

           env:
             - name: SPRING_PROFILES_ACTIVE
               value: prod

             - name: DB_HOST
               value: postgres-service

             - name: DB_PORT
               value: "5432"

           livenessProbe:
             httpGet:
               path: /actuator/health
               port: 8080
             initialDelaySeconds: 30
             periodSeconds: 10

           readinessProbe:
             httpGet:
               path: /actuator/health
               port: 8080
             initialDelaySeconds: 15
             periodSeconds: 5

3. Service YAML

File: service.yaml

 apiVersion: v1
 kind: Service
 metadata:
   name: payment-service
   namespace: banking-app

 spec:
   type: ClusterIP

   selector:
     app: payment-service

   ports:
     - protocol: TCP
       port: 80
       targetPort: 8080

4. Ingress YAML

File: ingress.yaml

 apiVersion: networking.k8s.io/v1
 kind: Ingress
 metadata:
   name: payment-ingress
   namespace: banking-app

   annotations:
     kubernetes.io/ingress.class: alb
     alb.ingress.kubernetes.io/scheme: internet-facing
     alb.ingress.kubernetes.io/target-type: ip

 spec:
   rules:
     - http:
         paths:
           - path: /payment
             pathType: Prefix
             backend:
               service:
                 name: payment-service
                 port:
                   number: 80

5. ConfigMap YAML

File: configmap.yaml

 apiVersion: v1
 kind: ConfigMap
 metadata:
   name: payment-config
   namespace: banking-app

 data:
   application.properties: |
     server.port=8080
     spring.datasource.url=jdbc:postgresql://postgres-service:5432/paymentdb
     spring.jpa.hibernate.ddl-auto=update

6. Secret YAML

File: secret.yaml

 apiVersion: v1
 kind: Secret
 metadata:
   name: payment-secret
   namespace: banking-app

 type: Opaque

 data:
   DB_USERNAME: cG9zdGdyZXM=
   DB_PASSWORD: cGFzc3dvcmQ=

7. HPA (Horizontal Pod Autoscaler)

File: hpa.yaml

 apiVersion: autoscaling/v2
 kind: HorizontalPodAutoscaler
 metadata:
   name: payment-hpa
   namespace: banking-app

 spec:
   scaleTargetRef:
     apiVersion: apps/v1
     kind: Deployment
     name: payment-service

   minReplicas: 2
   maxReplicas: 10

   metrics:
     - type: Resource
       resource:
         name: cpu
         target:
           type: Utilization
           averageUtilization: 70

8. Persistent Volume Claim (Optional)

File: pvc.yaml

 apiVersion: v1
 kind: PersistentVolumeClaim
 metadata:
   name: payment-pvc
   namespace: banking-app

 spec:
   accessModes:
     - ReadWriteOnce

   resources:
     requests:
       storage: 5Gi

9. Deployment Commands

Create Namespace

 kubectl apply -f namespace.yaml

Apply Configurations

 kubectl apply -f configmap.yaml
 kubectl apply -f secret.yaml
 kubectl apply -f deployment.yaml
 kubectl apply -f service.yaml
 kubectl apply -f ingress.yaml
 kubectl apply -f hpa.yaml

10. Verification Commands

Check Pods

  kubectl get pods -n banking-app

Check Services

 kubectl get svc -n banking-app

Check Ingress

  kubectl get ingress -n banking-app

Describe Pod

  kubectl describe pod <pod-name> -n banking-app

Check Logs

  kubectl logs <pod-name> -n banking-app

11. Internal Working Flow

      Developer Push Docker Image
                ↓
      Amazon ECR
                ↓
      Kubernetes Deployment YAML
                ↓
      EKS Scheduler
                ↓
      Worker Node Selected
                ↓
      Pod Created
                ↓
      Service Exposes Application
                ↓
      Ingress/ALB Exposes Public URL

12. Real Enterprise Architecture

      Client
         ↓
      ALB Ingress
         ↓
      Kubernetes Service
         ↓
      Spring Boot Pods
         ↓
      PostgreSQL / Redis / Kafka

13. Important Interview Points

Why Deployment?

Deployment maintains desired pod replicas and supports rolling updates.

Why Service?

Provides stable networking for pods.

Why Ingress?

Exposes APIs externally and routes traffic.

Why HPA?

Automatically scales pods based on CPU/memory.

Why ConfigMap?

Stores external configuration.

Why Secret?

Stores sensitive data securely.

⚠️ **GitHub.com Fallback** ⚠️