对 DaemonSet 执行滚动更新

    • Kubernetes 1.6 或者更高版本中才支持 DaemonSet 滚动更新功能。

    DaemonSet 更新策略

    DaemonSet 有两种更新策略:

    • OnDelete: 使用 OnDelete 更新策略时,在更新 DaemonSet 模板后,只有当你手动删除老的 DaemonSet pods 之后,新的 DaemonSet Pod 才会被自动创建。跟 Kubernetes 1.6 以前的版本类似。
    • RollingUpdate: 这是默认的更新策略。使用 RollingUpdate 更新策略时,在更新 DaemonSet 模板后, 老的 DaemonSet pods 将被终止,并且将以受控方式自动创建新的 DaemonSet pods。 更新期间,最多只能有 DaemonSet 的一个 Pod 运行于每个节点上。

    要启用 DaemonSet 的滚动更新功能,必须设置 .spec.updateStrategy.typeRollingUpdate

    你可能想设置 (默认为 1) 和 .spec.minReadySeconds (默认为 0)。

    下面的 YAML 包含一个 DaemonSet,其更新策略为 ‘RollingUpdate’:

    检查了 DaemonSet 清单中更新策略的设置之后,创建 DaemonSet:

    1. kubectl create -f https://k8s.io/examples/controllers/fluentd-daemonset.yaml

    另一种方式是如果你希望使用 kubectl apply 来更新 DaemonSet 的话,也可以 使用 kubectl apply 来创建 DaemonSet:

    1. kubectl apply -f https://k8s.io/examples/controllers/fluentd-daemonset.yaml

    首先,检查 DaemonSet 的更新策略,确保已经将其设置为 RollingUpdate:

    1. kubectl get ds/fluentd-elasticsearch -o go-template='{{.spec.updateStrategy.type}}{{"\n"}}' -n kube-system

    如果还没在系统中创建 DaemonSet,请使用以下命令检查 DaemonSet 的清单:

    1. RollingUpdate

    如果输出不是 RollingUpdate,请返回并相应地修改 DaemonSet 对象或者清单。

    RollingUpdate DaemonSet 的 .spec.template 的任何更新都将触发滚动更新。 这可以通过几个不同的 kubectl 命令来完成。

    1. apiVersion: apps/v1
    2. metadata:
    3. name: fluentd-elasticsearch
    4. namespace: kube-system
    5. labels:
    6. k8s-app: fluentd-logging
    7. selector:
    8. matchLabels:
    9. name: fluentd-elasticsearch
    10. updateStrategy:
    11. type: RollingUpdate
    12. rollingUpdate:
    13. maxUnavailable: 1
    14. template:
    15. metadata:
    16. labels:
    17. name: fluentd-elasticsearch
    18. spec:
    19. tolerations:
    20. # this toleration is to have the daemonset runnable on master nodes
    21. # remove it if your masters can't run pods
    22. - key: node-role.kubernetes.io/master
    23. effect: NoSchedule
    24. containers:
    25. - name: fluentd-elasticsearch
    26. resources:
    27. limits:
    28. memory: 200Mi
    29. requests:
    30. cpu: 100m
    31. volumeMounts:
    32. - name: varlog
    33. mountPath: /var/log
    34. - name: varlibdockercontainers
    35. mountPath: /var/lib/docker/containers
    36. readOnly: true
    37. terminationGracePeriodSeconds: 30
    38. volumes:
    39. - name: varlog
    40. hostPath:
    41. path: /var/log
    42. - name: varlibdockercontainers
    43. hostPath:
    44. path: /var/lib/docker/containers

    声明式命令

    如果你使用 配置文件 来更新 DaemonSet,请使用 kubectl apply:

    1. kubectl apply -f https://k8s.io/examples/controllers/fluentd-daemonset-update.yaml

    指令式命令

    如果你使用 指令式命令 来更新 DaemonSets,请使用kubectl edit

    只更新容器镜像

    如果你只需要更新 DaemonSet 模板里的容器镜像,比如,.spec.template.spec.containers[*].image, 请使用 kubectl set image:

    1. kubectl set image ds/fluentd-elasticsearch fluentd-elasticsearch=quay.io/fluentd_elasticsearch/fluentd:v2.6.0 -n kube-system

    最后,观察 DaemonSet 最新滚动更新的进度:

    1. kubectl rollout status ds/fluentd-elasticsearch -n kube-system

    当滚动更新完成时,输出结果如下:

    1. daemonset "fluentd-elasticsearch" successfully rolled out

    故障排查

    有时,DaemonSet 滚动更新可能卡住,以下是一些可能的原因:

    一些节点可用资源耗尽

    发生这种情况时,通过对 kubectl get nodes 和下面命令行的输出作比较, 找出没有调度部署 DaemonSet Pods 的节点:

    一旦找到这些节点,从节点上删除一些非 DaemonSet Pod,为新的 DaemonSet Pod 腾出空间。

    不完整的滚动更新

    如果最近的 DaemonSet 模板更新被破坏了,比如,容器处于崩溃循环状态或者容器镜像不存在 (通常由于拼写错误),就会发生 DaemonSet 滚动更新中断。

    要解决此问题,需再次更新 DaemonSet 模板。新的滚动更新不会被以前的不健康的滚动更新阻止。

    时钟偏差

    如果在 DaemonSet 中指定了 .spec.minReadySeconds,主控节点和工作节点之间的时钟偏差会使 DaemonSet 无法检测到正确的滚动更新进度。

    从名字空间中删除 DaemonSet:

    1. kubectl delete ds fluentd-elasticsearch -n kube-system

    接下来

    • 查看

    最后修改 May 12, 2021 at 6:00 PM PST : [zh]Resync tasks files[11] (e43de9ad1)