Kubernetes
Pod Scheduling
Worker Node
DaemonSet
Container Orchestration

Kubernetes How to ensure one pod gets scheduled on each worker node?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Kubernetes, often abbreviated as K8s, has become the de facto standard for container orchestration. Its extensive capabilities make it possible to manage containerized applications efficiently and at scale. A common requirement within Kubernetes is to ensure that a specific pod is scheduled onto each worker node. This can be useful in scenarios where you need monitoring agents, logging collectors, or network proxies deployed uniformly across every node. This article delves into how you can achieve this using Kubernetes concepts and tools.

Understanding DaemonSets

DaemonSets are one of the most efficient ways to ensure that a single Pod runs on each node in a Kubernetes cluster. They are particularly helpful for running system daemons and other infrastructure services. When a new node is added to the cluster, the DaemonSet controller ensures that each newly added node gets the daemon Pod. Conversely, if nodes are removed, the Pods are removed as well.

How DaemonSets Work

Every Pod created by a DaemonSet runs a copy of itself on every node in the cluster. The Kubernetes control plane manages this process. When nodes are or join the cluster, the set of pods gets adjusted accordingly.

Here’s a simple YAML configuration for a DaemonSet:

  • name: example-daemon-container
  • apiVersion: This is typically `apps/v1` for DaemonSets.
  • kind: Specifies the resource type, which is `DaemonSet`.
  • metadata: Contains data that helps identify the DaemonSet, such as its name.
  • spec: Describes the desired state. The `template` defines the Pod that will be created for each node.
  • Label Selectors: DaemonSets allocate and manage pods based on label selectors. It’s crucial to ensure that the labels match.
  • Node Selectors and Affinities: If you want to limit which nodes will run your Pods, you can use node selectors or affinity rules in the Pod specs.
  • Resource Limits and Requests: DaemonSets should have resource allocations defined to avoid resource contention, ensuring the DaemonSet pods do not overwhelm the nodes.
  • kubectl commands: Use `kubectl get daemonset` to list all DaemonSets. Detailed information can be obtained using `kubectl describe daemonset ``<name>```.
  • Logs and Events: To ensure smooth operation, monitor the logs generated by your daemon pods. Events related to DaemonSets can be checked for debugging and auditing purposes.
  • Scaling and Updates: DaemonSets are auto-scaling by design, but you do have control over rolling updates. Kubernetes will automatically ensure all nodes adhere to the desired configuration when changes occur.

Course illustration
Course illustration

All Rights Reserved.