Hot For Coding
Kubernetes如何在Deployment中更新ConfigMap

Grumpy Cat

前两天我写了一篇文章Kubernetes如何修改从文件创建的ConfigMap内容介绍了如何更新ConfigMap,今天我要说的是ConfigMap更新之后如何让已经在使用Deployment方式部署Pods里的ConfigMap生效,因为当你变更ConfigMap之后已经在运行的Pods是不会自动更新最新的ConfigMap的,需要手动处理一下。

有两种方式干这个事

第一种

由于Deployment方式部署的时候有一个.spec.replicas字段让你写副本是多少个,Kubernetes会让你的Deployment部署的Pod数量永远保持在指定的副本数量,所以我们可以删除掉正在running的Pod,它会自动重建新的Pod,而在重建时会自动使用最新的ConfigMap。如我更改了Traefik配置然后可以这么干:

先获取Pod名

$ kubectl get po -n kube-system | grep traefik
traefik-ingress-controller-5k9zs          1/1     Running   1          8d
traefik-ingress-controller-8spf5          1/1     Running   0          7d6h
traefik-ingress-controller-bsh49          1/1     Running   1          8d

删除

$ kubectl delete po -n kube-system traefik-ingress-controller-5k9zs
pod "traefik-ingress-controller-5k9zs" deleted
$ kubectl delete po -n kube-system traefik-ingress-controller-8spf5
pod "traefik-ingress-controller-8spf5" deleted
$ kubectl delete po -n kube-system traefik-ingress-controller-bsh49
pod "traefik-ingress-controller-bsh49" deleted

简单来说,就是你要过滤出你要删除的Pod,然后在挨个删除,也可以使用for循环处理

for pod_name in $(kubectl get po -n kube-system | grep traefik |  awk '{print $1}');
do
  kubectl delete po -n kube-system $pod_name
done

如果在Namespace下所有的Pod都是Deployment部署的并且是需要更新ConfigMap的,有更直接的命令

$ kubectl delete po --all -n kube-traefik
pod "traefik-ingress-controller-5k9zs" deleted
pod "traefik-ingress-controller-8spf5" deleted
pod "traefik-ingress-controller-bsh49" deleted

第二种

这种方式相比第一种来说,比较圆滑一点,通过restart Deployment来达到目的,命令如下:

$ kubectl rollout restart deploy/traefik-ingress-controller -n kube-system
deployment.apps/traefik-ingress-controller restarted

当然,如果你是DaemonSet也可以使用kubectl rollout restart来处理,不过如果你是CronJob的话就不需要担心了,因为在下一个周期创建Pod时会直接使用最新的ConfigMap配置信息。

TITLE: Kubernetes如何在Deployment中更新ConfigMap

LINK: https://www.qttc.net/504_how_update_latest_configmap_in_pods.html

NOTE: 转载内容请注明出处