Updated March 30, 2023
Definition of Spring Cloud Kubernetes
Spring Cloud Kubernetes provides Spring Cloud interface implementations which was allowing developers to construct and run Spring Cloud applications on Kubernetes. While this project may be handy when developing a cloud-native application. It is not required to deploy the Spring Boot application on Kubernetes. If we are just getting started with Kubernetes and Spring Boot, we can get a lot done with only a basic Spring Boot app and Kubernetes.
Spring cloud Kubernetes overviews
- Spring Cloud and Kubernetes are two prominent tools that can be used for a variety of purposes.
- Micro services architecture, on the other hand, is sometimes referred to as a competitive solution.
- Both Spring Cloud and Kubernetes are excellent choices for building a micro services solution since they include components that address the most prevalent issues.
- If we chose Kubernetes as our main container manager and deployment platform, we can still take advantage of Spring Cloud’s useful capabilities, mostly through the Spring Cloud Kubernetes project.
- This new project makes it simple to integrate Spring Boot apps with Kubernetes. It’s a good idea to look at how to deploy a Spring Boot application on Minikube, a local Kubernetes environment.
- Kubernetes is a containerized application platform for scaling, executing, and managing them. The setup and orchestration are done on the platform, not in the application.
- Etcd is one of the most crucial Kubernetes components. All cluster data, including the service registry and application configuration, is stored in that highly accessible key-value store.
- We won’t be able to replace it with another tool. Third-party components such as Istio or Linkerd can be used to implement more advanced routing and load balancing schemes.
- We don’t need to add anything to the source code to deploy and execute applications on Kubernetes.
- The project was still in its infancy when we are transferring our microservices to OpenShift. Because we didn’t have any other viable options, we decided to remove the components for discovery and configuration from the Spring Boot application.
- Other Spring Cloud components, such as Ribbon, Sleuth, and OpenFeign were still available.
- One of the most popular Spring Cloud projects now is in Spring Cloud Kubernetes. It may be unsurprising that it isn’t up to speed with the latest Spring Cloud features in this situation.
- It still utilizes Ribbon instead of the new Spring Cloud Load Balancer, for example. It includes some handy tools for deploying Spring Boot applications on Kubernetes.
Spring Cloud Kubernetes create a Secret
The below example shows to create secrets which are as follows.
1) Create project template by using spring initializer
In the below step we have provided project group name as com. example, artifact name as springcloudkubernetes, project name as springcloudkubernetes, and selected java version as 8.
In the below project, we have selected spring web dependency to implement the project.
Group – com. example Artifact name – springcloudkubernetes
Name – springcloudkubernetes Spring boot – 2.6.0
Project – Maven Java – 8
Dependencies – spring web
Package name – com. example.springcloudkubernetes
Project Description – Project for springcloudkubernetes
2) After generating project extract files and open this project by using spring tool suite –
After generating the project by using spring initializer in this step we are extracting the jar file and opening project by using spring tool suite.
3) After opening project using spring tool suite check the project and its files –
In this step, we are checking all the project template files. We also need to check maven dependencies and system libraries.
4) Add dependency packages –
In this step, we are adding the Kubernetes dependency in the project.
Code:
<dependency> -- Start of dependency tag.
<groupId>org.springframework.cloud</groupId> -- Start and end of groupId tag.
<artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
5) Start the single node kubernetes cluster –
In this step, we are staring single-node Kubernetes cluster using minikube.
# minikube start –vm-driver-virtualbox
6) Switch the context –
The default context is minikube in the below step we have switched the context.
# kubectl config use-context minikube
7) Start the dashboard –
- After switching the context in this step we are starting the dashboard of Kubernetes to monitor services, logs, secrets, and pods.
# minikube dashboard
8) Build the project –
- In this step, we are building the spring cloud Kubernetes project using the following command or by using the spring tool suite.
# mvn clean install
9) Check the pods are running on dashboard –
- In this step, we are checking all pods are running without any error.
10) Create a main class of project –
- In this step, we are creating the main class of the application.
Code:
@SpringBootApplication
@EnableDiscoveryClient
public class springcloudkubernetes {
public static void main(String[] args) {
SpringApplication.run (springcloudkubernetes.class, args);
}
}
11) Create rest controller –
Code:
@RestController
public class Controller {
@Autowired
private springClient sClient;
}
12) Create configuration class –
Code:
@Configuration
@ConfigurationProperties (prefix = "bean")
public class config
{
private String string = "spring cloud kubernetes"
}
13) Create client controller –
Code:
@RestController
public class clientcontroller {
@Autowired
private config con;
@GetMapping
public String load() {
return String.format (config.getMessage(), "", "");
}
}
14) Create secret –
Code:
apiVersion: v1
kind: Secret
metadata:
name: database-secret
data:
username: cYVmrk==
password: cCU4BSiolfX=
15) Create mongodb service –
Code:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mongo
spec:
replicas: 1
template:
metadata:
labels:
service: mongo
name: service
spec:
containers:
- args:
- mongod
- --smallfiles
image: mongo:new
name: mongo
env:
- name: USERNAME
valueFrom:
secretKeyRef:
name: database-secret
key: username
16) Setup mongodb service –
Code:
spring.cloud.kubernetes.secrets.name=database-secret
spring.data.mongodb.host=service
spring.data.mongodb.port=27017
spring.data.mongodb.database=admin
spring.data.mongodb.username=${kubernetes}
spring.data.mongodb.password=${kubernetes}
17) Run the application and check dashboard –
Conclusion
Spring cloud Kubernetes are using microservices design patterns including distributed configuration, service discovery, circuit breaking, and load balancing. Spring Cloud Kubernetes provides Spring Cloud interface implementations which was allowing developers to construct and run Spring Cloud applications on Kubernetes.
Recommended Articles
This is a guide to Spring Cloud Kubernetes. Here we discuss the definition, overviews, create a Secret, Examples, and applications. You may also have a look at the following articles to learn more –