create
ConfigMap
Resource object using key-value
the form of key-value pairs to the configuration data that can Pod
use it, ConfigMap
and we want to address the back of Secrets
relatively similar, is a relatively large difference ConfigMap
can be more convenient to deal with some non-sensitive data, such as passwords still need Secrets
to be managed. Let's take the next example illustrates the ConfigMap
use of:
kind: ConfigMap apiVersion: v1 metadata: name: cm-demo namespace: default data: data.1: hello data.2: world config: | property.1=value-1 property.2=value-2 property.3=value-3
Wherein the configuration data data
for the following configuration attributes, the first two are used to hold a single attribute, a back is used to hold a profile.
Of course, the same we can use kubectl create -f xx.yaml
to create the above ConfigMap
objects, but if we do not know how to create ConfigMap
it, do not forget kubectl
is our best teacher, can be used kubectl create configmap -h
to see about creating ConfigMap
help information,
Examples:
# Create a new configmap named my-config based on folder bar
kubectl create configmap my-config --from-file=path/to/bar
# Create a new configmap named my-config with specified keys instead of file basenames on disk
kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
# Create a new configmap named my-config with key1=config1 and key2=config2
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
We can see that can create a catalog from a given ConfigMap
subject, such as we have a testcm
directory, the directory contains configuration files, redis
and mysql
connection information, as follows:
$ ls testcm
redis.conf
mysql.conf
$ cat testcm/redis.conf
host=127.0.0.1
port=6379
$ cat testcm/mysql.conf host=127.0.0.1 port=3306
Then we can use the from-file
keyword to create a directory that contains the configuration file so ConfigMap
:
$ kubectl create configmap cm-demo1 --from-file=testcm
configmap "cm-demo1" created
Where from-file
all the files in the following parameter specifies the directory will be used on ConfigMap
the inside to create a key-value pair, the key is the name of the file name, value is the contents of the file.
Once created, the same we can use the following command to view ConfigMap
a list:
$ kubectl get configmap
NAME DATA AGE
cm-demo1 2 17s
You can see already created a cm-demo1
of the ConfigMap
object, and then you can use the describe
command to check detailed information:
kubectl describe configmap cm-demo1
Name: cm-demo1
Namespace: default
Labels: <none>
Annotations: <none>
Data
==== mysql.conf: ---- host=127.0.0.1 port=3306 redis.conf: ---- host=127.0.0.1 port=6379 Events: <none>
We can see the two key
is testcm
the following list of file names, the corresponding value
value is the contents of the file, then, here is worth noting that if a large file inside the configuration information, then describe
the time may not display the corresponding value, to view the key you can use the following command:
$ kubectl get configmap cm-demo1 -o yaml
apiVersion: v1
data:
mysql.conf: |
host=127.0.0.1
port=3306
redis.conf: |
host=127.0.0.1 port=6379 kind: ConfigMap metadata: creationTimestamp: 2018-06-14T16:24:36Z name: cm-demo1 namespace: default resourceVersion: "3109975" selfLink: /api/v1/namespaces/default/configmaps/cm-demo1 uid: 6e0f4d82-6fef-11e8-a101-525400db4df7
In addition to the file directory created by, we can also use the specified file created ConfigMap
, similar to the above configuration file, for example, we create a redis
a separate configuration of ConfigMap
objects:
$ kubectl create configmap cm-demo2 --from-file=testcm/redis.conf
configmap "cm-demo2" created
$ kubectl get configmap cm-demo2 -o yaml
apiVersion: v1
data:
redis.conf: |
host=127.0.0.1
port=6379 kind: ConfigMap metadata: creationTimestamp: 2018-06-14T16:34:29Z name: cm-demo2 namespace: default resourceVersion: "3110758" selfLink: /api/v1/namespaces/default/configmaps/cm-demo2 uid: cf59675d-6ff0-11e8-a101-525400db4df7
We can see an associated redis.conf
file configuration information of ConfigMap
the object is successfully created, the other is worth noting that --from-file
this parameter can be used multiple times, such as we use here two were designated redis.conf
and mysql.conf
file, you specify an entire directory and direct the same effects.
In addition, through the help documentation we can see that we can also use direct string created by --from-literal
passing configuration parameters, the same, this parameter can be used multiple times in the following format:
$ kubectl create configmap cm-demo3 --from-literal=db.host=localhost --from-literal=db.port=3306
configmap "cm-demo3" created
$ kubectl get configmap cm-demo3 -o yaml
apiVersion: v1
data:
db.host: localhost
db.port: "3306"
kind: ConfigMap
metadata:
creationTimestamp: 2018-06-14T16:43:12Z
name: cm-demo3
namespace: default
resourceVersion: "3111447"
selfLink: /api/v1/namespaces/default/configmaps/cm-demo3
uid: 06eeec7e-6ff2-11e8-a101-525400db4df7
use
ConfigMap
Creating successful, then we should be how in Pod
the use of it? We say that ConfigMap
these data can be configured in a variety of ways by Pod
using the years, mainly in the following ways:
- The value of the environment variable settings
- Set command-line arguments in containers
- Create a config file in the data inside the volume
First, we use ConfigMap
to fill our environment variables:
apiVersion: v1 kind: Pod metadata: name: testcm1-pod spec: containers: - name: testcm1 image: busybox command: [ "/bin/sh", "-c", "env" ] env: - name: DB_HOST valueFrom: configMapKeyRef: name: cm-demo3 key: db.host - name: DB_PORT valueFrom: configMapKeyRef: name: cm-demo3 key: db.port envFrom: - configMapRef: name: cm-demo1
After running the Pod will output the following lines:
$ kubectl logs testcm1-pod
......
DB_HOST=localhost
DB_PORT=3306 mysql.conf=host=127.0.0.1 port=3306 redis.conf=host=127.0.0.1 port=6379 ......
We can see DB_HOST
and DB_PORT
have normal output, the other environment variables directly because we are here cm-demo1
to inject it came in, so they put out the whole key to the output, and this is in line with expectations.
In addition, we can use ConfigMap
to set the command line parameters ConfigMap
may be used to set a parameter value or a command vessel, the following Pod
:
apiVersion: v1 kind: Pod metadata: name: testcm2-pod spec: containers: - name: testcm2 image: busybox command: [ "/bin/sh", "-c", "echo $(DB_HOST) $(DB_PORT)" ] env: - name: DB_HOST valueFrom: configMapKeyRef: name: cm-demo3 key: db.host - name: DB_PORT valueFrom: configMapKeyRef: name: cm-demo3 key: db.port
Run Pod
after outputs the following information:
$ kubectl logs testcm2-pod
localhost 3306
Another use is very common ConfigMap
ways: by using the data volume, in which the use of data volume ConfigMap
, the file is filled in data volumes, in this file, the file name is the key, the key is the contents of the file:
apiVersion: v1 kind: Pod metadata: name: testcm3-pod spec: containers: - name: testcm3 image: busybox command: [ "/bin/sh", "-c", "cat /etc/config/redis.conf" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: cm-demo2
Run Pod
, and view the log:
$ kubectl logs testcm3-pod
host=127.0.0.1
port=6379
Of course, we may also be in ConfigMap
the control path of the data volume values are mapped to go, following Pod
definition:
apiVersion: v1 kind: Pod metadata: name: testcm4-pod spec: containers: - name: testcm4 image: busybox command: [ "/bin/sh","-c","cat /etc/config/path/to/msyql.conf" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: cm-demo1 items: - key: mysql.conf path: path/to/msyql.conf
Run Pod
, and view the log:
$ kubectl logs testcm4-pod
host=127.0.0.1
port=3306
Also note that, when the ConfigMap
loading of data into the form of a roll Pod
when, then updates ConfigMap
(or deleted reconstruction ConfigMap
), Pod
the thermal loading configuration information is updated. Then you can increase the number of monitoring scripts configuration file is changed, then reload
the corresponding service.