0

I have a k3s cluster with kube-system pods and my application's (xyz-system namespace) pods:

kube-system   pod/calico-node-xxxx                          
kube-system   pod/calico-kube-controllers-xxxxxx   
kube-system   pod/metrics-server-xxxxx
kube-system   pod/local-path-provisioner-xxxxx
kube-system   pod/coredns-xxxxx
xyz-system    pod/my-app
xyz-system    pod/my-app-mqtt

I want to reset/restart all of these pods (kube-system + xyz-system) in one command (or it can be two commands for two namespace but without deployment name) without giving deployment names because in future I can have more deployments created, so it will be difficult to provide many deployment names manually.

Debugging:
With command kubectl -n kube-system rollout restart daemonsets,deployments as mentioned in link I am able to restart the kube-system pods. But when I modify this command with xyz-namespace : kubectl -n xyz-system rollout restart deployments the respective pods are not restarting when I monitor watch kubectl get all -A they remain as it is in Running state.

Can someone let me know how to achieve this ?

solveit
  • 255
  • 2
  • 11
  • 1
    But why would you want to restart them? it is against the principles k8s is built for – Jonas Aug 16 '21 at 15:26
  • @Jonas: I want to understand more about what you said that "its against the principle....". Can you provide any k3s/k8s official doc from where it can be evident ? – solveit Aug 17 '21 at 04:29
  • 1
    Pods are disposable, e.g. immutable architecture - you don't mutate, but throw away (delete) and instead create new pods. It is not intended to change anything about a Pod. See e.g. https://kubernetes.io/docs/concepts/workloads/pods/#working-with-pods – Jonas Aug 17 '21 at 05:48

1 Answers1

4

In two commands:

kubectl delete pod -n kube-system --all
kubectl delete pod -n xyz-system --all

In "one":

kubectl get pods -A | awk 'NR>1{print $1" "$2}' \
    | while read ns pod; do \
        kubectl delete -n $ns pod $pod; done

Or:

kubectl get ns | awk 'NR>1{print $1}' \
    | while read ns; do \
         kubectl delete pod -n $ns --all; done

Though I second @jonas comment: sounds like a weird question, with no real-life use cases that I know of.

SYN
  • 1,751
  • 8
  • 14
  • 2
    Or you could issue `kubectl delete pod --all --all-namespaces`, but this will restart (or delete if not in deployment) absolutely everything. – p10l Aug 17 '21 at 07:20