Deploy a Spring Boot Application on Kubernetes with PostgreSQL

Connecting a Spring Boot application to a PostgreSQL database on Kubernetes requires more than running two pods side by side. The database needs a stable hostname your application can rely on across pod restarts, credentials need to stay out of your container image, and external traffic needs a consistent entry point. This template wires all of that together using a StatefulSet-backed PostgreSQL database, headless DNS for stable database addressing, Kubernetes Secrets for credential injection, and NGINX Ingress for external routing.
| Component | Type | Port | Role |
|---|---|---|---|
| Spring Boot App | Deployment + ClusterIP Service | 8080 | Stateless application tier connecting to PostgreSQL via headless DNS |
| PostgreSQL | StatefulSet | 5432 | Database backend deployed as a StatefulSet for stable pod identity (postgres-0) and guaranteed PVC reattachment |
| Headless Service | Service | - | Gives PostgreSQL a predictable DNS address (postgres-0.postgres-service) that does not change when the pod restarts |
| Secret | Secret | - | Database credentials stored outside the image and injected as environment variables at pod startup |
| NGINX Ingress | Ingress | 80 | Routes external requests to the Spring Boot service at /frontend-service |
The Spring Boot Deployment resolves the database host using the stable DNS name postgres-0.postgres-service, provided by the headless Service pointing directly at the StatefulSet pod. Unlike a standard ClusterIP Service that load-balances across multiple endpoints, the headless Service maps to the specific pod, giving the application a reliable connection target that survives pod restarts. Database credentials are injected from a Kubernetes Secret as environment variables at startup. NGINX Ingress routes external HTTP traffic to the Spring Boot ClusterIP Service at /frontend-service.
kubectl get pods -n <namespace>.curl http://<ingress-ip>/frontend-service and confirm a response is returned.kubectl logs <springboot-pod> -n <namespace>.kubectl describe ingress -n <namespace>.This template configures a Spring Boot Deployment on Kubernetes connected to a PostgreSQL StatefulSet via headless DNS, with externalized credentials and NGINX Ingress routing for external access. The source code for the Spring Boot starter application is available at github.com/kubekanvas/deploy-springboot-kubernetes.