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--
I. background
In many cases, we do a good image for an application, and when we want to modify some of these parameters, it becomes more troublesome, and we have to re-create the image. Do we have a way to let the image call our different configuration files according to different scenarios? then we need to use another resource of K8s, that is, ConfigMap.
We know that in almost all application development, configuration file changes will be involved, such as web programs, need to connect to the database, cache and even queues and so on. One of our applications starts from writing the first line of code and goes through the development environment, the test environment, the pre-release environment and the final online environment. Each environment has to define its own independent configurations. If we can not manage these configuration files well, your operation and maintenance work will suddenly become extremely cumbersome. For this purpose, some large companies in the industry have developed their own set of configuration management centers, such as Qcon of 360th, disconf of Baidu and so on. Kubernetes also provides its own set of solutions, namely ConfigMap. Kubernetes implements the configuration management of the application in the container through ConfigMap.
Second, create a ConfigMap
ConfigMap is the kubernetes resource object used to store configuration files, and all configuration content is stored in etcd.
There are 4 ways to create a ConfigMap:
Created by specifying the configmap parameter directly on the command line, that is,-- from-literal; is created by the specified file, that is, a configuration file is created as a ConfigMap,--from-file=; through multiple key-value pairs in a file,-- from-env-file=; writes the standard configmap yaml file beforehand, and then kubectl create-f is created. 2.1.Adoption-- from-literalkubectl create configmap test-config1-- from-literal=db.host=172.18.8.200-- from-literal=db.port='3306'
View the contents of the configuration.
[root@master] # kubectl get cm test-config1-o yamlapiVersion: v1data: db.host: 172.18.8.200 db.port: "3306" kind: ConfigMapmetadata: creationTimestamp: "2018-12-16T04:32:42Z" name: test-config1 namespace: default resourceVersion: "3676" selfLink: / api/v1/namespaces/default/configmaps/test-config1 uid: a0ee762b-00eb-11e9-9fa7-000c291fb1b32.2, Pass-- from-fileecho-n 172.18.8.200 >. / db.hostecho-n 3306 >. / db.portkubectl create cm test-config2-- from-file=./db.host-- from-file=./db.port
View the configuration content:
[root@master] # kubectl get cm test-config2-o yamlapiVersion: v1data: db.host: 172.18.8.200 db.port: "3306" kind: ConfigMapmetadata: creationTimestamp: "2018-12-16T04:37:50Z" name: test-config2 namespace: default resourceVersion: "4107" selfLink: / api/v1/namespaces/default/configmaps/test-config2 uid: 583ed4e7-00ec-11e9-9fa7-000c291fb1b3
Each file content corresponds to an information entry.
2.3.Adoption-- from-env-filecat env.txtdb.host=172.18.8.200db.port=3306EOFkubectl create cm test-config3-- from-env-file=env.txt
View the configuration content:
[root@master] # kubectl get cm test-config3-o yamlapiVersion: v1data: db.host: 172.18.8.200 db.port: "3306" kind: ConfigMapmetadata: creationTimestamp: "2018-12-16T04:43:02Z" name: test-config3 namespace: default resourceVersion: "4544" selfLink: / api/v1/namespaces/default/configmaps/test-config3 uid: 12746e5f-00ed-11e9-9fa7-000c291fb1b32.4, YAML configuration file
The contents of the profile are as follows.
ApiVersion: v1kind: ConfigMapmetadata: name: test-config4data: db.host: 172.18.8.200 db.port: "3306"
Create and view its contents.
[root@master ~] # kubectl apply-f db.yaml configmap/test-config4 created [root@master ~] # kubectl get cm test-config4-o yamlapiVersion: v1data: db.host: 172.18.8.200 db.port: "3306" kind: ConfigMapmetadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion": "v1", "data": {"db.host": "172.18.8.200", "db.port": "3306"} "kind": "ConfigMap", "metadata": {"annotations": {}, "name": "test-config4", "namespace": "default"}} creationTimestamp: "2018-12-16T04:49:01Z" name: test-config4 namespace: default resourceVersion: "5045" selfLink: / api/v1/namespaces/default/configmaps/test-config4 uid: e87cdafa-00ed-11e9-9fa7-000c291fb1b3 III, ConfigMap use
There are two ways to use ConfigMap:
The first is passed directly to the pod; as an environment variable, and the second is mounted into the pod as a volume. 3.1. Use with environment variables
Use valueFrom, configMapKeyRef, name, key to specify the key to use.
ApiVersion: v1kind: Podmetadata: name: mypodspec: containers:-name: mypod image: busybox args: ["/ bin/sh", "- c", "sleep 3000"] env:-name: DB_HOST valueFrom: configMapKeyRef: name: test-config4 key: db.host-name: DB_PORT valueFrom: configMapKeyRef: name: test-config4 key: db.port
You can also use envFrom, configMapRef, and name to make all key/value pairs in configmap automatically become environment variables.
ApiVersion: v1kind: Podmetadata: name: mypodspec: containers:-name: mypod image: busybox args: ["/ bin/sh", "- c", "sleep 3000"] envFrom:-configMapRef: name: test-config33.2, mounted as volume
Mount all the key/value of test-config4:
ApiVersion: v1kind: Podmetadata: name: mypodspec: containers:-name: mypod image: busybox args: ["/ bin/sh", "- c", "sleep 3000"] volumeMounts:-name: db mountPath: "/ etc/db" readOnly: true volumes:-name: db configMap: name: test-config4
Go to the container and see that multiple files have been created under the db folder with each key as the value of the file name value.
[root@master] # kubectl exec-it mypod-/ bin/sh/ # cd / etc/db/etc/db # ls-altotal 0drwxrwxrwx 3 root root 89 Dec 16 05:23. Drwxr-xr-x 1 root root 16 Dec 16 05:23.. drwxr-xr-x 2 root root 36 Dec 16 05:23.. 2018 _ 12_16_05_23_04.654058863lrwxrwxrwx 1 root Root 31 Dec 16 05:23.. data->.. 2018 _ 12_16_05_23_04.654058863lrwxrwxrwx 1 root root 14 Dec 16 05:23 db.host->.. data/db.hostlrwxrwxrwx 1 root root 14 Dec 16 05:23 db.port->.. data/db.port/etc/db # cat db.host 172.18.8.200/etc/db # 3.3, Hot updates to ConfigMap
Env mounted with this ConfigMap will not be updated synchronously
The data in the Volume mounted using this ConfigMap takes a period of time (measured for about 10 seconds) to be updated synchronously.
3.4. Best use
In most cases, configuration information is provided as a file, so ConfigMap is usually created in-- from-file or YAML mode, and ConfigMap is usually read in Volume mode.
For example, our MySQL configuration file / etc/my.cnf.
[mysqld] datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socksymbolic-links= 0 [mysqld _ safe] logMurray errorcards, var _ max, log _ ABUM, Mariaadb.logpidML, filebanks, var _ RunRun, mariadbUnix, Mariadb.pidhands, mariadb.pidcards, please dedir / etc/my.cnf.d.
Create a CongifMap.
Kubectl create cm mysql-cm-from-file=/etc/my.cnf
View the created cm.
[root@master ~] # kubectl get cm mysql-cm-o yamlapiVersion: v1data: my.cnf: | [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid! includedir / etc/my.cnf.dkind: ConfigMapmetadata: creationTimestamp: "2018-12-16T05:38:29Z "name: mysql-cm namespace: default resourceVersion:" 9273 "selfLink: / api/v1/namespaces/default/configmaps/mysql-cm uid: d1201233-00f4-11e9-9fa7-000c291fb1b3
To use this ConfigMap in Pod, the configuration file is:
ApiVersion: v1kind: Podmetadata: name: mypodspec: containers:-name: mypod image: busybox args: ["/ bin/sh", "- c", "sleep 3000"] volumeMounts:-name: mysql mountPath: "/ tmp" volumes:-name: mysql configMap: name: mysql-cm items:-key: my.cnf path: mysql/my.cnf
Create a Pod and read the configuration information:
[root@master ~] # kubectl exec-it mypod sh/ # cat / tmp/mysql/my.cnf [mysqld] datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socksymbolic-links= 0 [mysqld _ safe] datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socksymbolic-links= 0 [mysqld _ safe] datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socksymbolic-links= 0 [mysqld _ safe]
About hanging on the path, you can modify it yourself.
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.