[Kubernetes resources articles] DaemonSet controller entry practical detailed explanation

1. Theoretical knowledge of DaemonSet controller

Chinese official document reference:

1. What is the DaemonSet controller?

The DaemonSet (abbreviated ds) controller in Kubernetes is a controller used to run the daemon process application. It ensures that each Node node runs a Pod copy with the specified configuration. When the Node node joins or deletes the DaemonSet controller, it will Automatically create or delete corresponding Pod replicas.

Features:

  1. Run only one instance per node: DaemonSet ensures that only one Pod instance runs on each node. This ensures that the daemons deployed on each node are tightly coupled to the host for higher availability and reliability.

  2. Rolling Upgrades: When updating a DaemonSet, rolling upgrades can be controlled in a few different ways. You can choose to update all pod instances at once, or you can gradually delete old pod instances before running the new version.

  3. Execute the task before starting: can pass DaemonSet's

2. Working principle of DaemonSet controller

The DaemonSet controller will monitor K8s daemonset objects, pod objects, and node objects. If these monitored objects change, a syncLoop cycle will be triggered to allow the K8s cluster to evolve towards the state described by the daemonset object.

3. Typical application scenarios of DaemonSet

  • Log and metric collection: run a log and metric collector on each node, such as flunentd, logstash, filebeat, etc.

  • Data storage: run data storage on each node, such as glusterd, ceph, etc.

  • Monitoring components: run monitoring components on each node, such as prometheus, node_exporter, collectd, etc.

4. The difference between DaemonSet and Deployment

  • The Pods created by the deployment will be distributed on each Node node, and each node may run several copies.
  • The Pod created by daemonset can only run at most one Pod copy on each Node node, which is usually used to run background services and daemon applications.

Therefore, in general, if you want to deploy single-node applications such as background services and daemon processes, you can use DaemonSet; if you need to deploy complex applications, such as Web services, you need to use deployment.

2. Case: Demonstration of DaemonSet Controller

1. Use DaemonSet to deploy log collection components

Deploy the fluentd log collection component (only to demonstrate the use of DaemonSet, not to do ELK overall experiment) YAML resource list is as follows:

cat fluentd-daemonset.yaml 
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: elasticsearch-fluentd
  namespace: default
spec:
  selector:
    matchLabels:
      app: elasticsearch-fluentd
  template:
    metadata:
      labels:
        app: elasticsearch-fluentd
    spec:
      tolerations:              # 定义容忍度,运行在master节点运行(根据自己master的污点定义)
      - effect: NoSchedule  
        key: node-role.kubernetes.io/control-plane
      volumes:                 # 定义卷名称为 system-log
      - name: system-log
        hostPath: 
          path: /var/log
      containers:
      - name: elasticsearch-fluentd
        image: qinziteng/fluentd:2.5.1 
        imagePullPolicy: IfNotPresent
        resources:              # 定义资源限制
          requests:
            cpu: 100m
            memory: 300Mi
          limits:
            memory: 300Mi
        volumeMounts:
        - name: system-log    # 使用system-log卷,挂载到容器/var/log目录
          mountPath: /var/log

Execute the YAML file:

kubectl apply -f fluentd-daemonset.yaml

View the Pod status, as shown in the figure below, you can see that subcontracting creates Pods on each node of the K8s cluster

kubectl get pods -o wide

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-rtDxDp8l-1687509544696) (D:\MD Archives\IMG\image-20230623161952395.png)]

2. DaemonSet manages Pod rolling update

The DaemonSet update strategy spec.updateStrategyis defined in the field, currently supports two update strategies:

  • rollingUpdate: Since daemonset does not support running multiple Pods on one node, the rollingUpdate update strategy is to delete first and then update.
  • OnDelete: No update is performed by default, it needs to be manually deleted and then updated.

Step 1: Create a daemonset resource and use the nginx:1.18.0 mirror, the YAML is as follows:

cat web-daemonset.yaml 
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: web-daemonset
  namespace: default
spec:
  updateStrategy:
    rollingUpdate:
      maxUnavailable: 1
  selector:
    matchLabels:
      app: web-daemonset
  template:
    metadata:
      labels:
        app: web-daemonset
    spec:
      tolerations:            
      - effect: NoSchedule    # 容忍度根据master 污点定义
        key: node-role.kubernetes.io/control-plane
      containers:
      - name: web-daemonset
        image: nginx:1.18.0
        imagePullPolicy: IfNotPresent
        resources:             
          requests:
            cpu: 100m
            memory: 300Mi
          limits:
            memory: 300Mi

Execute the YAML file:

kubectl apply -f web-daemonset.yaml

Check pod status:

kubectl get pods -o wide -l app=web-daemonset

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-4HXmpoUM-1687509544702) (D:\MD Archives\IMG\image-20230623162346648.png)]

Step 2: Update the mirror to usenginx:latest

Only change the image place, do not operate the rest of the place, and then re-apply to make it take effect.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-R9t4TPjr-1687509544704) (D:\MD Archives\IMG\image-20230623162435864.png)]

kubectl apply -f web-daemonset.yaml

Step 3: Verify that the image is updated

kubectl describe pod web-daemonset-2vtd9|grep Image

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-TpUqXnXf-1687509544705) (D:\MD Archives\IMG\image-20230623163222395.png)]

It can be seen that the image version has been updated successfully. This update is actually to delete the old version first, and then use the new image version to create.

3. Summary

  • The DaemonSet controller is used to control each Node node to run a specified Pod replica set. When a Node node is added or deleted, DaemonSet will automatically create a specified Pod replica set to ensure that each Node node runs this Pod. Used in scenarios such as collecting logs and monitoring.
  • The DaemonSet rollingUpdate update strategy is to delete the old version of the Pod first, and then create the new version of the Pod, because each Node node can only create one Pod replica set.

Guess you like

Origin blog.csdn.net/weixin_45310323/article/details/131351965