Using DaemonSets in Kubernetes

Introduction

DaemonSets are a great way to ensure a pod replica runs dynamically on each node. They even automatically handle the creation and removal of such pods when nodes join or leave the cluster. In this lab, you will have the opportunity to practice your skills with DaemonSets by using them to run pods on all nodes in an existing cluster.

Solution

Log in to the lab server using the credentials provided:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

Create a DaemonSet Specification YAML File

  1. Create a DaemonSet descriptor:

    vi daemonset.yml
    
  2. Add the following to the file:

    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      name: beebox-cleanup
    spec:
      selector:
        matchLabels:
          app: beebox-cleanup
      template:
        metadata:
          labels:
            app: beebox-cleanup
        spec:
          containers:
          - name: busybox
            image: busybox:1.27
            command: ['sh', '-c', 'while true; do rm -rf /beebox-temp/*; sleep 60; done']
            volumeMounts:
            - name: beebox-tmp
              mountPath: /beebox-temp
          volumes:
          - name: beebox-tmp
            hostPath:
              path: /etc/beebox/tmp
    
  3. Save and exit the file by pressing Escape followed by :wq.

Create the DaemonSet in the Cluster

  1. Create the DaemonSet in the cluster:

    kubectl apply -f daemonset.yml
    
  2. Get a list of pods, and verify a DaemonSet pod is running on each worker node:

    kubectl get pods -o wide