ConfigMap祥解
ConfigMap与 Secret 类似,用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中。
与 Secret 的区别:
- ConfigMap 保存的是不需要加密的、应用所需的配置信息。
- ConfigMap 的用法几乎与 Secret 完全相同:可以使用 kubectl create configmap 从文件或者目录创建ConfigMap,也可以直接编写 ConfigMap 对象的 YAML 文件。
创建ConfigMap
创建ConfigMap的方式有4种:
命令行方式
- 方式1:通过直接在命令行中指定configmap参数创建,即—from-literal
- 方式2:通过指定文件创建,即将一个配置文件创建为一个ConfigMap,—from-file=<文件>
- 方式3:通过指定目录创建,即将一个目录下的所有配置文件创建为一个ConfigMap,—from-file=<目录>
- 方式4:事先写好标准的configmap的yaml文件,然后kubectl create -f 创建
通过命令行参数—from-literal创建
创建命令:
结果如下面的data内容所示:[root@k8s-master01 ~]# kubectl create configmap test-configmap --from-literal=user=admin --from-literal=pwd=123456
configmap/test-configmap created
[root@k8s-master01 ~]# kubectl get configmap test-configmap -o yaml apiVersion: v1 data: pwd: "123456" user: admin kind: ConfigMap metadata: creationTimestamp: "2021-04-27T21:16:45Z" name: test-configmap namespace: default resourceVersion: "128176" selfLink: /api/v1/namespaces/default/configmaps/test-configmap uid: 179f6019-ac66-4195-9c04-6bb43aba2d6d
通过指定文件创建
编辑配置文件app.properties内容如下:
创建(可以有多个—from-file):[root@k8s-master01 ~]# vim app.properties property.1 = value-1 property.2 = value-2 property.3 = value-3 property.4 = value-4 [mysqld] !include /home/wing/mysql/etc/mysqld.cnf port = 3306 socket = /home/wing/mysql/tmp/mysql.sock pid-file = /wing/mysql/mysql/var/mysql.pid basedir = /home/mysql/mysql datadir = /wing/mysql/mysql/var
结果如下面data内容所示:[root@k8s-master01 ~]# kubectl create configmap test-config1 --from-file=app.properties configmap/test-config1 created
通过指定文件创建时,configmap会创建一个key/value对,key是文件名,value是文件内容。[root@k8s-master01 ~]# kubectl get configmap test-config1 -o yaml apiVersion: v1 data: app.properties: | property.1 = value-1 property.2 = value-2 property.3 = value-3 property.4 = value-4 [mysqld] !include /home/wing/mysql/etc/mysqld.cnf port = 3306 socket = /home/wing/mysql/tmp/mysql.sock pid-file = /wing/mysql/mysql/var/mysql.pid basedir = /home/mysql/mysql datadir = /wing/mysql/mysql/var kind: ConfigMap metadata: creationTimestamp: "2021-04-27T21:23:26Z" name: test-config1 namespace: default resourceVersion: "129104" selfLink: /api/v1/namespaces/default/configmaps/test-config1 uid: af823d8e-7ba7-4e50-b50b-06996d78eccb
如不想configmap中的key为默认的文件名,可以在创建时指定key名字:kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>
指定目录创建
configs 目录下的config-1和config-2内容如下所示: ```shell mkdir config cd config/ vim config1
aaa bbb
c=d
vim config2
eee fff
h=k
创建:
```shell
[root@k8s-master01 config]# cd ..
[root@k8s-master01 ~]# kubectl create configmap test-config3 --from-file=./config
configmap/test-config3 create
结果下面data内容所示:
[root@k8s-master01 ~]# kubectl get configmap test-config3 -o yaml
apiVersion: v1
data:
config1: |
aaa
bbb
c=d
config2: |
eee
fff
h=k
kind: ConfigMap
metadata:
creationTimestamp: "2021-04-27T21:29:24Z"
name: test-config3
namespace: default
resourceVersion: "129925"
selfLink: /api/v1/namespaces/default/configmaps/test-config3
uid: dc07ae06-5a46-44c8-b7c2-815190b3021d
指定目录创建时,configmap内容中的各个文件会创建一个key/value对,key是文件名,value是文件内容。
假如目录中还包含子目录: 说明指定目录时只会识别其中的文件,忽略子目录
通过事先写好configmap的标准yaml文件创建
yaml文件内容如下: 注意其中一个key的value有多行内容时的写法
[root@k8s-master01 ~]# vim configmap.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config4
namespace: default
data:
cache_host: memcached-gcxt
cache_port: "11211"
cache_prefix: gcxt
my.cnf: |
[mysqld]
log-bin = mysql-bin
haha = hehe
创建:
[root@k8s-master01 ~]# kubectl apply -f configmap.yaml
configmap/test-config4 create
结果如下面data内容所示:
[root@k8s-master01 ~]# kubectl get configmap test-config4 -o yaml
apiVersion: v1
data:
cache_host: memcached-gcxt
cache_port: "11211"
cache_prefix: gcxt
my.cnf: |
[mysqld]
log-bin = mysql-bin
haha = hehe
kind: ConfigMap
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","data":{"cache_host":"memcached-gcxt","cache_port":"11211","cache_prefix":"gcxt","my.cnf":"[mysqld]\nlog-bin = mysql-bin\nhaha = hehe\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test-config4","namespace":"default"}}
creationTimestamp: "2021-04-27T21:33:24Z"
name: test-config4
namespace: default
resourceVersion: "130480"
selfLink: /api/v1/namespaces/default/configmaps/test-config4
uid: ce09f202-d68d-48ca-9ab7-d093dbe8fb8f
查看configmap的详细信息:
[root@k8s-master01 ~]# kubectl describe configmap
使用ConfigMap
使用ConfigMap有三种方式,一种是通过环境变量的方式,直接传递pod,另一种是通过在pod的命令行下运行的方式,第三种是使用volume的方式挂载入到pod内
示例ConfigMap文件:
[root@k8s-master01 ~]# vim config-map.yml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: config-map
namespace: default
data:
special.how: very
special.type: charm
[root@k8s-master01 ~]# kubectl apply -f config-map.yml
configmap/config-map created
通过环境变量使用
使用valueFrom、configMapKeyRef、name、key指定要用的key:
[root@k8s-master01 ~]# vim testpod.yml apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: daocloud.io/library/nginx env: #专门在容器里面设置变量的关键字 - name: SPECIAL_LEVEL_KEY #这里的-name,是容器里设置的新变量的名字 valueFrom: configMapKeyRef: name: config-map #这里是来源于哪个configMap key: special.how #configMap里的key - name: SPECIAL_TYPE_KEY valueFrom: configMapKeyRef: name: config-map key: special.type restartPolicy: Never
创建
[root@k8s-master01 ~]# kubectl apply -f testpod.yml pod/dapi-test-pod created
测试:
[root@k8s-master01 ~]# kubectl exec -it dapi-test-pod /bin/sh # echo $SPECIAL_LEVEL_KEY very
通过envFrom、configMapRef、name使得configmap中的所有key/value对儿 都自动变成环境变量: ```shell [root@k8s-master01 ~]# kubectl delete -f testpod.yml pod “dapi-test-pod” deleted [root@k8s-master01 ~]# cp testpod.yml testpod.yml.bak [root@k8s-master01 ~]# vim testpod.yml
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers:
- name: test-container
image: daocloud.io/library/nginx
envFrom:
- configMapRef:
name: config-map
restartPolicy: Never
[root@k8s-master01 ~]# kubectl apply -f testpod.yml pod/dapi-test-pod created
这样容器里的变量名称直接使用configMap里的key名:
```shell
[root@k8s-master01 ~]# kubectl exec -it dapi-test-pod /bin/bash
root@dapi-test-pod:/# env
special.type=charm
special.how=very
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
MYAPP_PORT_80_TCP_ADDR=10.20.179.180
HOSTNAME=dapi-test-pod
MYAPP_SERVICE_PORT_HTTP=80
MYAPP_PORT=tcp://10.20.179.180:80
PWD=/
MYAPP_PORT_80_TCP=tcp://10.20.179.180:80
PKG_RELEASE=1~buster
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.20.0.1:443
MYAPP_SERVICE_PORT=80
MYAPP_SERVICE_HOST=10.20.179.180
NJS_VERSION=0.5.0
TERM=xterm
SHLVL=1
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.20.0.1
KUBERNETES_SERVICE_HOST=10.20.0.1
KUBERNETES_PORT=tcp://10.20.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MYAPP_PORT_80_TCP_PORT=80
NGINX_VERSION=1.19.6
MYAPP_PORT_80_TCP_PROTO=tcp
_=/usr/bin/env
作为volume挂载使用
- 把1.4中test-config4所有key/value挂载进来: ```shell [root@k8s-master01 ~]# kubectl delete -f testpod.yml pod “dapi-test-pod” deleted [root@k8s-master01 ~]# vim volupod.yml
apiVersion: v1 kind: Pod metadata: name: nginx-configmap spec: containers:
- name: nginx-configmap
image: daocloud.io/library/nginx
volumeMounts:
- name: config-volume4 mountPath: “/tmp/config4” volumes:
- name: config-volume4
configMap:
name: test-config4
[root@k8s-master01 ~]# kubectl apply -f volupod.yml
pod/nginx-configmap created
可以看到,在config4文件夹下以每一个key为文件名value为值创建了多个文件。进入容器中/tmp/config4查看: ```shell [root@k8s-master01 ~]# kubectl exec -it nginx-configmap /bin/bash root@nginx-configmap:/# ls /tmp/config4/ cache_host cache_port cache_prefix my.cnf root@nginx-configmap:/# cat /tmp/config4/cache_port 11211