All Products
Search
Document Center

Container Service for Kubernetes:Create a DaemonSet

Last Updated:Apr 01, 2024

A DaemonSet ensures that only one replicated pod runs on each node. If a new node is added to a cluster, a pod is scheduled to the node. You can use a DaemonSet to run a log collection daemon, a monitoring daemon, or a system management daemon on each node. This topic describes how to create a DaemonSet for a Container Service for Kubernetes (ACK) cluster.

Prerequisites

Procedure

You can use the ACK console or kubectl to create a DaemonSet.

Create a DaemonSet in the ACK console

Create a DaemonSet from an image

  1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Workloads > DaemonSets in the left-side navigation pane.

  3. In the upper-right corner of the DaemonSets page, click Create from Image.

  4. Configure parameters for the DaemonSet.

    1. In the Basic Information step, configure the basic settings of the application. For more information about the parameters, see Create a Deployment from an image.

    2. In the Container step, configure one or more containers. For more information about the parameters, see Create a Deployment from an image.

    3. In the Advanced step, configure the advanced settings of the DaemonSet.

      A DaemonSet can schedule a pod to a node that is in the Unschedulable state. To run a pod on only a specific node, set node affinity, pod affinity, or toleration rules. For more information about the parameters, see Create a Deployment from an image.

  5. Click Create.

    After the DaemonSet is created, you can view the DaemonSet on the DaemonSets page.

    Note

    On the DaemonSets page, select Batch Redeploy to redeploy multiple DaemonSets.

Create a DaemonSet from a YAML template

  1. In the upper-right corner of the DaemonSets page, click Create from YAML.

  2. On the Create page, configure the DaemonSet in the Template section.

  3. In the lower part of the Template section, click Create.

    After the DaemonSet is created, you can view the DaemonSet on the DaemonSets page.

Create a DaemonSet by using kubectl

A DaemonSet can schedule a pod to a node that is in the Unschedulable state. To run a pod on only the specified node, set the following parameters.

Parameter

Description

nodeSelector

A pod is scheduled only to the node with the specified labels.

nodeAffinity

Node affinity. Pods are scheduled to nodes based on node labels. Node affinity allows you to set other matching rules.

podAffinity

Pod affinity. Pods are scheduled to nodes based on pod labels. A pod is scheduled only to the node that runs a pod that matches the affinity rules.

To demonstrate how to create a DaemonSet by using kubectl, a DaemonSet named fluentd-elasticsearch is created in this example.

  1. Create a file named daemonset.yaml and copy the following content to the file:

    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:
          # this toleration is to have the daemonset runnable on master nodes
          # remove it if your masters can't run pods
          - key: node-role.kubernetes.io/master
            effect: NoSchedule
          containers:
          - name: fluentd-elasticsearch
            image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
            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
  2. Run the following command to create the DaemonSet:

    kubectl create -f daemonset.yaml

    If daemonset.apps/fluentd-elasticsearch created is returned, the DaemonSet is created.

What to do next

After you create a DaemonSet, you can perform the following operations:

  • On the DaemonSets page, click the Label field, enter the key and value that you specified for the application, and then click OK to filter the applications.

  • On the DaemonSets page, find the created DaemonSet and click Details in the Actions column. On the details page, you can view basic information about the DaemonSet. The information includes pods, access method, events, and logs.

  • On the DaemonSets page, find the created DaemonSet and choose More > View in YAML in the Actions column to view the YAML file of the DaemonSet. You can also choose More > Delete to delete the DaemonSet.