In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
ConfigMap concept
In the production environment, we often encounter the need to modify the configuration file, the traditional modification will not only affect the normal operation of the service, but also the operation steps are very tedious. To solve this problem, the kubernetes project introduced the ConfigMap function from version 1.2 to separate the configuration information of the application from the program. In this way, not only can the application be reused, but also more flexible functions can be achieved through different configurations. When creating a container, you can package the application as a container image and inject it through environment variables or externally mounted files.
ConfigMap holds configuration items in the form of key:value, which can be used to represent either the value of a variable (such as config=info) or the contents of a complete configuration file (such as server.xml=... ).
If you are familiar with the ecology of Spring Cloud, then you will find that ConfigMap is very similar to Spring Cloud server. There are two ways to use ConfigMap:
Use ConfigMap as an environment variable
Mount ConfigMap as a file
Let's start to practice! We will use a simple application to demonstrate based on Spring Boot, Docker and Kubernetes
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 properties 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 refer to 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.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.