This section provides the steps needed to run the Spring Boot micro service as a Docker container inside Kubernetes.
When using a single VM of Kubernetes, it’s really handy to reuse the minikube’s built-in Docker daemon; as this means you don’t have to build a docker registry on your host machine and push the image into it - you can just build inside the same docker daemon as minikube which speeds up local experiments. Just make sure you tag your Docker image with something other than ‘latest’ and use that tag while you pull the image. Otherwise, if you do not specify version of your image, it will be assumed as :latest, with pull image policy of Always correspondingly, which may eventually result in ErrImagePull as you may not have any versions of your Docker image out there in the default docker registry (usually DockerHub) yet.
- Copy the MyFirstApp-0.0.1-SNAPSHOT.jar built in the previous section to another folder say C:\MyFirstApp.
- Let us now create a Docker container using this standalone jar. We need to introduce the Docker file. Create a file with name Dockerfile in C:\MyFirstApp and add the following lines.
FROM java:8
ADD MyFirstApp-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
- Dockerfile and the MyFirstApp-0.0.1-SNAPSHOT.jar should be under the same folder.
Create Dockerfile |
- Understanding the elements of a basic Dockerfile.
- FROM specifies the base image on which container is built. In this case we use java:8 which uses Java container as both the build and runtime environment.
- ADD element copies the standalone Jar to app.jar.
- ENTRYPOINT specifies a command that will be executed when the container starts. In this case “java –jar app.jar”.
When using a single VM of Kubernetes, it’s really handy to reuse the minikube’s built-in Docker daemon; as this means you don’t have to build a docker registry on your host machine and push the image into it - you can just build inside the same docker daemon as minikube which speeds up local experiments. Just make sure you tag your Docker image with something other than ‘latest’ and use that tag while you pull the image. Otherwise, if you do not specify version of your image, it will be assumed as :latest, with pull image policy of Always correspondingly, which may eventually result in ErrImagePull as you may not have any versions of your Docker image out there in the default docker registry (usually DockerHub) yet.
- To start Kubernetes cluster use “minikube start”. This should start a local Kubernetes cluster on your laptop. You can see a minikube VM entry added in Oracle VM Virtual Box Manager.
Oracle VM Virtual Box - minikube instance |
- Once you start minikube the console would look like this. Now we are ready to deploy applications on kubernetes.
minikube start |
- Get the minikube IP using the command "minikube ip". We will run the containerized app on this IP.
minikube IP |
- Run “minikube docker-env” to get the environment of built-in docker daemon.
minikube docker-env |
- Run the last command displayed above to configure the shell for the docker environment.
Set the docker environment |
- Verify by running “docker ps” command. You should see several kubernetes related containers already running.
docker ps listing |
- Go to the folder where we copied the Spring Boot standalone Jar. We already have the Dockerfile here. Run the command "docker build –t myfirstapp:v1 ." to build the docker image. It is important to specify the version tag when working with Kubernetes environment. This command should pull the java libraries, add the Spring Boot application, set the entry point and build the container for us.
Build docker image |
- Verify if the container is listed by running the "docker images" command. We will see an entry for the myfirstapp container.
docker images listing |
- Now it is time to start our container in kubernetes. We will leverage kubectl to run this container.
kubectl run myfirstapp –image=myfirstapp:v1 –port=8080
kubectl run |
- Launch the minikube dashboard and verify if the application is created. Run the command “minikube dashboard” this should launch a browser and display the dashboard. You can see the app running in dashboard.
minikube dashboard launch |
Kubernetes dashboard |
- Some useful commands. Use “kubectl get pods” to get the list of pods in the cluster. Use “kubectl get services” to get list of services running in the cluster.
kubectl get pods |
kubectl get services |
- Next step is to expose this application for external access as a service. Use the command “kubectl expose deployment myfirstapp –type=NodePort”.
kubectl expose service |
- Run the “kubectl get services” command again to see if myfirstapp is listed.
kuebctl get services listing exposed service |
- Verify if the application works. Using the browser. Type the URL http://192.168.99.103:31608/sayhello to invoke the API. (or) Run the command “minkube service myfirstapp”. This should automatically launch the browser. Append the URL sayhello to get the message.
minikube service myfirstapp |
Launch myfirstapp API from kubernetes IP |
Now we have successfully deployed and run a Spring Boot application as a container on Kubernetes.
0 comments:
Post a Comment