Kubernetes + Docker + Spring boot + Fabric8 Maven plugin – in a nutshell

Maxim
Surov

Lead Software Engineer
@EPAM

Kubernetes + Docker + Spring boot + Fabric8 Maven plugin – in a nutshell


Microservices. How could we concisely illustrate architecture of microservices? Let’s refer to Roerich Banner Of Peace picture: wikipedia
bannerofpeace

Here you can see the idea. The awesome tool to implement self-healing circle is Kubernetes. To conduct kubernetes kubectl command line tool is indented to be used. Plus there is fabric8 maven plugin to facilitate Java developers to work with kubernetes.

Kubernetes is awesome tool but there is a gap between skills that should be applied to run kubernetes cluster and general Java developer skills. From this point kubernetes could be treated as a tool to be used by devops experts.

Hopefully, there are tools which greatly simplify deployment of kubernetes cluster under developers machines. One of those tools is minikube. This allows us to quickly run Kubernetes single node cluster. Hopefully, by the end of this article reading you will manage to:

– run kubernetes cluster
– develop simple Spring boot microservices
– deploy microservices into kubernetes by using fabric8 maven plugin ot kubectl tool- scale particular service

Minikube

Prerequisites:
1. Windows machine with Oracle VM installed + Maven
2. Refer to: github.com
3. In my case I downloaded the binaries: kubectl.exe, minikube.exe
4. And added them to PATH:
jd8_1

location

1) Create a single node Kubernetes cluster.
You should execute: minikube start –host-only-cidr=”192.168.99.1/24″
(–host-only-cidr=”192.168.99.1/24″) this is for correct minikubeVM configuration: NAT (to provide IP address to be accessed from window machine) + Host-only adapter.
After this command has been executed, a minikubeVM ready-for-use is produced (running under CoreOS). A virtual machine is created automatically.
minikube

2) Execute the minikube ip – to discover the address of the Kubernetes machine (or you could login to the virtual machine and execute: ifconfig)
ip

3) Kubernetes + Docker machine has been just created.
4) Some local hacks to be performed: Take config file from:
settings
And place it somewhere you feel is convenient (e.g. I placed it under the root of D:/). We should use this file to execute kubectl commands.

5) E.g. refer to the classic Spring boot example: github
Plus, we should add a Fabric8 plugin to pom.xml

6) Now we can build the project and build a Docker image on the host where Kubernetes + Docker is being run.
At first you should set:
SET DOCKER_HOST=tcp://192.168.99.110:2376 SET DOCKER_CERT_PATH=C:\Users\Maxim_Surov.minikube\certs (this variable have to be set up only if kubernetes cluster is being accessed via SSL)
You can discover those params by executing:
environment
Then build:
mvn clean install fabric8:build
It should create an executable Spring boot jar + create a Docker image on the remote host.
If we execute: <> on Kubernetes + Docker host we should see that a springframework/gs-spring-boot-docker image has been created.
Now it’s time to deploy the image into Kubernetes. From the Windows host execute consecutively
kubectl run gs-spring-boot-docker –image=springframework/gs-spring-boot-docker:0.1.0 –port=8080 –replicas=3
--kubeconfig="d:/config"
kubectl expose replicationController gs-spring-boot-docker –type=NodePort –external-ip=192.168.99.110
--kubeconfig="d:/config"
kubectl describe services gs-spring-boot-docker
--kubeconfig="d:/config"
kubectl get pods
--kubeconfig="d:/config"
alternative approach is just to use fabric8 maven plugin commands to accomplish it, e.g.:
mvn clean install fabric8:build fabric8:run
to scale:
mvn fabric8:start -Dfabric8.replicas=3
By default service that has one pod is created
single
if e.g. service has been scaled to 3 replicas the output is:
whole
We have just set up single node 3 replicas (SLA) kubernetes cluster. You can see that all replicas are run.
To execute it externally we should use: http://192.168.99.101:32598/ Or it could have been configured to use also 8080 port.
replica1replica2
As you can see, different replicas play periodically.
Other useful commands:
get pods --kubeconfig="d:/config"
get nodes --kubeconfig="d:/config"
get services --kubeconfig="d:/config"
kubectl cluster-info --kubeconfig="d:/config" You can also leverage different capabilities of fabric8 maven plugin (https://maven.fabric8.io/)

Summary

You have just managed to run single node kubernetes cluster. Plus, you can communicate with it to deploy, scale, etc services by using fabric8 maven plugin. From docker perspectives there is counterpart of fabric8 maven plugin. It is called: com.spotify (docker-maven-plugin) though it is also powerfull it is indented to work only with docker, To work with kubernetes you should rely on fabric8 maven plugin. Thank you for reading. I wanted to share this knowledge and approach. Since I spent a lot of time trying to set up kubernetes (with Ubuntu, etc). It seems it is the most comfortable solution (for general Java developer not for linux guy) just to try locally.