Kubernetes配置管理 ConfigMap
一、ConfigMap
1.1 什么是configmap
- kubernetes集群可以使用ConfigMap来实现对容器中应用的配置管理。
- 可以把ConfigMap看作是一个挂载到pod中的存储卷
1.2 创建ConfigMap的4种方式
1.2.1 在命令行指定参数创建
通过直接在命令行中指定configmap参数创建,即
--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 在命令行通过多个文件创建
通过指定文件创建,即将一个配置文件创建为一个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 在命令行通过文件提供多个键值对创建
通过一个文件内多个键值对,
--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 通过YAML资源清单文件创建
通过
kubectl create/apply -f YMAL文件
创建
[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 ConfigMap的2种使用方式
1.3.1 通过环境变量的方式传递给pod
[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 通过volume的方式挂载到pod内
[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 ConfigMap的热更新
1.4.1 ConfigMap热更新方式
如果修改了value, 那么容器内部会不会更新?
- 通过环境变量的方式传递给pod。这种方式不会热更新
- 通过volume的方式挂载到pod内。这种方式会热更新,大概需要半分钟左右。
1.4.2 ConfigMap热更新验证
1.4.2.1 通过环境变量方式
此种方式不会热更新
1.编辑修改对应的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
- 验证对应的pod里的变化,发现很久都不会改变(环境变量方式)
[root@k8s-master1 ~]# kubectl exec pod-cm1 -- env |grep port
port=3306 仍然为3306
1.4.2.2 通过volume方式
- 编辑修改对应的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.验证对应的pod里的变化,一段时间后会改变(卷挂载方式)
[root@k8s-master1 ~]# kubectl exec pod-cm2 -- cat /etc/mysql/port
3308 大概半分钟后更新