Guide to Spring Boot Kubernetes.

1. Overview


When we build a microservices solution, both Spring Cloud and Kubernetes are optimal solutions, as they provide components for resolving the most common challenges. However, if we decide to choose Kubernetes as the main container manager and deployment platform for our solution, we can still use Spring Cloud’s interesting features mainly through the Spring Cloud Kubernetes project.


This relatively new project undoubtedly provides easy integration with Kubernetes for Spring Boot applications. Before starting, it may be helpful to look at how to deploy a Spring Boot application on Minikube, a local Kubernetes environment.


In this tutorial, we’ll:


Install Minikube on our local machine


Develop a microservices architecture example with two independent Spring Boot applications communicating through REST


Set up the application on a one-node cluster using Minikube


Deploy the application using YAML config files


2. Scenario


In our example, we’re using the scenario of travel agents offering various deals to clients who will query the travel agents service from time to time. We’ll use it to demonstrate:


service discovery through Spring Cloud Kubernetes


configuration management and injecting Kubernetes ConfigMaps and secrets to application pods using Spring Cloud Kubernetes Config


load balancing using Spring Cloud Kubernetes Ribbon


3. Environment Setup


First and foremost, we need to install Minikube on our local machine and preferably a VM driver such as VirtualBox. It’s also recommended to look at Kubernetes and its main features before following this environment setup.


Let’s start the local single-node Kubernetes cluster:


1


minikube start --vm-driver=virtualbox

This command creates a Virtual Machine that runs a Minikube cluster using the VirtualBox driver. The default context in kubectl will now be minikube. However, to be able to switch between contexts, we use:


1


kubectl config use-context minikube

After starting Minikube, we can connect to the Kubernetes dashboard to access the logs and monitor our services, pods, ConfigMaps, and Secrets easily:


1


minikube dashboard

3.1. Deployment


Firstly, let’s get our example from GitHub.


At this point, we can either run the “deployment-travel-client.sh” script from the parent folder, or else execute each instruction one by one to get a good grasp of the procedure:

### build the repository

mvn clean install

 

### set docker env

eval $(minikube docker-env)

 

### build the docker images on minikube

cd travel-agency-service

docker build -t travel-agency-service .

cd ../client-service

docker build -t client-service .

cd ..

 

### secret and mongodb

kubectl delete -f travel-agency-service/secret.yaml

kubectl delete -f travel-agency-service/mongo-deployment.yaml

 

kubectl create -f travel-agency-service/secret.yaml

kubectl create -f travel-agency-service/mongo-deployment.yaml

 

### travel-agency-service

kubectl delete -f travel-agency-service/travel-agency-deployment.yaml

kubectl create -f travel-agency-service/travel-agency-deployment.yaml

 

### client-service

kubectl delete configmap client-service

kubectl delete -f client-service/client-service-deployment.yaml

 

kubectl create -f client-service/client-config.yaml

kubectl create -f client-service/client-service-deployment.yaml

 

# Check that the pods are running

kubectl get pods

4. Service Discovery


This project provides us with an implementation for the ServiceDiscovery interface in Kubernetes. In a microservices environment, there are usually multiple pods running the same service. Kubernetes exposes the service as a collection of endpoints that can be fetched and reached from within a Spring Boot Application running in a pod in the same Kubernetes cluster.


For instance, in our example, we have multiple replicas of the travel agent service, which is accessed from our client service as http://travel-agency-service:8080. However, this internally would translate into accessing different pods such as travel-agency-service-7c9cfff655-4hxnp.


Spring Cloud Kubernetes Ribbon uses this feature to load balance between the different endpoints of a service.


We can easily use Service Discovery by adding the spring-cloud-starter-kubernetesdependency on our client application:

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-kubernetes</artifactId>

</dependency>

Also, we should add @EnableDiscoveryClient and inject the DiscoveryClient into the ClientController by using @Autowired in our class:


@SpringBootApplication

@EnableDiscoveryClient

public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}


@RestController

public class ClientController {

    @Autowired

    private DiscoveryClient discoveryClient;

}


Reference

Source-> https://www.baeldung.com/spring-cloud-kubernetes&ved=2ahUKEwi_qriao-bjAhWr-ioKHafLAiQQFjABegQIBBAB&usg=AOvVaw1SB6meu2TLr4tMYiDbwwR9

Comments

Popular Posts