Configuring microservices running in Kubernetes

duration 15 minutes

Explore how to externalize configuration using MicroProfile Config and configure your microservices using Kubernetes ConfigMaps and Secrets.

Prerequisites:

What you’ll learn

You will learn how and why to externalize your microservice’s configuration. Externalized configuration is useful because configuration usually changes depending on your environment. You will also learn how to configure the environment by providing required values to your application using Kubernetes.

MicroProfile Config provides useful annotations that you can use to inject configured values into your code. These values can come from any configuration source, such as environment variables. Using environment variables allows for easier deployment to different environments. To learn more about MicroProfile Config, read the Configuring microservices guide.

Furthermore, you’ll learn how to set these environment variables with ConfigMaps and Secrets. These resources are provided by Kubernetes and act as a data source for your environment variables. You can use a ConfigMap or Secret to set environment variables for any number of containers.

Additional prerequisites

Before you begin, you need a containerization software for building containers. Kubernetes supports various container runtimes. You will use Docker in this guide. For Docker installation instructions, refer to the official Docker documentation.

Use Docker Desktop, where a local Kubernetes environment is pre-installed and enabled. If you do not see the Kubernetes tab, then upgrade to the latest version of Docker Desktop.

Complete the setup for your operating system:

After you complete the Docker setup instructions for your operating system, ensure that Kubernetes (not Swarm) is selected as the orchestrator in Docker Preferences.

Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

Copied to clipboard
git clone https://github.com/openliberty/guide-kubernetes-microprofile-config.git
cd guide-kubernetes-microprofile-config

The start directory contains the starting project that you will build upon.

The finish directory contains the finished project that you will build.

Before you begin, make sure you have all the necessary prerequisites.

Starting and preparing your cluster for deployment

Start your Kubernetes cluster.

Start your Docker Desktop environment.

Ensure that Kubernetes is running on Docker Desktop and that the context is set to docker-desktop.

Next, validate that you have a healthy Kubernetes environment by running the following command from the active command-line session.

Copied to clipboard
kubectl get nodes

This command should return a Ready status for the master node.

You do not need to do any other step.

Deploying the microservices

The two microservices you will deploy are called system and inventory. The system microservice returns the JVM system properties of the running container. The inventory microservice adds the properties from the system microservice to the inventory. This demonstrates how communication can be established between pods inside a cluster. To build these applications, navigate to the start directory and run the following command.

Copied to clipboard
cd start
mvn clean package

Next, run the docker build commands to build container images for your application:

Copied to clipboard
docker build -t system:1.0-SNAPSHOT system/.
docker build -t inventory:1.0-SNAPSHOT inventory/.

The -t flag in the docker build command allows the Docker image to be labeled (tagged) in the name[:tag] format. The tag for an image describes the specific image version. If the optional [:tag] tag is not specified, the latest tag is created by default.

Run the following command to deploy the necessary Kubernetes resources to serve the applications.

Copied to clipboard
kubectl apply -f kubernetes.yaml

When this command finishes, wait for the pods to be in the Ready state. Run the following command to view the status of the pods.

Copied to clipboard
kubectl get pods

When the pods are ready, the output shows 1/1 for READY and Running for STATUS.

NAME                                   READY     STATUS    RESTARTS   AGE
system-deployment-6bd97d9bf6-6d2cj     1/1       Running   0          34s
inventory-deployment-645767664f-7gnxf  1/1       Running   0          34s

After the pods are ready, you will make requests to your services.

The default host name for Docker Desktop is localhost.

Navigate to http://[hostname]:31000/system/properties and use the username bob and the password bobpwd to authenticate. Replace [hostname] with the IP address or host name of your Kubernetes cluster. Open your browser’s developer console and examine the response headers.

You can also run the curl command to make requests to your microservices. Use the -u option to pass in the username bob and the password bobpwd.

Copied to clipboard
curl http://localhost:31000/system/properties -u bob:bobpwd

If the curl command is unavailable, then use Postman. Postman enables you to make requests using a graphical interface. To make a request with Postman, enter http://localhost:31000/system/properties into the URL bar. Next, set the Authorization with Basic Auth type. Set the Username field to bob and the Password field to bobpwd. Read the Authorizing requests document for more details. Finally, click the blue Send button to make the request.

