Kubernetes

Spring Microservices on K8s

Two Spring Boot services with Deployments, ClusterIP Services, and resource limits

📋Configuration Files

service-a-deployment.yml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: service-a
5spec:
6 replicas: 1
7 selector:
8 matchLabels:
9 app: service-a
10 template:
11 metadata:
12 labels:
13 app: service-a
14 spec:
15 containers:
16 - name: service-a
17 image: spring-api-a:local
18 imagePullPolicy: IfNotPresent
19 ports:
20 - containerPort: 8080
21 resources:
22 requests:
23 memory: "256Mi"
24 cpu: "250m"
25 limits:
26 memory: "512Mi"
27 cpu: "500m"

service-a-svc.yml

yaml
1apiVersion: v1
2kind: Service
3metadata:
4 name: service-a
5spec:
6 type: ClusterIP
7 selector:
8 app: service-a
9 ports:
10 - port: 8080
11 targetPort: 8080

service-b-deployment.yml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: service-b
5spec:
6 replicas: 1
7 selector:
8 matchLabels:
9 app: service-b
10 template:
11 metadata:
12 labels:
13 app: service-b
14 spec:
15 containers:
16 - name: service-b
17 image: spring-api-b:local
18 imagePullPolicy: IfNotPresent
19 ports:
20 - containerPort: 8080
21 env:
22 - name: SERVICE_A_URL
23 value: "http://service-a:8080"
24 resources:
25 requests:
26 memory: "256Mi"
27 cpu: "250m"
28 limits:
29 memory: "512Mi"
30 cpu: "500m"

service-b-svc.yml

yaml
1apiVersion: v1
2kind: Service
3metadata:
4 name: service-b
5spec:
6 type: NodePort
7 selector:
8 app: service-b
9 ports:
10 - port: 8080
11 targetPort: 8080
12 nodePort: 32080
📄

Step 01

Deploy Spring Microservices

(01)Build and load images

Linux
1# Build Spring JARs, then Docker images
2docker build -t spring-api-a:local ./service-a
3docker build -t spring-api-b:local ./service-b
4minikube image load spring-api-a:local
5minikube image load spring-api-b:local

(02)Apply all manifests

Linux
1kubectl apply -f service-a-deployment.yml
2kubectl apply -f service-a-svc.yml
3kubectl apply -f service-b-deployment.yml
4kubectl apply -f service-b-svc.yml
5kubectl get pods,svc