In this tutorial, we would learn how to deploy a Spring Boot application to a local Kubernetes cluster. This would be the first of series of tutorials on DevOps with Docker and Kubernetes for beginners. At the end of the series, you should be able to deploy web applications along with their databases to Docker and Kubernetes cluster.
In this very part, we would setup Kubernetes cluster locally (Minikube), create a simple SpringBoot application and deploy to the Kubernetes cluster
We cover the following
- Have Docker Installed and Tested
- Setup Minikube
- Create and Build a Spring Boot Application
- Create a Docker Image
- Optional – Push the Image to Docker Hub
- Create Deployment and Deploy to Kubernetes Cluster
- Test the Deployment
Follow the steps below:
Step 1 – Have Docker installed
You need to ensure that Docker is installed and running. Use this command:
docker version
You may want to get used to docker and Spring Boot using this short practical tutorial:
Introduction to Dockerfile with Spring Boot – How to Dockerize SpringBoot App
SpringBoot Docker – Video Tutorial
Step 2 – Setup Minikube
Minikube is a way to run Kubernetes cluster locally on your machine. Also check that you have Minikube.
Use the command
minikube version
Once you are sure Minkube is installed, go ahead to start it using the command:
minikube start
Just to let you know, kubectl is a command line tool for managing Minikube.
Step 3 – Create and build a SpringBoot Application
In this demo, we just create a simple SpringBoot REST API that return a string. But this works for bigger applications as well (without database because we would be talking about database deployment later). I call this application kubedemo
Use the command below to create your jar file
mvn clean install -DskipTests
You can now check that the target folder of your application contains your jar file.
Step 4 – Create a Docker image of your application
To do this you should have a docker file in the root directory of your application. The complete procedure to dockerize your Spring Boot Application is given here. The easiest way to create a docker image of your application is to use Buildpacks. The command is simply:
mvn spring-boot:build-image
This command would create a local image of your application using the jar file you created in step 3.
You can verify that the image is created using the command below:
docker images
This command would list all the docker images available in your local repository. You should see the new image you just built!
Step 5 – Push to Docker Hub (Optional)
You can actually leave your image locally. But it’s fine you know how to push your image to Docker hub online. In this way, you can get them from anywhere or even share with friends.
You need two commands:
First, tag the image:
docker tag kubedemo:0.0.1-SNAPSHOT kindsonthegenius/kubedemo
Second, push to Docker hub:
docker push kindsonthegenius/kubedemo
Step 6 – Deploy to Kubernetes Cluster
As you know, a cluster is a collection of two or more machines called nodes. Same is true of Kubernetes. A Kubernetes cluster is made up of a number of nodes. However, the nodes in a Kubernetes cluster consists of ‘sub-nodes‘ called pods. So when we setup Minikube, we did create a cluster with a single node but could have several pods.
To be able to deploy our image to Kubernetes, we need to do three things:
1. Create a deployment
A deployment is a yml file that specifies how your application would run in the Kubernetes cluster. You can have this yaml file created automatically for you using the command:
kubectl create deployment kubedemo --image=kindsonthegenius/kubedemo --dry-run -o=yaml
2. Create a Service
A service defines how connections can be make from outside the cluster into your application running inside the Kubernetes cluster. To have a service created for you, use the command:
kubectl create service clusterip kubedemo --tcp=8080:8080 --dry-run -o=yaml
In this case, we specify that our application would be running on port 8080 both inside and outside the container. ClusterIp indicates that we can connect to the application using the cluster’s ip address. The command below gives you the details of the cluster including the IPs.
kubectl get all
3. Apply the deployment
This means you actually deploy the application to the cluster and the pods(containers are started). Use the command:
kubectl apply -f deployment.yaml
Step 7 – Test Your Deployment
Now we need to check that everything is working! First you may want to check that the pods are up. This can be done using the command
kubectl get pods
Then you can use the command below to get the list of services
kubectl get services
Finally, use the command below to fire up your application:
minikube service <service-name>
This command opens up your browser and you can then go ahead to visit the endpoint /home