Similarly, navigate to http://[hostname]:32000/inventory/systems/system-service, or use the following curl command to add the system to your inventory.

Copied to clipboard
curl http://localhost:32000/inventory/systems/system-service

Modifying system microservice

The system service is hardcoded to use a single forward slash as the context root. The context root is set in the webApplication element, where the contextRoot attribute is specified as "/". You’ll make the value of the contextRoot attribute configurable by implementing it as a variable.

Replace the server.xml file.
View Code
Copied to clipboard
system/src/main/liberty/config/server.xml

The contextRoot attribute in the webApplication element now gets its value from the context.root variable. To find a value for the context.root variable, Open Liberty looks for the following environment variables, in order:

  • context.root

  • context_root

  • CONTEXT_ROOT

Modifying inventory microservice

The inventory service is hardcoded to use bob and bobpwd as the credentials to authenticate against the system service. You’ll make these credentials configurable.

Replace the SystemClient class.
View Code
Copied to clipboard
inventory/src/main/java/io/openliberty/guides/inventory/client/SystemClient.java

The changes introduced here use MicroProfile Config and CDI to inject the value of the environment variables CONTEXT_ROOT, SYSTEM_APP_USERNAME and SYSTEM_APP_PASSWORD into the SystemClient class.

Creating a ConfigMap and Secret

Several options exist to configure an environment variable in a Docker container. You can set it directly in the Dockerfile with the ENV command. You can also set it in your kubernetes.yaml file by specifying a name and a value for the environment variable that you want to set for a specific container. With these options in mind, you’re going to use a ConfigMap and Secret to set these values. These are resources provided by Kubernetes as a way to provide configuration values to your containers. A benefit is that they can be reused across many different containers, even if they all require different environment variables to be set with the same value.

Create a ConfigMap to configure the app name with the following kubectl command.

Copied to clipboard
kubectl create configmap sys-app-root --from-literal contextRoot=/dev

This command deploys a ConfigMap named sys-app-root to your cluster. It has a key called contextRoot with a value of /dev. The --from-literal flag allows you to specify individual key-value pairs to store in this ConfigMap. Other available options, such as --from-file and --from-env-file, provide more versatility as to what you want to configure. Details about these options can be found in the Kubernetes CLI documentation.

Run the following command to display details of the ConfigMap.

Copied to clipboard
kubectl describe configmaps sys-app-root

Create a Secret to configure the new credentials that inventory uses to authenticate against system with the following kubectl command.

Copied to clipboard
kubectl create secret generic sys-app-credentials --from-literal username=alice --from-literal password=wonderland

This command looks similar to the command to create a ConfigMap, but one difference is the word generic. This word creates a Secret that doesn’t store information in any specialized way. Different types of secrets are available, such as secrets to store Docker credentials and secrets to store public and private key pairs.

Run the following command to display details of the Secret.

Copied to clipboard
kubectl describe secrets/sys-app-credentials

A Secret is similar to a ConfigMap. A key difference is that a Secret is used for confidential information such as credentials. One of the main differences is that you must explicitly tell kubectl to show you the contents of a Secret. Additionally, when it does show you the information, it only shows you a Base64 encoded version so that a casual onlooker doesn’t accidentally see any sensitive data. Secrets don’t provide any encryption by default, that is something you’ll either need to do yourself or find an alternate option to configure. Encryption is not required for the application to run.

Updating Kubernetes resources

Next, you will update your Kubernetes deployments to set the environment variables in your containers based on the values that are configured in the ConfigMap and Secret that you created previously.

Replace the kubernetes file.
View Code
Copied to clipboard
kubernetes.yaml

The CONTEXT_ROOT, SYSTEM_APP_USERNAME, and SYSTEM_APP_PASSWORD environment variables are set in the env sections of system-container and inventory-container.

Using the valueFrom field, you can specify the value of an environment variable from various sources. These sources include a ConfigMap, a Secret, and information about the cluster. In this example configMapKeyRef gets the value contextRoot from the sys-app-root ConfigMap. Similarly, secretKeyRef gets the values username and password from the sys-app-credentials Secret.

