Demo project to Dockerize a Java(Spring Boot) application & run it on a Kubernetes(K8) cluster
Prerequisites:
This article assumes that you have:
- Docker installed in your local system.Setup Docker
- A DockerHub account. Signup for DockerHub
- A running Kubernetes cluster. Setup a cluster in GKE
Steps:
1. Write a Java(Spring Boot) application and create its artifact(*.jar)
2. Write a Dockerfile
3. Build the Docker image locally with some tag.
docker build -t rounakraj8/demo:latest .
4. Push the Docker image to any container registry(eg. DockerHub, AWS ECR, GCR).
docker push rounakraj8/demo:latest
5. Create the deployment
kubectl create deployment demo --image=rounakraj8/demo:latest
Alternatively, you can create deployment.yaml file and achieve the same results using kubectl apply -f deployment.yaml
command.
6. Create the service
kubectl expose deployment demo --name=demo --port=80 --target-port=8080 --type=LoadBalancer
Alternatively, you can create service.yaml file and achieve the same results
using kubectl apply -f service.yaml
command.
7. Scale the deployment
kubectl scale deployment demo --replicas=2
8. Test the service
To test this setup/demo, you will have to hit the hello API.
After creating the service, this service will get an external/public API(This may take couple of minutes).
Once you get the IP, you can hit `http://xxx.xxx.xxx.xxx/hello/someName`.
If you get `Hello! SomeName` as response, then you have successfully completed the demo.
9. Delete the created service
kubectl delete svc demo
10. Delete the created deployment
kubectl delete deployment demo