[Kubernetes series] How to elegantly configure Java microservices

Hits: 0


Configure Java Microservices


  • Create Kubernetes ConfigMap and Secret
  • Inject microservice configuration using MicroProfile Config

1. Realize external application configuration

In [Kubernetes] , there are several different ways to set environment variables for docker containers, such as: Dockerfile, kubernetes.yml, Kubernetes ConfigMaps, and Kubernetes Secrets . This course mainly explains how to use the latter two methods to set your [environment variables] , and the values ​​of the environment variables will be injected into your microservices. One advantage of using ConfigMaps and Secrets is that they can be reused across multiple containers, such as assigning to different environment variables in different containers.

1.1. Create Kubernetes ConfigMaps and Secrets

ConfigMaps are [API] objects that store non-confidential key-value pairs . The ConfigMap API will not be explained too much in this section. Now you only need to know it and know what it can do. We will give a detailed explanation in the next article. In this section, we will learn how to use ConfigMap to Save the application name.

Although Secrets is also used to store key-value pairs, the difference from ConfigMaps is that it is for confidential/sensitive data, and the storage format is Base64 encoding. This property of secrets makes it suitable for storing certificates, keys, and tokens.

1.2, externalized placement

CDI provides a standard set of dependency injection capabilities so that applications can be assembled from cooperating, loosely coupled beans. MicroProfile Config provides a standard way for apps and microservices to obtain configuration parameters from various sources, such as applications, runtimes, and environments. Based on the priority defined by the source, attributes can be automatically merged into a single set of attributes that applications can access through the API. Both CDI & MicroProfile are used in the interactive tutorial to get externally provided properties from Kubernetes ConfigMaps and Secrets and inject them into the application code.

2. Practical deployment of Java microservices

In this chapter we will deploy two Java microservices to Kubernetes and change their configuration using MicroProfile Config, Kubernetes ConfigMaps and Secrets.

First, make sure our Kubernetes environment is set up. Once the terminal has finished outputting messages and is ready for input, the setup should begin.

2.1. Verify kubectl

Execute the kubectl versioncommand to confirm that it is ready:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.4", GitCommit:"e87da0bd6e03ec3fea7933c4b5263d151aafd07c", GitTreeState:"clean", BuildDate:"2021-02-18T16:12:00Z", GoVersion:"go1.15.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

You should now see the version of the kubectl virtual side and server. If so, the environment is already set up. If you don’t see the version of the Kubernetes server, wait a moment and repeat the previous command until it is displayed.

2.2, enter the project directory

Execute the cd sample-kubernetes-config/start/command to enter the project directory. It contains the implementation of the MicroProfile microservice, the configuration of the MicroProfile runtime, and the Kubernetes configuration.

$ cd sample-kubernetes-config/start/

You’ll see a finishdirectory where the two microservices we’ll deploy are called” system “and” inventory “. The system microservice returns the JVM properties of the container running it. The catalog microservice adds properties from the system microservice to the catalog. Here’s how to implement communication between two separate microservices in a Kubernetes cluster.

2.3, Maven build application

To build the application with Maven, run the following commands in sequence:

$ mvn package -pl system

A lot of information will be output here, I will only intercept some parts for demonstration:

$ mvn package -pl inventory

As shown in the figure, now we have built the system separately, inventory two microservices.

2.4. Deploy to Kubernetes

Now, we need to deploy them to Kubernetes. Execute the kubectl apply -f kubernetes.yamlcommand to complete the deployment:

$ kubectl apply -f kubernetes.yaml


You may also like...

Leave a Reply

Your email address will not be published.