Kubernetes configuration management ConfigMap
1. ConfigMaps
1.1 What is configmap
- The kubernetes cluster can use ConfigMap to realize the configuration management of the application in the container .
- Think of ConfigMap as a storage volume mounted to a pod
1.2 4 ways to create ConfigMap
1.2.1 Create by specifying parameters on the command line
Created by specifying the configmap parameter directly on the command line, ie
--from-literal=key=value
;
[root@k8s-master1 ~]# kubectl create configmap cm1 --from-literal=host=127.0.0.1 --from-literal=port=3306
configmap/cm1 created
[root@k8s-master1 ~]# kubectl get cm
NAME DATA AGE
cm1 2 12s
[root@k8s-master1 ~]# kubectl describe cm cm1
Name: cm1
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
host:
----
127.0.0.1
port:
----
3306
Events: <none>
1.2.2 Create from multiple files on the command line
Create by specifying a file, that is, create a configuration file as a ConfigMap
--from-file=文件路径
;
[root@k8s-master1 ~]# echo -n 127.0.0.1 > host
[root@k8s-master1 ~]# echo -n 3306 > port
[root@k8s-master1 ~]# kubectl create configmap cm2 --from-file=./host --from-file=./port
configmap/cm2 created
[root@k8s-master1 ~]# kubectl get cm
NAME DATA AGE
cm1 2 3m45s
cm2 2 94s
[root@k8s-master1 ~]# kubectl describe cm cm2
Name: cm2
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
host:
----
127.0.0.1
port:
----
3306
Events: <none>
1.2.3 Create multiple key-value pairs by providing a file on the command line
Through multiple key-value pairs in a file,
--from-env-file=文件路径
;
[root@k8smaster ~]# vi env.properties
[root@k8smaster ~]# cat env.properties
host=127.0.0.1
port=3306
[root@k8smaster ~]# kubectl create configmap cm3 --from-env-file=env.properties
configmap/cm3 created
[root@k8smaster ~]# kubectl get cm
NAME DATA AGE
cm1 2 76m
cm2 2 4m6s
cm3 2 8s
[root@k8s-master1 ~]# kubectl describe cm cm3
Name: cm3
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
host:
----
127.0.0.1
port:
----
3306
Events: <none>
1.2.4 Create through YAML resource list file
kubectl create/apply -f YMAL文件
created by
[root@k8s-master1 ~]# vim cm4.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm4
data:
host: 127.0.0.1
port: "3306"
[root@k8s-master1 ~]# kubectl apply -f cm4.yml
configmap/cm4 created
[root@k8s-master1 ~]# kubectl get cm
NAME DATA AGE
cm1 2 6m18s
cm2 2 4m7s
cm3 2 113s
cm4 2 11s
[root@k8s-master1 ~]# kubectl describe cm cm4
Name: cm4
Namespace: default
Labels: <none>
Annotations:
Data
====
host:
----
127.0.0.1
port:
----
3306
Events: <none>
1.3 Two ways to use ConfigMap
1.3.1 passed to the pod through environment variables
[root@k8s-master1 ~]# vim pod-cm1.yml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm1
spec:
containers:
- name: busybox
image: busybox
args: [ "/bin/sh", "-c", "sleep 10000" ]
envFrom: # env方式
- configMapRef:
name: cm1 # configmap名称
[root@k8s-master1 ~]# kubectl apply -f pod-cm1.yml
pod/pod-cm1 created
[root@k8s-master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-cm1 1/1 Running 0 9s
[root@k8s-master1 ~]# kubectl exec pod-cm1 -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=pod-cm1
host=127.0.0.1 # 我们创建的configmap传进去的env
port=3306 # 我们创建的configmap传进去的env
DEPLOY_NGINX_PORT_80_TCP=tcp://10.2.205.160:80
DEPLOY_NGINX_PORT_80_TCP_ADDR=10.2.205.160
KUBERNETES_PORT=tcp://10.2.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.2.0.1:443
MY_SERVICE_SERVICE_HOST=10.2.52.46
MY_SERVICE_PORT_80_TCP_ADDR=10.2.52.46
KUBERNETES_SERVICE_HOST=10.2.0.1
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.2.0.1
MY_SERVICE_SERVICE_PORT=80
MY_SERVICE_PORT=tcp://10.2.52.46:80
MY_SERVICE_PORT_80_TCP=tcp://10.2.52.46:80
DEPLOY_NGINX_SERVICE_HOST=10.2.205.160
DEPLOY_NGINX_SERVICE_PORT=80
KUBERNETES_PORT_443_TCP_PORT=443
MY_SERVICE_PORT_80_TCP_PORT=80
DEPLOY_NGINX_PORT=tcp://10.2.205.160:80
DEPLOY_NGINX_PORT_80_TCP_PROTO=tcp
DEPLOY_NGINX_PORT_80_TCP_PORT=80
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
MY_SERVICE_PORT_80_TCP_PROTO=tcp
HOME=/root
1.3.2 Mount to pod by volume
[root@k8s-master1 ~]# vim pod-cm2.yml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm2
spec:
containers:
- name: busybox
image: busybox
args: [ "/bin/sh", "-c", "sleep 10000" ]
volumeMounts: # 用volume挂载方式
- name: vol-cm # 对应下面的volume名
mountPath: "/etc/mysql" # 挂载到容器内部的路径
readOnly: true # 只读
volumes:
- name: vol-cm # 卷名称
configMap:
name: cm2 # configmap的名称
[root@k8s-master1 ~]# kubectl apply -f pod-cm2.yml
pod/pod-cm2 created
[root@k8s-master1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod-cm1 1/1 Running 0 3m51s
pod-cm2 1/1 Running 0 49s
[root@k8s-master1 ~]# kubectl exec pod-cm2 -- ls /etc/mysql
host
port
[root@k8s-master1 ~]# kubectl exec pod-cm2 -- cat /etc/mysql/host
127.0.0.1
[root@k8s-master1 ~]# kubectl exec pod-cm2 -- cat /etc/mysql/port
3306
1.4 Hot update of ConfigMap
1.4.1 ConfigMap hot update method
If the value is modified, will the inside of the container be updated?
- Passed to the pod through environment variables. This method will not be hot updated
- Mount it to the pod by volume. This method will be hot updated, which takes about half a minute.
1.4.2 ConfigMap hot update verification
1.4.2.1 Through environment variables
This method will not be hot updated
1. Edit and modify the corresponding configmap
[root@k8s-master1 ~]# kubectl edit cm cm1
apiVersion: v1
data:
host: 127.0.0.1
port: "3307" 3306修改成3307
kind: ConfigMap
metadata:
creationTimestamp: "2020-11-07T12:07:04Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {
}
f:host: {
}
f:port: {
}
manager: kubectl
operation: Update
time: "2020-11-07T12:07:04Z"
name: cm1
namespace: default
resourceVersion: "169386"
selfLink: /api/v1/namespaces/default/configmaps/cm1
uid: f06cd44d-2ef9-48f2-9ccc-995f9d9ea2ad
- Verify the changes in the corresponding pod and find that it will not change for a long time ( environment variable method )
[root@k8s-master1 ~]# kubectl exec pod-cm1 -- env |grep port
port=3306 仍然为3306
1.4.2.2 Through the volume method
- Edit and modify the corresponding configmap
[root@k8s-master1 ~]# kubectl edit cm cm2
apiVersion: v1
data:
host: 127.0.0.1
port: "3308" 修改成3308
kind: ConfigMap
metadata:
creationTimestamp: "2020-11-07T12:09:15Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {
}
f:host: {
}
f:port: {
}
manager: kubectl
operation: Update
time: "2020-11-07T12:09:15Z"
name: cm2
namespace: default
resourceVersion: "169707"
selfLink: /api/v1/namespaces/default/configmaps/cm2
2. Verify the changes in the corresponding pod, which will change after a period of time ( volume mount method )
[root@k8s-master1 ~]# kubectl exec pod-cm2 -- cat /etc/mysql/port
3308 大概半分钟后更新