Skip to main content Link Menu Expand (external link) Document Search Copy Copied

22. Controller - DaemonSet

Table of contents
  1. 22. Controller - DaemonSet
    1. Summary
    2. DaemonSet definition
    3. DaemonSet example-1
      1. Prepare test
      2. test
      3. Check the DaemonSet status at Master
    4. DaemonSet example-2: Rolling update & Rollback
      1. Check the pods and daemonset status
      2. Test
      3. Check the pods event
    5. DaemonSet example-3: Rolling Rollback

Summary

  • A DaemonSet is a controller that deploys pods to all nodes or a specific node group in a cluster.

  • Unlike other replication controllers, DaemonSets cannot be scheduled for failure - they must always run on every node.

  • When new nodes are added, DaemonSets automatically deploy pods to those nodes.

  • When nodes are removed, DaemonSets automatically delete the pods running on those nodes.

  • DaemonSets are commonly used for deploying system processes such as log collectors or monitoring agents, which need to run on every node in the cluster.

DaemonSet definition

ReplicaSet
        
apiVersion: apps/v1     #rc apiVersion
kind: ReplicaSet        #rc kind
metadata:
  name: rs-nginx        #rc name
spec:
  replicas: 3           # Min. Pods Numb.
  selector:
    matchLabels:
      app: webui        #no meaning
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
    containers:
    - name: nginx-container     #container name
            image: nginx:1.14   #container image
        
      
Deployment
        
apiVersion: apps/v1                # DaemonSet apiVersion
kind: DaemonSet                    # DaemonSet kind option
metadata:
  name: daemonset-nginx
spec:
  selector:                        # no replicas option
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
        
      

DaemonSet example-1

Prepare test

  • Create DaemonSet by yaml

    vim daemonset-exam.yaml
    
    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      name: daemonset-nginx
    spec:
      selector:
        matchLabels:
          app: webui
      template:
        metadata:
          name: nginx-pod 
          labels:
            app: webui
        spec:
          containers:
          - name: nginx-container
            image: nginx:1.14
    
  • Turn on the watch mode for pods(T1)

    watch kubectl get pods -o wide
    

    6

  • Delete node3

    kubectl delete nodes node3
    

    1

  • enforce daemonset-exam.yaml

    kubectl create -f daemonset-exam.yaml
    

    2

  • Check the daemonset status again

    kubectl get daemonset
    

    7

  • Check the T1 watch again

    8

test

  • Check the token list at Master

    sudo kubeadm token list
    
  • Get a new token reissued.

    sudo kubeadm token create --ttl 1h
    

    9

  • Get a new hash code

    sudo openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
    

    10

  • Node3 re-register to Master

    sudo kubeadm join (Master Ip Address):6443 --token (Token value) --discovery-token-ca-cert-hash sha256:
    (Token hash code)
    

    11

Check the DaemonSet status at Master

  • T1(watch kubectl get pods -o wide) 8 4 5

  • deamonset status

    kubectl get daemonset.apps
    

    7 12

DaemonSet example-2: Rolling update & Rollback

Check the pods and daemonset status

  • Turn on watching mode to pods
    13

  • Turn on watching mode to daemonset 14

Test

  • Change Nginx version to 1.15 in edit mode

    kubectl edit daemonsets.app daemonset-nginx
    
    ...
          spec:
            containers:
            - image: nginx:1.14     # Change to nginx:1.15
              imagePullPolicy: IfNotPresent
            name: nginx-container
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
    ...
    

    <!– <div class="video-container">

</div>

–>

Check the pods event

  • Pod in node3
    1615

  • Pod in node2 1718

  • Pod in node3 1920

DaemonSet example-3: Rolling Rollback

  • Check the image version check within pod’s container

    kubectl get daemonset.apps -o wide
    

    21

  • Downgrade container image nginx to before current

    ```bash kubectl rollout undo daemonset daemons


Table of contents