对DaemonSet执行滚动更新
本文介绍了如何对DaemonSet执行滚动更新。
在开始之前
你必须拥有一个Kubernetes的集群,同时你的Kubernetes集群必须带有kubectl命令行工具。建议在至少有两个节点的集群上运行本教程,且这些节点不作为控制平面主机。如果你还没有集群,你可以通过Minikube构建一个你自己的集群,或者你可以使用下面任意一个Kubernetes工具构建:
DaemonSet更新策略
DaemonSet有两种更新策略:
执行滚动更新
要启用DaemonSet的滚动更新功能,必须设置
.spec.updateStrategy.type
为
RollingUpdate
。
你可能想设置
.spec.updateStrategy.rollingUpdate.maxUnavailable
(默认为1),
.spec.minReadySeconds
(默认为0)和
.spec.updateStrategy.rollingUpdate.maxSurge
(一种Beta阶段的特性,默认为0)。
创建带有RollingUpdate更新策略的DaemonSet
下面的YAML包含一个DaemonSet,其更新策略为‘RollingUpdate’:
apiVersion: apps/v1kind: DaemonSetmetadata:name: fluentd-elasticsearchnamespace: kube-systemlabels:k8s-app: fluentd-Loggingspec:selector:matchLabels:name: fluentd-elasticsearchupdateStrategy:type: RollingUpdaterollingUpdate:maxUnavailable: 1template:metadata:labels:name: fluentd-elasticsearchspec:tolerations:# this toleration is to have the daemonset runnable on master nodes# remove it if your masters can't run pods- key: node-role.kubernetes.io/mastereffect: NoSchedulecontainers:- name: fluentd-elasticsearchimage: quay.io/fluentd_elasticsearch/fluentd:v2.5.2volumeMounts:- name: varlogmountPath: /var/log- name: varlibDockercontainersmountPath: /var/lib/docker/containersreadOnly: trueterminationGracePeriodSeconds: 30volumes:- name: varloghostPath:path: /var/log- name: varlibdockercontainershostPath:path: /var/lib/docker/containers
检查了DaemonSet清单中更新策略的设置之后,创建DaemonSet:
kubectl create -f另一种方式是如果你希望使用kubectlapply
来更新DaemonSet的话,也可以使用kubectlapply
来创建DaemonSet:
kubectl apply -f检查DaemonSet的滚动更新策略
首先,检查DaemonSet的更新策略,确保已经将其设置为RollingUpdate
:
kubectl get ds/fluentd-elasticsearch -o go-template='{{.spec.updateStrategy.type}}{{"\n"}}' -n kube-system
如果还没在系统中创建DaemonSet,请使用以下命令检查DaemonSet的清单:
kubectl apply -f--dry-run=client -o go-template='{{.spec.updateStrategy.type}}{{"\n"}}'
两个命令的输出都应该为:
RollingUpdate
如果输出不是RollingUpdate
,请返回并相应地修改DaemonSet对象或者清单。
更新DaemonSet模板
对RollingUpdate
DaemonSet的.spec.template
的任何更新都将触发滚动更新。这可以通过几个不同的命令来完成。
apiVersion: apps/v1kind: DaemonSetmetadata:name: fluentd-elasticsearchnamespace: kube-systemlabels:k8s-app: fluentd-loggingspec:selector:matchLabels:name: fluentd-elasticsearchupdateStrategy:type: RollingUpdaterollingUpdate:maxUnavailable: 1template:metadata:labels:name: fluentd-elasticsearchspec:tolerations:# 这些容忍度使得守护进程能够在控制平面节点上运行# 如果你的控制平面节点不应该运行 pod,请删除它们- key: node-role.kubernetes.io/control-planeoperator: Existseffect: NoSchedule- key: node-role.kubernetes.io/masteroperator: Existseffect: NoSchedulecontainers:- name: fluentd-elasticsearchimage: quay.io/fluentd_elasticsearch/fluentd:v2.5.2resources:limits:memory: 200Mirequests:cpu: 100mmemory: 200MivolumeMounts:- name: varlogmountPath: /var/log- name: varlibdockercontainersmountPath: /var/lib/docker/containersreadOnly: trueterminationGracePeriodSeconds: 30volumes:- name: varloghostPath:path: /var/log- name: varlibdockercontainershostPath:path: /var/lib/docker/containers
声明式命令
如果你使用配置文件来更新DaemonSet,请使用kubectlapply
:
kubectl apply -f指令式命令
如果你使用指令式命令来更新DaemonSets,请使用kubectledit
:
kubectl edit ds/fluentd-elasticsearch -n kube-system
只更新容器镜像
如果你只需要更新DaemonSet模板里的容器镜像,比如.spec.template.spec.containers[*].image
,请使用kubectlsetimage
:

kubectl set image ds/fluentd-elasticsearch fluentd-elasticsearch=quay.io/fluentd_elasticsearch/fluentd:v2.6.0 -n kube-system
监视滚动更新状态
最后,观察DaemonSet最新滚动更新的进度:
kubectl rollout status ds/fluentd-elasticsearch -n kube-system
当滚动更新完成时,输出结果如下:
daemonset "fluentd-elasticsearch" successfully rolled out
故障排查
DaemonSet滚动更新卡住
有时,DaemonSet滚动更新可能卡住,以下是一些可能的原因:
一些节点可用资源耗尽
DaemonSet滚动更新可能会卡住,其Pod至少在某个节点上无法调度运行。当节点上可用资源耗尽时,这是可能的。
发生这种情况时,通过对kubectlgetnodes
和下面命令行的输出作比较,找出没有调度部署DaemonSetPods的节点:
kubectl get pods -l name=fluentd-elasticsearch -o wide -n kube-system
一旦找到这些节点,从节点上删除一些非DaemonSetPod,为新的DaemonSetPod腾出空间。
不完整的滚动更新
如果最近的DaemonSet模板更新被破坏了,比如,容器处于崩溃循环状态或者容器镜像不存在(通常由于拼写错误),就会发生DaemonSet滚动更新中断。
要解决此问题,需再次更新DaemonSet模板。新的滚动更新不会被以前的不健康的滚动更新阻止。
时钟偏差
如果在DaemonSet中指定了.spec.minReadySeconds
,主控节点和工作节点之间的时钟偏差会使DaemonSet无法检测到正确的滚动更新进度。
清理
从名字空间中删除DaemonSet:
kubectl delete ds fluentd-elasticsearch -n kube-system
先安装,如有提示重启先重启一下... 在右下用右键点击daemon tools的图标 ->选最上面的Virtual CD/DVD-ROM菜单->Device 0:[X:]No Media->Mount image然后选择你需要模拟的光驱文件就可以了,X盘就是你所模拟的盘,跟实际的光驱没什么区别. 退出时在盘里面选弹出是没有用的,应该选Unmount image才能退出 另外在在右下用右键点击daemon tools的图标->选最上面的Virtual CD/DVD-ROM菜单->Set number of devices...可以模拟出多个盘
发表评论