configmap热更新
ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中,下文主要是探究 ConfigMap 的创建和更新流程,以及对 ConfigMap 更新后容器内挂载的内容是否同步更新的测试。
使用configmap挂载环境变量
先查看下提前建好的configmap
[root@master test]# kubectl describe configmap test-appvars
Name: test-appvars
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","data":{"appdatadir":"/var/data","apploglevel":"info"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test-appvars","namesp…
Data
====
appdatadir:
—-
/var/data
apploglevel:
—-
info
Events: <none>
创建deployment并挂载上面的configmap文件
[root@master test]# cat hotchange.yaml
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: my-configmap spec: replicas: 1 template: metadata: labels: run: my-configmap spec: containers: - name: hotchange image: busybox command: ["sleep","100000"] ports: - containerPort: 80 envFrom: - configMapRef: name: test-appvars
查看变量apploglevel的值为warn
[root@master test]# kubectl exec my-configmap-b8896f8dd-6krzg env | grep log
apploglevel=info
更改configmap中apploglevel值为warn
[root@master-168-32 test]# kubectl edit configmap test-appvars
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
appdatadir: /var/data
apploglevel: warn
kind: ConfigMap
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","data":{"appdatadir":"/var/data","apploglevel":"info"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test-appvars","namespace":"defa
ult"}}
creationTimestamp: 2018-06-10T07:52:49Z
name: test-appvars
namespace: default
resourceVersion: "11414106"
selfLink: /api/v1/namespaces/default/configmaps/test-appvars
uid: 45591c26-6c83-11e8-a84e-b82a72dbdfdc
"/tmp/kubectl-edit-fplln.yaml" 19L, 765C written
configmap "test-appvars" edited
查看容器里面的logapplevel是否变更
[root@master test]# kubectl exec my-configmap-b8896f8dd-6krzg env | grep log
apploglevel=info
可以查看变量没有更新,所以现在我们需要更新下配置文件怎么做呢,可以通过修改 pod annotations 的方式强制触发滚动更新。
kubectl patch deployment my-configmap --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20180715" }}}}}'
可以看到里面中的变量已经更新
[root@master test]# kubectl exec my-configmap-55c46d4d5f-47r9n env | grep log
apploglevel=warn
使用configmap挂载配置
首先建一个配置文件的configmap
[root@master test]# kubectl create configmap cm-server.xml –from-file=server.xml
创建一个deployment部署刚建好的configmap文件
[root@master test]# cat hotchange.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-configmap
spec:
replicas: 1
template:
metadata:
labels:
run: my-configmap
spec:
volumes:
– name: conf
configMap:
name: cm-server.xml
items:
– key: server.xml
path: server.xml
containers:
– name: hotchange
image: busybox
command: ["sleep","100000"]
ports:
– containerPort: 80
volumeMounts:
– mountPath: "/conf"
name: "conf"
现在看下配置文件
[root@master- test]# kubectl exec my-configmap-7c694dddf5-q8hc4 cat /conf/server.xml | less
<?xml version='1.0' encoding='utf-8'?>
……
更新下configmpa中的配置文件,将其中的1.0改为2.0
kubectl edit configmap cm-server.xml
[root@master test]# kubectl edit configmap cm-server.xml
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
server.xml: |
<?xml version='2.0' encoding='utf-8'?>
……
等待一分钟左右在看下容器中的配置文件是否更改
[root@master test]# kubectl exec my-configmap-7c694dddf5-q8hc4 cat /conf/server.xml | less
<?xml version='2.0' encoding='utf-8'?>
…….
从上面我们可以看出容器的中的配置已经更改!
总结
更新 ConfigMap 后:
-
使用该 ConfigMap 挂载的 Env 不会同步更新
-
使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(大概一分钟左右)才能同步更新