Kubernetes – How to Restart or Reset Custom Namespace Pods

k3skubernetes

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 ?

Best Answer

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.

Related Topic