Deploying your changes

Rebuild the application using mvn clean package.

Copied to clipboard
mvn clean package

Run the docker build commands to rebuild container images for your application:

Copied to clipboard
docker build -t system:1.0-SNAPSHOT system/.
docker build -t inventory:1.0-SNAPSHOT inventory/.

Run the following command to deploy your changes to the Kubernetes cluster.

Copied to clipboard
kubectl replace --force -f kubernetes.yaml

When this command finishes, wait for the pods to be in the Ready state. Run the following command to view the status of the pods.

Copied to clipboard
kubectl get pods

When the pods are ready, the output shows 1/1 for READY and Running for STATUS.

Your application will now be available at the http://[hostname]:31000/dev/system/properties URL. You now need to use the new username, alice, and the new password, wonderland, to log in. Alternatively, you can run the following command:

Copied to clipboard
curl http://localhost:31000/dev/system/properties -u alice:wonderland

If the curl command is unavailable, then use Postman. To make a request with Postman, enter http://localhost:31000/dev/system/properties into the URL bar. Next, set the Authorization with Basic Auth type, and set the Username field to alice and Password field to wonderland. Finally, click the blue Send button to make the request.

Notice that the URL you are using to reach the application now has /dev as the context root.

Verify that http://[hostname]:32000/inventory/systems/system-service is working as intended. If it is not working, then check the configuration of the credentials.

Testing the microservices

Run the integration tests:

Copied to clipboard
mvn failsafe:integration-test -Dsystem.context.root=/dev

The tests for inventory verify that the service can communicate with system using the configured credentials. If the credentials are misconfigured, then the inventory test fails, so the inventory test indirectly verifies that the credentials are correctly configured.

After the tests succeed, you should see output similar to the following in your console.

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.system.SystemEndpointIT
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.706 s - in it.io.openliberty.guides.system.SystemEndpointIT

Results:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.inventory.InventoryEndpointIT
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.696 s - in it.io.openliberty.guides.inventory.InventoryEndpointIT

Results:

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

Tearing down the environment

Run the following commands to delete all the resources that you created.

Copied to clipboard
kubectl delete -f kubernetes.yaml
kubectl delete configmap sys-app-root
kubectl delete secret sys-app-credentials

Nothing more needs to be done for Docker Desktop.

 1<server description="Sample Liberty server">
 2
 3  <featureManager>
 4    <platform>jakartaee-10.0</platform>
 5    <platform>microprofile-7.0</platform>
 6    <feature>restfulWS</feature>
 7    <feature>jsonb</feature>
 8    <feature>cdi</feature>
 9    <feature>jsonp</feature>
10    <feature>mpConfig</feature>
11    <feature>mpHealth</feature>
12    <feature>appSecurity</feature>
13  </featureManager>
14
15  <variable name="http.port" defaultValue="9090"/>
16  <variable name="https.port" defaultValue="9453"/>
17  <variable name="system.app.username" defaultValue="bob"/>
18  <variable name="system.app.password" defaultValue="bobpwd"/>
20  <variable name="context.root" defaultValue="/"/>
22
23  <httpEndpoint host="*" httpPort="${http.port}" 
24    httpsPort="${https.port}" id="defaultHttpEndpoint" />
25
27  <webApplication location="guide-kubernetes-microprofile-config-system.war" contextRoot="${context.root}"/>
29
30  <basicRegistry id="basic" realm="BasicRegistry">
31    <user name="${system.app.username}" password="${system.app.password}" />
32  </basicRegistry>
33
34</server>35

Prerequisites:

Nice work! Where to next?

Nice work! You have used MicroProfile Config to externalize the configuration of two microservices, and then you configured them by creating a ConfigMap and Secret in your Kubernetes cluster.

Configuring microservices running in Kubernetes by Open Liberty is licensed under CC BY-ND 4.0

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

What could make this guide better?

Raise an issue to share feedback

Create a pull request to contribute to this guide

Need help?

Ask a question on Stack Overflow

Like Open Liberty? Star our repo on GitHub.

Star