Kubernetes基础:ConfigMap:卷挂载方式的热更新

在Kubernetes中使用ConfigMap的卷挂载方式时,一旦ConfigMap有更新,由于此ConfigMap和Pod进行了关联,Kubelet在进行Pod同步时会将所关联的卷标记为RequireRemount(需要重新挂载)的卷,而热更新最大的时间延迟则来源于这个同步的间隔,这篇文章将结合具体示例进行简要说明。

Kubelet的同步时间间隔

设定参数:sync-frequency
缺省设定值:在1.17中缺省设定为1m0s,所以最大同步间隔为1分钟,之所以说最大,因为更新的时间点距其即将开始的同步的时间可能不同。

  • 确认方法
    如果显示的修改了sync-frequency选项的设定,使用systemctl status kubelet -l即可看到参数的设定值
    如果未修改,则可到kubelet的日志中去确认(kubelet.INFO链接所指向的文件),执行示例如下所示:
[root@host131 kubernetes]# view kubelet.INFO 
...省略
I0103 06:12:22.070503    9724 flags.go:33] FLAG: --sync-frequency="1m0s"
I0103 06:12:22.070505    9724 flags.go:33] FLAG: --system-cgroups=""
I0103 06:12:22.070507    9724 flags.go:33] FLAG: --system-reserved=""
I0103 06:12:22.070509    9724 flags.go:33] FLAG: --system-reserved-cgroup=""
I0103 06:12:22.070510    9724 flags.go:33] FLAG: --tls-cert-file=""
I0103 06:12:22.070512    9724 flags.go:33] FLAG: --tls-cipher-suites="[]"
I0103 06:12:22.070515    9724 flags.go:33] FLAG: --tls-min-version=""
I0103 06:12:22.070517    9724 flags.go:33] FLAG: --tls-private-key-file=""
I0103 06:12:22.070519    9724 flags.go:33] FLAG: --topology-manager-policy="none"
I0103 06:12:22.070521    9724 flags.go:33] FLAG: --v="2"
I0103 06:12:22.070523    9724 flags.go:33] FLAG: --version="false"
I0103 06:12:22.070530    9724 flags.go:33] FLAG: --vmodule=""
I0103 06:12:22.070533    9724 flags.go:33] FLAG: --volume-plugin-dir="/usr/libexec/kubernetes/kubelet-plugins/volume/exec/"
I0103 06:12:22.070536    9724 flags.go:33] FLAG: --volume-stats-agg-period="1m0s"
I0103 06:12:22.070578    9724 feature_gate.go:243] feature gates: &{map[]}

ConfigMap准备

[root@host131 configmap]# cat user.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: user-configmap
  namespace: default
data:
  user.name: liumiaocn
  user.id: '1001'
[root@host131 configmap]# 
[root@host131 configmap]# kubectl create -f user.yml 
configmap/user-configmap created
[root@host131 configmap]# 
[root@host131 configmap]# kubectl get cm
NAME             DATA   AGE
user-configmap   2      4s
[root@host131 configmap]#

挂载至pod的卷中

[root@host131 configmap]# cat busybox-pod-volume.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: configmap-test-pod-volume
spec:
  containers:
    - name: busybox-container
      image: busybox:latest
      command: ["sleep", "1000"]
      volumeMounts:
      - name: user-config-volume
        mountPath: /etc/user-config
  volumes:
  - name: user-config-volume
    configMap:
      name: user-configmap
  restartPolicy: Never
[root@host131 configmap]# 
[root@host131 configmap]# kubectl create -f busybox-pod-volume.yaml 
pod/configmap-test-pod-volume created
[root@host131 configmap]# 
[root@host131 configmap]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
configmap-test-pod-volume   1/1     Running   0          5s
[root@host131 configmap]# 

当前值确认

[root@host131 configmap]# kubectl exec configmap-test-pod-volume cat /etc/user-config/user.name /etc/user-config/user.id
liumiaocn1001[root@host131 configmap]# 
[root@host131 configmap]#

修改ConfigMap的值

[root@host131 configmap]# kubectl edit cm user-configmap
configmap/user-configmap edited
[root@host131 configmap]# 

user.id修改为1002,user.name修改为michael

[root@host131 configmap]# kubectl describe cm user-configmap
Name:         user-configmap
Namespace:    default
Labels:       
Annotations:  

Data
====
user.id:
----
1002
user.name:
----
miachel
Events:  
[root@host131 configmap]# 

结果确认

[root@host131 configmap]# i=0; while [ $i -lt 20 ]; 
> do
> sleep 3
> kubectl exec configmap-test-pod-volume cat /etc/user-config/user.name /etc/user-config/user.id
> echo [$i]
> i=`expr $i + 1`
> done
liumiaocn1001[0]
liumiaocn1001[1]
liumiaocn1001[2]
liumiaocn1001[3]
liumiaocn1001[4]
liumiaocn1001[5]
liumiaocn1001[6]
liumiaocn1001[7]
liumiaocn1001[8]
liumiaocn1001[9]
liumiaocn1001[10]
liumiaocn1001[11]
liumiaocn1001[12]
liumiaocn1001[13]
liumiaocn1001[14]
liumiaocn1001[15]
liumiaocn1001[16]
liumiaocn1001[17]
miachel1002[18]
miachel1002[19]
[root@host131 configmap]# 
...

由于执行的时间点的不同对于结果的显示会有较大的区别,多试几次可能会有较大不同,可以结合缩短sync-frequency的设定来进行结果确认。

你可能感兴趣的:(#,深入浅出kubernetes)