Running Pod with Daemonsets

KubernetesKubernetesBeginner
Practice Now

Introduction

In Kubernetes, a DaemonSet is a type of controller that ensures a copy of a pod is running on every node in the cluster. This lab will guide you through the process of creating a DaemonSet to run replicas of a pod on every node in the cluster.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/BasicsGroup -.-> kubernetes/initialization("`Initialization`") subgraph Lab Skills kubernetes/describe -.-> lab-8454{{"`Running Pod with Daemonsets`"}} kubernetes/create -.-> lab-8454{{"`Running Pod with Daemonsets`"}} kubernetes/get -.-> lab-8454{{"`Running Pod with Daemonsets`"}} kubernetes/apply -.-> lab-8454{{"`Running Pod with Daemonsets`"}} kubernetes/initialization -.-> lab-8454{{"`Running Pod with Daemonsets`"}} end

Start the Minikube Cluster

Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.

  1. Navigate to your working directory:

    Open the terminal and navigate to the default project folder:

    cd /home/labex/project
  2. Start Minikube:

    Start Minikube to initialize a Kubernetes cluster:

    minikube start
    • This command sets up a single-node Kubernetes cluster on your local machine.
    • Minikube may take a few minutes to start depending on your system's performance.
  3. Verify Minikube is running:

    Check the status of the Minikube cluster:

    minikube status
    • Look for components like kubelet and apiserver listed as Running.
    • If the cluster is not running, rerun minikube start.

If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.

Create a Pod

Create a simple pod that will be used as the template for the replicas. Create a file called /home/labex/project/myapp-pod.yaml with the following contents:

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
    - name: myapp-container
      image: nginx
      ports:
        - containerPort: 80

Create the pod using the following command:

kubectl apply -f /home/labex/project/myapp-pod.yaml

Create a Daemonset

Create a DaemonSet to run replicas of the myapp-pod on every node in the cluster. Create a file called /home/labex/project/myapp-daemonset.yaml with the following contents:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: myapp-daemonset
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp-container
          image: nginx
          ports:
            - containerPort: 80

This DaemonSet uses the myapp-pod as the template for the replicas and sets the matchLabels selector to app: myapp to ensure that the replicas are created on every node.

Create the DaemonSet using the following command:

kubectl apply -f /home/labex/project/myapp-daemonset.yaml

Verify the Daemonset

Verify that the DaemonSet has been created and that replicas of the myapp-pod are running on every node. Use the following command to list the nodes in the cluster:

kubectl get nodes

Use the following command to list the pods created by the DaemonSet:

kubectl get pods -l app=myapp

You should see one pod for each node in the cluster.

Update the Daemonset

Update the DaemonSet to change the image used by the myapp-container. Create a file called /home/labex/project/myapp-daemonsett-update.yaml with the following contents:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: myapp-daemonset
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp-container
          image: busybox
          command: ["sleep", "3600"]

This updated DaemonSet changes the image used by the myapp-container to busybox and sets the command to sleep 3600.

Update the DaemonSet using the following command:

kubectl apply -f /home/labex/project/myapp-daemonset-update.yaml

Verify that the DaemonSet has been updated and that replicas of the myapp-pod are running with the new image. Use the following command to list the pods created by the DaemonSet:

kubectl get pods -l app=myapp

You should see new pods created with the updated image.

Summary

In this lab, you learned how to use a DaemonSet in Kubernetes to run replicas of a pod on every node in the cluster.

Other Kubernetes Tutorials you may like