WE ARE TECHNOLOGY, WE ARE INNOVATION, WE ARE GLOBAL, WE ARE GROWTH

Spring Boot Micro Services in Kubernetes

Quick Inquiry

captcha

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.

img

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.

Project Setup:

Apart from Spring boot web starter dependency, we need to add the below dependencies.

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-kubernetes-core</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-kubernetes-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-commons</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

Here, we included Spring Boot Actuator in our project dependency, the Actuator endpoint are used to provide in Kubernetes configuration in livenessProbe, and readnessProbe.

Example:

"livenessProbe": { "httpGet": { "path": "/actuator/health", "port": 8080, "scheme": "HTTP" } }, "readinessProbe": { "httpGet": { "path": "/actuator/health", "port": 8080, "scheme": "HTTP" } }

Pre-Requirements:

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.

img

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:

<plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <version>${fabric8.maven.plugin.version}</version> <executions> <execution> <id>fmp</id> <goals> <goal>resource</goal> <goal>build</goal> <!-- goal>push</goal --> </goals> </execution> </executions> <configuration> <authConfig> <username>sravan4rmhyd</username> <password>*******</password> </authConfig> <machine> <autoCreate>true</autoCreate> <name>HelloWorld</name> </machine> </configuration> </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.

img

And we can see the below line in console.

img

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.

img

Note: while doing the push, make sure that groupId has the docker hub username.

Application setup:

@EnableDiscoveryClientis one of the annotations to enable the discovery of the services.

DiscoveryClient is the class which hold the information about services.

Conclusion: 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

For More information about Java Development Services, visit our official Website.

Awesome clients we worked for

Client Testimonials

  • Fabio Durso

    We found a reliable and efficient partner in Aegis Infoways, ready to support our strategy of offshore software development. Aegis Infoways has particularly impressed by the speed of response to any request for the development of software applications and for their maintenance.

  • Filipe

    We did hire full time Java developers from Aegis Infoways, to help us to improve a time to market of a product. The software platform is based on Java & Extjs, and they are delivering the software on time and with success. We strongly recommend Aegis Infoways as Reliable Development partner.

  • Steve

    Powerful solutions are given by Aegis Infoways dedicated developers for my projects. They suggest solutions as per current market trend. Other than this, the team is always ready for any type of changes or update. That is the main reason that I would like to give my next project to them.

Copyright © 2016 - Aegis Infoways All rights reserved