DaemonSet is used to deploy resident background programs in the cluster. For example, it can collect logs for a node. DaemonSet ensures that specified Pods are running on all or certain nodes. When you add new nodes to a cluster, Pods are deployed automatically. When nodes are removed from the cluster, Pods are recovered automatically.
If nodeSelector or affinity parameters are configured on the Pod, the Pod managed by DaemonSet is scheduled based on the specified rules. If the nodeSelector or affinity parameters are not configured on the Pod, the Pod will be deployed on all nodes.
latest
, the Always
policy is used. Otherwise, the IfNotPresent
policy is used.Note:Rolling update of DaemonSet is only supported in Kubernetes v1.6 or higher.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: k8s.gcr.io/fluentd-elasticsearch:1.20
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
Note:Note: The YAML sample described above comes from
https://kubernetes.io/docs/concepts/workloads/controllers/daemonset
. The container image may not be got during creation. The sample is only used to describe the composition of DaemonSet.
For more information, see Kubernetes' official document about DaemonSet.
kubectl create -f <DaemonSet YAML filename>
For example, to create a StatefulSet YAML file named fluentd-elasticsearch.yaml, run the following command:
kubectl create -f fluentd-elasticsearch.yaml
kubectl get DaemonSet
If a message similar to the following is returned, the creation is successful.
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
frontend 0 0 0 0 0 app=frontend-node 16d
Run the following command to view the update policy type of the DaemonSet.
kubectl get ds/<daemonset-name> -o go-template='{{.spec.updateStrategy.type}}{{"\n"}}'
DaemonSet has the following two update policy types:
-OnDelete: default update policy. With this policy, after the DaemonSet is updated, you have to manually delete the old DaemonSet Pod to create a new one.
-RollingUpdate: Kubernetes 1.6 or later is supported. With this policy, after the DaemonSet template is updated, the old DaemonSet Pod will be killed, and a new one will be created in a rolling update manner.
Run the following command to update a DaemonSet.
kubectl edit DaemonSet/[name]
This method applies to simple debugg verification. It is not recommended to use it in production environments. You can update any DaemonSet parameters in this way.
Run the following command to update the image of the specified container.
kubectl set image ds/[daemonset-name][container-name]=[container-new-image]
It is recommended to keep other DaemonSet parameters unchanged and only update the container image when the business is updated.
Run the following command to view the update history of the DaemonSet.
kubectl rollout history daemonset /[name]
Run the following command to view the details of the specified version.
kubectl rollout history daemonset /[name] --revision=[REVISION]
Run the following command to roll back to the earlier version.
kubectl rollout undo daemonset /[name]
To specify the rollback version, run the following command.
kubectl rollout undo daemonset /[name] --to-revision=[REVISION]
Run the following command to delete a DaemonSet.
kubectl delete DaemonSet [NAME]
Was this page helpful?