SoFunction
Updated on 2025-04-09

Method for k8s to control the number of pods in deamonset

DaemonSet is a controller in Kubernetes that ensures that each node in the cluster (or node matching a specific tag selector) runs a copy of a Pod. DaemonSet is usually used to run cluster daemons, such as log collection, monitoring agent, storage volume plug-ins, etc. Here is how to control the number of pods in DaemonSet:

1. Use Node Selector

By setting nodeSelector in the spec of DaemonSet, you can specify which nodes run the pods. For example, if you want to run a Pod on a node with a specific tag, you can do this:

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
nodeSelector:
key: value
containers:
  • name: example-container
    image: example-image

In this example, only nodes with the key=value tag will run the pod.

2. Use Node Affinity

Node affinity provides finer granular control, and nodeAffinity can be used to specify which nodes the Pod should be scheduled. For example:

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: key
operator: In
values:
- value
containers:
- name: example-container
image: example-image

In this example, only nodes that meet the key In [value] condition will run the pod.

3. Tolerations

If there are taints on some nodes, you can allow Pods to be scheduled to these nodes by setting tolerances. For example:

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
tolerations:
- key: key
operator: Equal
value: value
effect: NoSchedule
containers:
- name: example-container
image: example-image

In this example, the Pod will tolerate nodes with key=value stains.

4. Update Strategy

DaemonSet supports rolling updates, and you can control the number of pods during the update process by setting updateStrategy. For example:

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example-container
image: example-image
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 0

In this example, maxUnavailable is set to 1, indicating that at most one Pod is unavailable during the update process; maxSurge is set to 0, indicating that no additional Pods are created during the update process.
Through the above methods, you can flexibly control the number and scheduling strategies of Pods in DaemonSet.

This is the article about how to control the number of pods in k8s Deamonset. For more related content on pods in k8s Deamonset, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!