Spring Boot + Kubernetes (K8s): Full Guide for Microservices Deployment

🔥 Read with Full Features on Our Website

Learn how to deploy Spring Boot applications on Kubernetes (K8s) with complete setup, YAML files, Docker integration, and real-world architecture.

Published on 18 Jun 2025
By ttimesnow

Introduction

🔥 Read with Full Features on Our Website

Kubernetes, also known as K8s, is the industry-standard container orchestration platform that enables automated deployment, scaling, and management of containerized applications. When combined with Spring Boot, a powerful Java framework for building microservices, Kubernetes unlocks the ability to deploy resilient and scalable Java apps in cloud-native environments.

This guide covers everything from containerizing a Spring Boot app using Docker, writing Kubernetes YAML deployment files, to exposing services and auto-scaling them efficiently.

Why Use Kubernetes for Spring Boot?

Step 1: Create a Simple Spring Boot App

@SpringBootApplication
@RestController
public class HelloApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello() {
        return "Hello from Spring Boot in Kubernetes!";
    }
}

Step 2: Create Dockerfile

Google Advertisement

This file tells Docker how to package your Spring Boot application.

FROM openjdk:17-alpine
VOLUME /tmp
COPY target/demo-app.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

Build Docker Image

docker build -t springboot-k8s-demo .

Push to Docker Hub (or other registry)

docker tag springboot-k8s-demo your-dockerhub-username/springboot-k8s-demo
docker push your-dockerhub-username/springboot-k8s-demo

Step 3: Kubernetes YAML Files

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: springboot
  template:
    metadata:
      labels:
        app: springboot
    spec:
      containers:
      - name: springboot-container
        image: your-dockerhub-username/springboot-k8s-demo
        ports:
        - containerPort: 8080

Service.yaml

apiVersion: v1
kind: Service
metadata:
  name: springboot-service
spec:
  selector:
    app: springboot
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Step 4: Apply YAML to Kubernetes

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Google Advertisement

Verify the Application

Once deployed, use the following command to get the service external IP:

kubectl get services

Access your app by visiting: http://<EXTERNAL-IP>/hello

Auto-scaling the Application

kubectl autoscale deployment springboot-app --cpu-percent=50 --min=2 --max=10

Using ConfigMaps and Secrets

You can externalize configuration using ConfigMaps and Secrets.

ConfigMap Example

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  app.message: "Hello from ConfigMap!"

Access in Application

Google Advertisement

Use Spring Boot’s application.properties:

app.message=${APP_MESSAGE}

Logging and Monitoring

Use tools like Prometheus, Grafana, and ELK Stack to monitor your Spring Boot microservices inside Kubernetes.

CI/CD Integration

Popular tools like Jenkins, GitHub Actions, and GitLab CI can be used to automate Docker build and Kubernetes deploy steps.

Spring Boot Kubernetes Best Practices

Conclusion

Combining Spring Boot with Kubernetes allows developers to build and deploy modern, cloud-native applications with high scalability and resilience. With Docker containers, Kubernetes orchestration, and Spring Boot’s power, teams can deliver software faster, with less downtime and easier management. Whether you're deploying on Minikube, EKS, AKS, or GKE — the process remains largely the same.

Start small by deploying a basic app as shown above, and scale up to enterprise-level microservices with auto-scaling, service mesh, and secure configurations. Kubernetes is no longer optional in the DevOps world — and mastering it with Spring Boot gives you an edge in the cloud-native landscape.

👉 View Full Version on Main Website ↗
👉 Read Full Article on Website