Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to scale kubernetes daemonset to 0?

Tags:

kubernetes

When the pod controlled by daemonset,Some error occur in the pod and it's state will be CrashLoopBackOff, I want to delete these pods but not delete the DaemonSet.

So I want to scale daemonset to 0, as far as I known, DaemonSet Spec do not support the replica of the pod.

How can I get there?

like image 627
litanhua Avatar asked Dec 26 '18 08:12

litanhua


People also ask

How do you scale a DaemonSet to zero?

DaemonSet ensures that every node run a copy of a Pod. So you can't scale down it as Deployment. DaemonSet use DaemonSet Controller and Deployment use Replication Controller for replications. So You can simply delete the DaemonSet.

How do you stop the DaemonSet in Kubernetes?

To do that, simply run the kubectl delete command with the DaemonSet. This would delete the DaemonSet with all the underlying pods it has created.

How does DaemonSet work in Kubernetes?

A DaemonSet ensures that all (or some) Nodes run a copy of a Pod. As nodes are added to the cluster, Pods are added to them. As nodes are removed from the cluster, those Pods are garbage collected. Deleting a DaemonSet will clean up the Pods it created.


3 Answers

In case you don't wanna delete the daemonset, one possible work-around is to use temporary nodeSelector with any non-existing label, for example:

kubectl -n <namespace> patch daemonset <name-of-daemon-set> -p '{"spec": {"template": {"spec": {"nodeSelector": {"non-existing": "true"}}}}}' 

This will scale the daemonset down.

And here is the patch to remove temporary nodeSelector:

kubectl -n <namespace> patch daemonset <name-of-daemon-set> --type json -p='[{"op": "remove", "path": "/spec/template/spec/nodeSelector/non-existing"}]' 

This will scale the daemonset up again.

like image 127
Alex Vorona Avatar answered Sep 22 '22 18:09

Alex Vorona


DaemonSet ensures that every node run a copy of a Pod. So you can't scale down it as Deployment. DaemonSet use DaemonSet Controller and Deployment use Replication Controller for replications. So You can simply delete the DaemonSet.

If you want to backup the exact Daemonset deployment you can use following command and save it somewhere and use it again for later deployement.

kubectl get daemonset <name-of-daemon-set> -n <namespace> -o yaml 
like image 20
Hansika Weerasena Avatar answered Sep 20 '22 18:09

Hansika Weerasena


only as addition to Alex Vorona's answer for scaling to more than 0 nodes:

scale to a single node:

kubectl -n <namespace> patch daemonset <name-of-daemon-set> -p '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "<hostname>"}}}}}'

scale to any number of nodes with some label:

kubectl -n <namespace> label nodes <name-of-node> someLabel=true
kubectl -n <namespace> patch daemonset <name-of-daemon-set> -p '{"spec": {"template": {"spec": {"nodeSelector": {"someLabel": "true"}}}}}'
like image 31
marioneta Avatar answered Sep 21 '22 18:09

marioneta