# How to manage clusters with Kubernetes DaemonSet

Kubernetes DaemonSets simplify the management of applications or services that you need to run on each node in the cluster. Configuration and changes to a DaemonSet are automatically applied to all nodes.

## What are Kubernetes DaemonSets?

Kubernetes DaemonSets are resources that ensure that at least one instance of a specific pod runs on every node in the cluster. The application or service is therefore **globally present in the entire cluster environment**. DaemonSets are particularly suitable for tasks such as monitoring, logging or network services that must be available on every node in the cluster. A major advantage of DaemonSets is their scalability, as they automatically start pods and delete existing pods when nodes fail or are removed. As a result, DaemonSets improve reliability and make cluster management easier.

Tip With the cloud [managed Kubernetes](https://cloud.ionos.com/managed/kubernetes "Managed Kubernetes from IONOS") solution from IONOS, you benefit from a fully automated cluster setup, as well as high-performance workloads and fail safety.

## How to configure Kubernetes DaemonSet

Pod distribution is granularly controllable based on **node properties, resource requirements** or other user-defined criteria. Here, we explain how to set up and use a DaemonSet.

### Create a DaemonSet YAML file

First, you must define the configuration of the DaemonSet in a **YAML file**. This file contains information such as the name of the DaemonSet, the type of pod used and the specific settings for placement on the nodes.

Here’s an example of a YAML file that creates a pod with an NGINX container on each node:

```yaml
apiVersion: apps/v1 
kind: DaemonSet 
metadata: 
    name: nginx-daemonset 
spec: 
    selector: 
        matchLabels: 
            app: nginx 
    template: 
        metadata: 
            labels: 
                app: nginx 
        spec: 
            containers: 
            - name: nginx-container 
                image: nginx:latest
```

### Activate YAML configuration

Open your command line and switch to the directory where your DaemonSet YAML file is located. Then execute the `kubectl apply` command to apply the configuration to your **Kubernetes cluster**:

```shell
kubectl apply -f daemonset.yaml
```

This command tells the Kubernetes cluster to create or update the resources defined in the YAML file.

### Check the Kubernetes DaemonSets

You should check whether the desired pods have been successfully started on each node.

```shell
kubectl get daemonset
```

The output of this command contains relevant information, including:

- **DESIRED**: The desired number of pods to be created by the DaemonSet.
- **CURRENT**: The actual number of pods provided.
- **READY**: The number of pods provided that are ready for operation.
- **UP-TO-DATE**: The number of pods that are currently up to date in terms of DaemonSet configuration.
- **AVAILABLE**: The number of operational pods that are not affected by other resources, such as affinities or tolerances.
- **NODE SELECTOR**: Labels for nodes on which the DaemonSet is to run (if configured).
- **AGE**: The time since the DaemonSet was created.

In our example, you can see that the Kubernetes DaemonSet `nginx-daemonset` has three desired pods and three pods are currently running on the nodes.

```shell
NAME                      DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE 
nginx-daemonset              3         3        3         3            3          <none>       5m
```

### Check the pods

To get a more detailed view of the pods you have created, use this command:

```shell
kubectl get pods
```

The output shows the status of the pods, including readiness, status and runtime.

```shell
NAME                      READY   STATUS    RESTARTS       AGE 
nginx-daemonset-abcde     1/1      Running      0          5m 
nginx-daemonset-fghij     1/1      Running      0          5m 
nginx-daemonset-klmno     1/1      Running      0          5m
```

### Delete a DaemonSet

First, you should check which DaemonSets are present in your cluster. Enter the command `kubectl delete daemonset` followed by the name of the DaemonSet to be deleted in the console. For example:

```shell
kubectl delete daemonset nginx-daemonset
```

### Limit Kubernetes DaemonSets to specific nodes

To run DaemonSets on specific nodes in a Kubernetes cluster, you can use node selectors or affinities in the DaemonSet configuration. This allows you to specify the nodes on which the DaemonSet pods are to be created.

```yaml
apiVersion: apps/v1 
kind: DaemonSet 
metadata: 
    name: nginx-daemonset 
spec: 
    selector: 
        matchLabels: 
            app: nginx 
    template: 
        metadata: 
            labels: 
                app: nginx 
        spec: 
            nodeSelector: 
                custom-label: special-node 
            containers: 
            - name: nginx-container 
                image: nginx:latest
```

In this example, the DaemonSet is configured so that it only runs on nodes with the label `custom-label: special-node`.

### Communicate with DaemonSet pods

There are different ways to communicate with Daemon pods in Kubernetes, depending on the requirements of your application.

- **Push**: In this case, the DaemonSet pods actively send data or updates to an external service, such as a statistics database or a central log system. The DaemonSet pods themselves have no clients and do not initiate any incoming connections.
- **NodeIP and Known Port**: With this method, each DaemonSet pod uses a specific port on the node to be reachable. Clients know the list of available node IPs and reach the pods via the corresponding node IP and port.
- **DNS**: By using a `__headless service__` with the same pod selector as the Kubernetes DaemonSet, you can enable DNS resolution for the individual pods. Clients can then communicate with the DaemonSet pods using the Endpoints resource or by retrieving multiple A records from the DNS.
- **Service**: Creating a regular Kubernetes service with the same pod selector gives clients access to the DaemonSet pods. This is done via the cluster IP address of the service. Note that this does not ensure that a specific node is reached. Instead, the service will forward the request to one of the available DaemonSet pods.

You can find more detailed information on cluster management for beginners in the [Kubernetes tutorial](https://www.ionos.com/digitalguide/server/configuration/kubernetes-tutorial/) from our guide.


This is a markdown version of: [https://www.ionos.com/digitalguide/server/configuration/kubernetes-daemonset/](https://www.ionos.com/digitalguide/server/configuration/kubernetes-daemonset/) for AI/LLM consumption.