Create daemonsets

Create a daemon set to automatically deploy and maintain pod replicas across selected nodes in your cluster, using node selectors to control placement.

When creating daemon sets, the nodeSelector field is used to indicate the nodes on which the daemon set should deploy replicas.

Important
  • If you update a daemon set pod template, the existing pod replicas are not affected.

  • If you delete a daemon set and then create a new daemon set with a different template but the same label selector, it recognizes any existing pod replicas as having matching labels. It does not update them or create new replicas despite a mismatch in the pod template.

  • If you change node labels, the daemon set adds pods to nodes that match the new labels and deletes pods from nodes that do not match the new labels.

To update a daemon set, force new pod replicas to be created by deleting the old replicas or nodes.

Prerequisites
  • The default project-wide node selector in your namespace is set to an empty string by using the openshift.io/node-selector annotation:

    $ oc patch namespace myproject -p \
        '{"metadata": {"annotations": {"openshift.io/node-selector": ""}}}'
    Tip

    You can alternatively apply the following YAML to disable the default project-wide node selector for a namespace:

    apiVersion: v1
    kind: Namespace
    metadata:
      name: <namespace>
      annotations:
        openshift.io/node-selector: ''
    #...
  • If you are creating a new project, the default node selector is overwritten:

    $ oc adm new-project <name> --node-selector=""
Procedure
  1. Define the daemon set yaml file:

    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      name: hello-daemonset
    spec:
      selector:
          matchLabels:
            name: hello-daemonset
      template:
        metadata:
          labels:
            name: hello-daemonset
        spec:
          nodeSelector:
            role: worker
          containers:
          - image: openshift/hello-openshift
            imagePullPolicy: Always
            name: registry
            ports:
            - containerPort: 80
              protocol: TCP
            resources: {}
            terminationMessagePath: /dev/termination-log
          serviceAccount: default
          terminationGracePeriodSeconds: 10
    #...

    where:

    spec.selector.matchLabels.name

    Specifies the label selector that determines which pods belong to the daemon set.

    spec.template.metadata.labels.name

    Specifies the pod template’s label selector. Must match the label selector above.

    spec.template.spec.nodeSelector

    Specifies the node selector that determines on which nodes pod replicas should be deployed. A matching label must be present on the node.

  2. Create the daemon set object by running the following command:

    $ oc create -f daemonset.yaml
Verification
  • Verify that the pods were created and that each node has a pod replica by running the following command:

    $ oc get pods
    Example output
    hello-daemonset-cx6md   1/1       Running   0          2m
    hello-daemonset-e3md9   1/1       Running   0          2m
  • Verify that the pods are placed onto the correct nodes by running the following command:

    $ oc describe pod/hello-daemonset-cx6md|grep Node
    Example output
    Node:        openshift-node01.hostname.com/10.14.20.134