1、使用目录创建
#创建两个配置文件
[root@k8s-master01-etcd01 dir]# ls game.properties test.conf [root@k8s-master01-etcd01 dir]# cat game.properties name=xiangwei age=22 weapon=sword [root@k8s-master01-etcd01 dir]# cat test.conf course=python log-config=INFO
#创建configmap [root@k8s-master01-etcd01 dir]# kubectl create configmap base-config --from-file=../dir/ configmap/base-config created
#查看 [root@k8s-master01-etcd01 dir]# kubectl get cm NAME DATA AGE base-config 2 27s [root@k8s-master01-etcd01 dir]# kubectl describe cm base-config Name: base-config Namespace: default Labels: <none> Annotations: <none> Data ==== game.properties: ---- name=xiangwei age=22 weapon=sword test.conf: ---- course=python log-config=INFO Events: <none>
--from-file这个参数可以使用多次,可以直接指定配置文件名称,效果跟指定整个目录一样
kubectl create configmap game-config --from-file=../dir/game.properties kubectl create configmap test-config --from-file=../dir/test.conf
2、使用yaml创建
创建单个数值
[root@k8s-master01-etcd01 yaml]# cat config.yaml apiVersion: v1 kind: ConfigMap metadata: name: special-config namespace: default data: special.how: very special.type: charm
[root@k8s-master01-etcd01 yaml]# cat config1.yaml apiVersion: v1 kind: ConfigMap metadata: name: log-config namespace: default data: log_level: INFO
[root@k8s-master01-etcd01 yaml]# kubectl apply -f config.yaml configmap/special-config created [root@k8s-master01-etcd01 yaml]# kubectl apply -f config1.yaml configmap/log-config created [root@k8s-master01-etcd01 yaml]# kubectl get cm NAME DATA AGE base-config 2 17m log-config 1 62s special-config 2 10m
#查看 [root@k8s-master01-etcd01 yaml]# kubectl describe cm special-config Name: special-config Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","data":{"special.how":"very","special.type":"charm"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"special-co... Data ==== special.how: ---- very special.type: ---- charm Events: <none> [root@k8s-master01-etcd01 yaml]# kubectl describe cm log-config Name: log-config Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","data":{"log_level":"INFO"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"log-config","namespace":"default"}} Data ==== log_level: ---- INFO Events: <none>
在pod中使用configmap来替代环境变量
[root@k8s-master01-etcd01 yaml]# cat test.pod.yaml apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test-busybox image: busybox:1.28.4 command: ["/bin/sh","-c","env"] env: #自定义环境变量名称的方式 - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: special-config key: special.how - name: SPECIAL_TYPE_KEY valueFrom: configMapKeyRef: name: special-config key: special.type envFrom: #由于base-config是文件的方式创建,可以看到环境变量中没有里面的数据 - configMapRef: name: base-config envFrom: #环境变量名称使用configmap中定义的key方式 - configMapRef: name: log-config restartPolicy: Never
[root@k8s-master01-etcd01 yaml]# kubectl apply -f test.pod.yaml
pod/test-pod created
[root@k8s-master01-etcd01 yaml]# kubectl logs test-pod KUBERNETES_PORT=tcp://10.0.0.1:443 KUBERNETES_SERVICE_PORT=443 HOSTNAME=test-pod SHLVL=1 HOME=/root SPECIAL_TYPE_KEY=charm #special-config配置 NGINX_SERVICE_PORT_8001_TCP_ADDR=10.0.0.64 NGINX_SERVICE_PORT_8001_TCP_PORT=8001 NGINX_SERVICE_PORT_8001_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin NGINX_SERVICE_SERVICE_HOST=10.0.0.64 KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_PROTO=tcp NGINX_SERVICE_PORT_8001_TCP=tcp://10.0.0.64:8001 SPECIAL_LEVEL_KEY=very #special-config配置 NGINX_SERVICE_SERVICE_PORT=8001 NGINX_SERVICE_PORT=tcp://10.0.0.64:8001 log_level=INFO #log-config配置 KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443 KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_SERVICE_HOST=10.0.0.1 PWD=/
3、创建配置文件,以数据卷的方式挂载
挂载配置文件,挂载前面通过文件定义的configmap
[root@k8s-master01-etcd01 yaml]# cat test.pod1.yaml apiVersion: v1 kind: Pod metadata: name: test1-pod spec: containers: - name: test1-busybox image: busybox:1.28.4 command: ["/bin/sh","-c","sleep 600s"] volumeMounts: - name: settings mountPath: /opt/settings.conf subPath: game.properties #指定配置文件,如果没有该配置,挂载点为一个目录 volumes: - name: settings configMap: name: base-config restartPolicy: Never
[root@k8s-master01-etcd01 yaml]# kubectl apply -f test.pod1.yaml pod/test1-pod created [root@k8s-master01-etcd01 yaml]# kubectl exec -it test1-pod -- cat /opt/settings.conf/test.conf course=python log-config=INFO [root@k8s-master01-etcd01 yaml]# kubectl exec -it test1-pod -- cat /opt/settings.conf/game.properties name=xiangwei age=22 weapon=sword
指定配置文件
[root@k8s-master01-etcd01 yaml]# kubectl exec -it test1-pod -- cat /opt/settings.conf name=xiangwei age=22 weapon=sword
使用yaml创建配置文件
[root@k8s-master01-etcd01 yaml]# cat config2.yaml apiVersion: v1 kind: ConfigMap metadata: name: nginx-config namespace: default data: nginx.conf: | { "ConnectionStrings": {"Redis": "redis-server","Reservation": "Server=localhost;uid=liweihan;pwd=**;database=Reservation", "ElasticSearch": "elasticsearch" }, "MpWechat":{ "AppId": "wx4a41d3773ae55543", "AppSecret": "**********", "Token": "AmazingDotNet", "AESKey": "------------" }, "AppSettings": { "WechatSubscribeReply": "", "SentryClientKey": "https://**" }, "Tencent": { "Captcha": { "AppId": "2062135016", "AppSecret": "****" } }, "GoogleRecaptcha": { "SiteKey": "6Lc-**", "Secret": "6Lc-**" }, "Logging": { "LogLevel": { "Default": "Warning", "ActivityReservation": "Debug", "RequestLog": "Debug" } } }
注意:|符号下面的字符也要符合yaml格式,开头空两格
[root@k8s-master01-etcd01 yaml]# cat test.pod2.yaml apiVersion: v1 kind: Pod metadata: name: test-pod2 spec: containers: - name: test2-busybox image: busybox:1.28.4 command: ["/bin/sh","-c","sleep 600s"] volumeMounts: - name: settings mountPath: /opt/nginx.conf subPath: nginx.conf volumes: - name: settings configMap: name: nginx-config restartPolicy: Never
[root@k8s-master01-etcd01 yaml]# kubectl apply -f config2.yaml configmap/nginx-config created [root@k8s-master01-etcd01 yaml]# kubectl apply -f test.pod2.yaml pod/test-pod2 created
[root@k8s-master01-etcd01 yaml]# kubectl exec -it test-pod2 -- cat /opt/nginx.conf { "ConnectionStrings": {"Redis": "redis-server","Reservation": "Server=localhost;uid=liweihan;pwd=**;database=Reservation", "ElasticSearch": "elasticsearch" }, "MpWechat":{ "AppId": "wx4a41d3773ae55543", "AppSecret": "**********", "Token": "AmazingDotNet", "AESKey": "------------" }, "AppSettings": { "WechatSubscribeReply": "", "SentryClientKey": "https://**" }, "Tencent": { "Captcha": { "AppId": "2062135016", "AppSecret": "****" } }, "GoogleRecaptcha": { "SiteKey": "6Lc-**", "Secret": "6Lc-**" }, "Logging": { "LogLevel": { "Default": "Warning", "ActivityReservation": "Debug", "RequestLog": "Debug" } } }
4、更新configMap
修改configMap中的值后,env环境变量中的值不会动态更新。
以挂载配置文件方式的,修改configMap中的值后,大约10秒后,容器中的值也会更新。
如果是deployment,可以通过更新注释的方式来触发更新。修改configmap的值,目前并不会触发相关Pod的滚动更新,可以通过修改Pod annotations的方式强制触发滚动更新
[root@k8s-master01-etcd01 yaml]# kubectl patch deployments my-busybox --patch '{"spec": {"template": {"metadata": {"annotations": {"version":"20191119"}}}}}' deployment.apps/my-busybox patched