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 in Kubernetes 1.2

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces how to use ConfigMap in Kubernetes 1.2. it is very detailed and has a certain reference value. Friends who are interested must read it!

Use ConfigMap

The configuration of many applications needs to be done through a combination of configuration files, command line parameters, and environment variables. These configurations should be decoupled from the image content to keep containerized applications portable. The ConfigMap API resource provides a way to inject configuration data into the container while keeping the container unaware of Kubernetes. ConfigMap can be used to save a single property, an entire configuration file, or a JSON binary large object.

Overview of ConfigMap

ConfigMap API resources hold key-value pairs of configuration data, which can be used in pods or used to store configuration data for system components like controller. Although ConfigMap is similar to Secrets, configmap can more easily handle strings that do not contain sensitive information

Let's take a look at an example of completion:

The data column contains configuration data. As you can see, ConfigMap can be used to save individual properties or to save a configuration file.

Configuration data can be used in Pods in many ways. ConfigMaps can be used to:

1. Set the value of the environment variable

2. Set command line parameters in the container

3. Create a config file in the data volume

Both users and system components can store configuration data in ConfigMap.

Create ConfigMaps

You can easily create a ConfigMaps from a text value, file, or directory using the kubectl create configmap command.

Let's take a look at many different ways to create a ConfigMap:

1. Create from a file

Now we have a directory that contains some files, which already contains the data we want to set up ConfigMaps:

The kubectl create configmap command can be used to create a ConfigMap to store the contents of each file in this directory:

When-- from-file points to the directory, all files under the directory will be used in ConfigMap to create a key-value pair, the name of the key is the file name, and the value is the content of the file.

Let's take a look at the ConfigMap created by this command:

You can see that the two file names in the mapping are created from the directory specified by kubectl. The contents of these keys can be very large, so in the kubectl describe output, you can only see the name of the key and their size.

If we want to see the value of the key, we can just kubectl get the resource:

2. Create from a file

We can also pass-- from-file a specific file and pass it to kubectl multiple times. The following command has the same result as in the previous example:

We can also use-- from-file to set keys for individual files by passing key=value:

-- from-file=game-special-key=docs/user-guide/configmap/kubectl/game.properties

3. Create from literal value

Provides a literal value for ConfigMap to use kubectl create configmap. The from-literal option uses syntax, which allows literals to be applied directly to the command line:

Using ConfigMap in pods

Use case: using ConfigMap in environment variables

ConfigMap can be used to fill in environment variables. For example, consider the following ConfigMap:

We can use this ConfigMap key in a pod like this:

When this pod runs, its output will include the following lines:

Use case: set command line parameters with ConfigMap

ConfigMap can also be used to set commands or parameter values in the container. This is a completed syntax that is replaced with Kubernetes$ (VAR_NAME). Consider ConfigMap:

In order to enter values into the command line, we must use the keys we want to use as environment variables, as in the previous example. Then we can refer to them and use the $(VAR_NAME) syntax in the container's command.

When the pod is running, the output from the test-container container will be:

Use case: using ConfigMap through the data volume plug-in

ConfigMap can also be used in data volumes. Let's go back to our ConfigMap example:

We have different options for using this ConfigMap in the data volume. The most basic thing is to fill the file into the data volume, in this file, the key is the file name, and the key value is the file content:

When the pod runs, the output looks like this:

We can also control the path in the data volume where the ConfigMap value is mapped.

When pod runs, the output looks like this:

Real case: configuring Redis

Let's take a look at the real case: configuring redis with Configmap. We want to use redis as a cache to enter redis with the recommended configuration. The Redis configuration should include:

There is such a file in docs/user-guide/configmap/redis; with it, we can create an instance of ConfigMap using the following command:

Now, let's create a pod with this configuration:

Notice that this pod has an ConfigMap data volume that replaces example-redis-config ConfigMap's redis-config key in a file called redis.conf. This volume grows to the / redis-master directory of the redis container, replacing our configuration file, / redis-master/redis.conf, in / redis-master/redis.conf

This is the redis configuration file that image looks up for master.

If we kubectl exec to this pod and run the redis-cli tool, we can check that our configuration is being used correctly:

Restriction condition

ConfigMaps must be created before it can be used in pod. Controllers can be written to tolerate the absence of some profile data. Configmaps belongs to namespace. They are only referenced through pods in the same namespaces.

The ConfigMap size quota is already a set feature.

Kubelet only supports using ConfigMap from the pods of the API server. This includes any pods created with Kubectl, or pods created indirectly through replication controller. It does not include pods created with the-manifest-url flag of Kubelet, nor does it include pods created from its REST API (generally not using these methods to create pods).

These are all the contents of the article "how to use ConfigMap in Kubernetes 1.2". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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