Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ConfigMap

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use ConfigMap". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Use ConfigMap as an environment variable

In this example, we will create a new environment variable in Kubernetes and use it in the code. In Java, you can use environment variables in your code through System.getenv (String) API. In regular Java applications, you can set environment variables in a J2EE application container, such as Oracle WLS or IBM WAS, or in OS. However, the situation is different in Kubernetes. To use environment variables, we must create a configuration map based on literal.

With the kubectl create configmap command, we created two environment variables: app.name and app.desc.

Let's find out what's going on behind this.

Now pay attention to the data section, under the data section, you will find the key-value pair. Technically, ConfigMap is just a store of key values. The name of the property is the key, and the value of the property is the value. The code of the application will require you to find these key-value pairs.

To use this environment variable in Java code, we need to write the following code:

The following code snippet defines two K8s environment variables, "SPRING_BOOT_HELLO_WORLD_APP_NAME" and "SPRING_BOOT_HELLO_WORLD_DESC". These variables will get values from ConfigMap app-env-config. The key that needs to be focused is the key.

Property profiles can save many properties in a single file to run applications in different environments. In Spring Boot applications, properties are saved in the application.properties file under classpath. Let's take a look at the application.properties file packaged in the application's jar package.

We are using the command kubectl create configmap to create a ConfigMap from a single file or from multiple files.

Now let's look at the complete code.

Mount ConfigMap as a file

In this section, I'll show you how to use ConfigMap to mount files to externalize the configuration. In this example, I will use ConfigMap to externalize the application.properties file. Even if the default file is packaged in jar, it is located under src / main / resources. To put it simply, we will overwrite the default file with the file provided by ConfigMap.

The first step is to create a ConfigMap from application.properties. Let's learn how to store this ConfigMap in K8s.

With ConfigMap, we will mount the application.properties file to the K8s cluster and can use it in the application. Notice that the data section contains the contents of the application.properties, and the key is the file name.

Now, in order to override the default configuration file, we need to mount the application.properties into the application's classpath (via ConfigMap). Spring Boot provides this functionality by providing different options. SpringApplication loads attributes from the application.properties file and adds them to Spring Environment at the following location:

The / config subdirectory of the current directory

Current directory

Classpath / config package

The classpath root

If you want more information, you can check the official documentation:

Https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

The easiest and best way is to mount application.properties in the "/ config" directory.

Check the mount path carefully and notice that the name of the ConfigMap should be exactly the same as the app-file-configmap we created above, and the key is the file name. Also, be sure to change the name of the volume mount configuration to the name of the volume configuration.

This code shows how to define properties in an application.properties file. This is simple if you use the standard method recommended by Spring. Specifically, you use the @ Value annotation to inject attribute values into variables.

Now we can move on to the ConfigMap sample application. Let's take a look at the complete code snippet.

Let's create a Docker image and upload it to Dockerhub. In this case, the mirror name is k8s-springboot-helloworld-configmap-app.

The following is the K8S pod configuration file:

Now let's create the service using the NodePort service type so that the Welcome service can be used from outside the K8S cluster.

Now, let's apply these changes to K8S.

Navigate to the browser and access http://:/welcome. In this case, it should be http:// 192.168.99.100:30880/welcome.

Looking carefully at the output, the returned string is:

At the same time, check the default values of the hard-coded environment variables in the code, as well as the property default values of the application.properties packaged in jar. You find that you get the values of environment variables and application.properties from ConfigMap.

That's all for the content of "how to use ConfigMap". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report