This demo deploys a simple Spring Boot web application that connects to Postgres onto a Kubernetes cluster.
You can watch this demo along with an introduction to Kubernetes concepts here.
- Kubernetes cluster with kubectl installed and configured to use your cluster
- docker cli installed, you must be signed into your Docker Hub account
-
Deploy postgres with a persistent volume claim
kubectl create -f specs/postgres.yml
-
Create a config map with the hostname of Postgres
kubectl create configmap hostname-config --from-literal=postgres_host=$(kubectl get svc postgres -o jsonpath="{.spec.clusterIP}")
-
Build the Spring Boot app
./mvnw -DskipTests package
-
Build a Docker image and push the image to Docker Hub
docker build -t <your Docker Hub account>/spring-boot-postgres-on-k8s:v1 . docker push <your Docker Hub account>/spring-boot-postgres-on-k8s:v1
-
Replace
<your Docker Hub account>
with your account name inspecs/spring-boot-app.yml
, then deploy the appkubectl create -f specs/spring-boot-app.yml
-
Create an external load balancer for your app
kubectl expose deployment spring-boot-postgres-sample --type=LoadBalancer --port=8080
-
Get the External IP address of Service, then the app will be accessible at
http://<External IP Address>:8080
kubectl get svc spring-boot-postgres-sample
Note: It may take a few minutes for the load balancer to be created
-
Scale your application
kubectl scale deployment spring-boot-postgres-sample --replicas=3
- Update the image that the containers in your deployment are using
kubectl set image deployment/spring-boot-postgres-sample spring-boot-postgres-sample=<your Docker Hub account>/spring-boot-postgres-on-k8s:v2
-
Delete the Spring Boot app deployment
kubectl delete -f specs/spring-boot-app.yml
-
Delete the service for the app
kubectl delete svc spring-boot-postgres-sample
-
Delete the hostname config map
kubectl delete cm hostname-config
-
Delete Postgres
kubectl delete -f specs/postgres.yml