Kubernetes
DaemonSet
Scaling
DevOps
Cloud Computing

how to scale kubernetes daemonset to 0?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Scaling a Kubernetes DaemonSet to 0 is a task that can be useful for situations where the workloads managed by the DaemonSet are temporarily not needed, or when you want to conserve resources. This article delves into the technical steps, considerations, and implications of scaling a DaemonSet to 0, along with examples to provide clarity.

Understanding DaemonSets

A DaemonSet in Kubernetes is a controller that ensures a specific pod is running on all or some nodes in a cluster. When a new node is added, Kubernetes automatically schedules the defined pod to run on it. Typical use cases include deploying logging daemons, monitoring agents, or network services like proxies or firewalls on each node.

Why Scale a DaemonSet to 0?

There might be scenarios where you need to temporarily halt the workloads running on every node without deleting the DaemonSet configuration. Some common reasons include:

  • Reducing resource usage during low traffic periods.
  • Debugging, testing, or redeploying updated DaemonSet configurations.
  • Preventing interference during critical operations or upgrades.

Technical Considerations

Scaling a DaemonSet is not directly supported since the very purpose of a DaemonSet is to ensure that a pod runs on every node. However, by leveraging tolerations and node selectors, we can effectively scale the DaemonSet pods down to 0. Here are the technical steps involved:

Step-by-Step Guide

  1. Add a Taint to All Nodes: Taints are applied to nodes and allow a node to repel a set of pods without a matching toleration.
bash
   kubectl taint nodes --all key=value:NoSchedule

This command will taint all nodes with key=value and a NoSchedule effect, preventing any further scheduling of pods without the necessary toleration.

  1. Update DaemonSet with Toleration: Modify the DaemonSet to include a toleration that matches the taint. Once this is active, the pods will not be scheduled due to the presence of the taint.
yaml
1   apiVersion: apps/v1
2   kind: DaemonSet
3   metadata:
4     name: my-daemonset
5   spec:
6     template:
7       spec:
8         tolerations:
9         - key: "key"
10           operator: "Equal"
11           value: "value"
12           effect: "NoSchedule"
  1. Remove the Toleration Temporarily: Without the toleration, the pods will be unscheduled and, effectively, the DaemonSet will scale to 0.
  2. Verify the Changes: Use the following command to verify that no pods from the DaemonSet are currently running:
bash
   kubectl get pods -o wide

With no running pods associated with the DaemonSet, it's successfully scaled to 0.

Restore to Original State

When you're ready to resume operations, remove the taint from the nodes and revert any changes made to the DaemonSet spec.

bash
kubectl taint nodes --all key=value:NoSchedule-

Additional Considerations

  • Impact on Node Resources: Be aware that once taints are applied, it can prevent necessary scheduling of other workloads if not properly handled, potentially affecting cluster operations.
  • Version Compatibility: Ensure that the Kubernetes version in use supports the required taint and toleration features, typically available since Kubernetes 1.6.
  • Automated Scripts: Consider writing scripts to automate the tainting and toleration process for efficiency and accuracy.

Summary Table

TopicDetails
ObjectiveScale DaemonSet to 0 at will
MethodUse taints and tolerations
Commands Involvedkubectl taint, kubectl get pods
ImpactRemoves scheduling of pods while maintaining DaemonSet config
RestorationRemove taints to resume normal operation
Version RequisiteKubernetes 1.6+
Automation PotentialHigh, scriptable task

Scaling a Kubernetes DaemonSet to 0 involves using Kubernetes' built-in mechanisms effectively, allowing you to manage resources efficiently while retaining the ability to quickly revert to full operational status as needed. This approach ensures optimal management of node resources and reduces the manual overhead involved in operational tasks.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.