Introduction
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?
- Scalability: Automatically scale your Spring Boot app up or down based on traffic.
- Resilience: Kubernetes handles restarts, failover, and self-healing.
- Cloud-Native: Run anywhere — cloud, hybrid, or on-premises.
- Rolling Updates: Update services with zero downtime.
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
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
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
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
- Use readiness and liveness probes
- Set resource limits (CPU, memory)
- Use centralized configuration with Spring Cloud Config Server
- Avoid hardcoded values; use ConfigMaps and Secrets
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.