DEV Community

DevCorner
DevCorner

Posted on

Deploying a Spring Boot Microservice on Kubernetes: Step-by-Step Guide

In this blog post, we'll walk through deploying a Spring Boot microservice on Kubernetes (K8s) using the provided Kubernetes manifest files. These files define a Deployment and a Service, ensuring that our microservice runs smoothly within the cluster.


1. Understanding the Manifest Files

The provided YAML files define:

  1. A Deployment that manages the lifecycle of the microservice.
  2. A Service that exposes the microservice to the network.

Let's go through the deployment process step by step.


2. Setting Up the Kubernetes Cluster

Before deploying, ensure that you have a Kubernetes cluster running. You can use:

  • Minikube (for local development)
  • Docker Desktop Kubernetes
  • A managed Kubernetes service (like AWS EKS, Azure AKS, or Google GKE)

Check your Kubernetes setup with:

kubectl cluster-info
Enter fullscreen mode Exit fullscreen mode

If you don't have Kubernetes installed, follow this guide to set it up.


3. Creating the Deployment

Deployment YAML Explanation

1️⃣ Metadata Section

apiVersion: apps/v1
kind: Deployment
metadata:
  name: accounts-deployment
  labels:
    app: accounts
Enter fullscreen mode Exit fullscreen mode
  • Defines a Deployment named accounts-deployment.
  • Uses labels (app: accounts) to identify the microservice.

2️⃣ Spec Section

spec:
  replicas: 1
  selector:
    matchLabels:
      app: accounts
Enter fullscreen mode Exit fullscreen mode
  • Creates one replica (you can increase this for scalability).
  • Selects Pods using the matchLabels condition.

3️⃣ Pod Template (Under template: Section)

    template:
      metadata:
        labels:
          app: accounts
Enter fullscreen mode Exit fullscreen mode
  • Specifies the labels inside the Pod template.

4️⃣ Container Definition

    spec:
      containers:
      - name: accounts
        image: eazybytes/accounts:s12
        ports:
        - containerPort: 8080
Enter fullscreen mode Exit fullscreen mode
  • Defines a container named accounts.
  • Uses the Docker image eazybytes/accounts:s12.
  • Exposes port 8080 for incoming traffic.

5️⃣ Environment Variables (From ConfigMap)

        env:
        - name: SPRING_APPLICATION_NAME
          valueFrom:
            configMapKeyRef:
              name: eazybank-configmap
              key: ACCOUNTS_APPLICATION_NAME
        - name: SPRING_PROFILES_ACTIVE
          valueFrom: 
            configMapKeyRef:
              name: eazybank-configmap
              key: SPRING_PROFILES_ACTIVE
        - name: SPRING_CONFIG_IMPORT
          valueFrom: 
            configMapKeyRef:
              name: eazybank-configmap
              key: SPRING_CONFIG_IMPORT
        - name: EUREKA_CLIENT_SERVICEURL_DEFAULTZONE
          valueFrom: 
            configMapKeyRef:
              name: eazybank-configmap
              key: EUREKA_CLIENT_SERVICEURL_DEFAULTZONE
Enter fullscreen mode Exit fullscreen mode
  • Pulls environment variables from a ConfigMap (eazybank-configmap).
  • These variables configure Spring Boot profiles, Eureka client URL, and application name dynamically.

Deploying the Application

Run the following command to create the Deployment:

kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Verify the deployment:

kubectl get deployments
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

If the Pod is running, you should see an output similar to:

NAME                   READY   STATUS    RESTARTS   AGE
accounts-deployment    1/1     Running   0          10s
Enter fullscreen mode Exit fullscreen mode

4. Exposing the Service

Service YAML Explanation

apiVersion: v1
kind: Service
metadata:
  name: accounts
spec:
  selector:
    app: accounts
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
Enter fullscreen mode Exit fullscreen mode
  • Creates a Service named accounts.
  • Selects Pods with the label app: accounts.
  • Uses LoadBalancer to expose the service externally.
  • Maps port 8080 on the Service to port 8080 in the container.

Deploying the Service

Run the following command:

kubectl apply -f service.yaml
Enter fullscreen mode Exit fullscreen mode

Check the service:

kubectl get services
Enter fullscreen mode Exit fullscreen mode

If successful, you'll see an output like:

NAME       TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
accounts   LoadBalancer   10.100.200.100   <pending>     8080:30000/TCP   10s
Enter fullscreen mode Exit fullscreen mode
  • If you're running Minikube, use minikube service accounts --url to get the external URL.

5. Accessing the Application

Once the LoadBalancer is ready, you can access the service using:

http://<EXTERNAL-IP>:8080
Enter fullscreen mode Exit fullscreen mode

For Minikube users, use:

minikube service accounts --url
Enter fullscreen mode Exit fullscreen mode

To check logs from the running Pod:

kubectl logs -f <POD_NAME>
Enter fullscreen mode Exit fullscreen mode

6. Scaling the Application

You can scale the Deployment to handle more traffic:

kubectl scale deployment accounts-deployment --replicas=3
Enter fullscreen mode Exit fullscreen mode

Verify the new replica count:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

7. Cleaning Up

To delete the deployment and service:

kubectl delete -f deployment.yaml
kubectl delete -f service.yaml
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this guide, we:

✅ Created a Deployment for a Spring Boot microservice.

✅ Defined a Service to expose the microservice.

✅ Used a ConfigMap to manage environment variables.

✅ Scaled the application dynamically.

Now your microservice is running on Kubernetes! 🚀

Top comments (0)