Skip to main content

Command Palette

Search for a command to run...

How do you ensure that a pod runs on each node?

Published
2 min read
How do you ensure that a pod runs on each node?
R

I am a Site Reliability Engineer with nearly 5 years of experience. I talk about Linux, Automation, Networking, and anything else related to tech and CS.

In Kubernetes, a Daemonset is a type of controller that ensures that a specific pod is running on all (or a subset of) nodes in a cluster. Daemonsets are typically used for running system-level services like logging agents, monitoring agents, or other types of infrastructure that need to run on every node in the cluster.

A daemonset creates one pod on each node and ensures that it stays running as long as the node is available. When a new node is added to the cluster, the daemonset controller creates a new pod on that node. When a node is removed from the cluster, the daemonset controller automatically removes the pod from that node.

DaemonSets are a powerful tool that can be used to ensure that essential services are running on every node in a cluster. They are also useful for running applications that need to be installed on every node, such as logging and monitoring daemons.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
  namespace: kube-system
spec:
  selector: #Should only manage pods with labels app: fluentd
    matchLabels:
      app: fluentd
  template: #Specifies the configurations of the pods
    metadata:
      labels:
        app: fluentd
    spec:
      containers:
        - name: fluentd
          image: fluent/fluentd:v1.3
          volumeMounts:
            - name: varlog
              mountPath: /var/log
          resources:
            limits:
              memory: 200Mi
            requests:
              cpu: 100m
              memory: 100Mi
      volumes:
        - name: varlog
          hostPath:
            path: /var/log

In this example, we're using a daemonset to run Fluentd, a popular logging agent, on every node in the cluster. The selector field specifies that the daemonset should only manage pods with the app: fluentd label, and the template field specifies the configuration of the pods.

In the containers section, we define a single container that runs the fluent/fluentd:v1.3 image. We also mount the host's /var/log directory into the container so that Fluentd can collect logs from the node. Finally, we specify resource limits to ensure that Fluentd doesn't consume too much CPU or memory on each node.

With this configuration, Kubernetes will automatically create a Fluentd pod on each node in the cluster and ensure that it stays running as long as the node is available. If a node is added or removed from the cluster, the daemonset controller will automatically adjust the number of pods to match the desired state.