Spring Boot Micro Services in Kubernetes
Technology: Spring cloud Kuberntes is one of the child projects of Spring Cloud. It mainly focuses on deploying the Spring boot micro services in Kubernetes Environment, and how to access other Kubernetes Services in micro services.
For those who don’t know about Kubernetes: Kubernetes is one of the tools for Container Orchestration, which used for automating deployment, scaling and management of the containerized applications.
Kubernetes is not specific not any containerized tools, it supportsall majorly used tools like Docker, rkt (Rocket Container), based on configuration settings.
Spring Cloud Kubernetes will be replacement for many of the Spring cloud child projects, like Spring Cloud Config, Spring Cloud Eureka, Spring Cloud Zuul, instead of these frameworks we can use Service Registry, configMaps, Secret, ingress provided by Kubernetes.
Ingress is one of the components in Kubernetes is route the traffic based on path to specified microservices in configuration.
Although Spring Cloud Kubernetes is sub-project of Spring cloud, but it is not released under Spring Cloud Releases, we need to add the Kubernetes dependency similar to other dependencies not like Spring Cloud dependency.
Apart from Spring boot web starter dependency, we need to add the below dependencies.
Here, we included Spring Boot Actuator in our project dependency, the Actuator endpoint are used to provide in Kubernetes configuration in livenessProbe, and readnessProbe.
We need at least single node cluster of running Kubernetes instance on machine.
Spring Cloud Kubernetes will access Kubernetes Rest API to get the information about cluster, services, deployments etc. Spring Cloud Kubernetes will try to get the Kubernetes components using defaultserviceaccount. As Kubernetes provides default role, we need to bind this role to cluser admin role.
For binding either we can use attached json file, and execute the kubectl apply -f
Or we can execute the below command
kubectl create clusterrolebinding admin --clusterrole=cluster-admin --serviceaccount=default:default
Maven Plugin Configuration:
Fabric8 is one of most used maven plugin for building Kubernetes resource json files, and creating docker images, pushing the docker images to Docker registry, and also deploying/undeploying the application docker images to Kubernetes cluster.
And provide the watch goal is to monitor for changes during the development and auto update the docker image in cluster.
log is the to display the logs of the application in cluster.
And it provides the helm(Packaging tool for Kubernetes) integration to create helm package.
It also provide the way to debug the application which is running in Kubernetes cluster using debuggoal.
mvn fabric8:debug -Dfabric8.debug.port=8000
Sample Maven Configuration for Fabric8 Plugin:
authConfig will contains the authentication information for contacting the public docker repository, for pulling and pushing the images into it.
Machine is the will contains the information about docker machine, used to build the docker images.
If the docker machine is not present, we can use autoCreate tag to create new one, using docker-machine.exe, which is provided by docker, we can download the same from docker website and add to PATH variable.
Note: for this work, we need to install VirtualBox, to create docker virtual machine.
fabric8:deploy will generate the Kubernetes component JSON files in targt/classes/META-INF/fabric8 folder.
And we can see the below line in console.
Note: Unfortuntaly, when you deploy using the fabric8 plugin, the readyness and liveness probes fail to point to the right actuator URL due a lack of support for spring boot. This push you to edit the generated deployment inside kubernetes and change these probes which points to "path": "/health" to "path": "/actuator/health". This will make your deployment go green. This issue is already reported into the fabric8 community: https://github.com/fabric8io/fabric8-maven-plugin/issues/1178
fabric8:push will push the docker image to docker hub.
Note: while doing the push, make sure that groupId has the docker hub username.
@EnableDiscoveryClientis one of the annotations to enable the discovery of the services.
DiscoveryClient is the class which hold the information about services.
Spring Cloud Kubernetes is the library to deploy, and consume the resources on Kubernetes environment, Fabric is the maven plugin for all related tasks, like building the image, push into docker hub, and deploying into Kubernetes environment.
Source code can be download at https://github.com/sravan4rmhyd/Hello-world-Kubernetes.git