0

I have a k3s cluster with following 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

How can I reset (stop and start the pods again) the pods either with command (kubectl maybe) or any script?

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
solveit
  • 869
  • 2
  • 12
  • 32
  • Why do you want to do that? – Software Engineer Aug 09 '21 at 07:50
  • to get to the starting/basic config of pods like coredns, metric-server etc. I want to simply reset the cluster which means stop and start the containers in one go. is there any command to do that ? – solveit Aug 09 '21 at 08:03
  • Not really, you'd need to write a script. – Software Engineer Aug 09 '21 at 08:11
  • For any of those pods you can just `kubectl delete` them and they will automatically be restarted (I think all of them are managed by a Deployment resource). You could do this in a single command pipeline with something like `kubectl get pods | ...filter to get just the name you want... | xargs kubectl delete pod` – larsks Aug 09 '21 at 09:18
  • 1
    To add to @larsks comment - you can get just the names of the pods with `kubectl get pods -o=jsonpath='{.items[*].metadata.name}'` –  Aug 09 '21 at 09:37
  • @Pawel-:: kubectl get pods -o=jsonpath='{.items[*].metadata.name}' command is not giving any output – solveit Aug 09 '21 at 11:39
  • @larsks-:: ...filter to get just the name you want... : how to filter the pods ? do I need to write any grep command for full podname eg: kubectl get pods | grep "coredns-7448499f4d-k2zt4" | xargs kubectl delete pod – solveit Aug 09 '21 at 11:41
  • @solveit, seems you don't have any pods in `default` namespace. To display pods from different namespace add `-n` flag to the command e.g.: `kubectl get pods -n kube-system -o=jsonpath='{.items[*].metadata.name}'`. grep-ing exact name of the pod defeats the purpose of piping and `xargs` –  Aug 09 '21 at 13:11
  • @PawełGrondal-:: By this command all pods of kube-system are coming in output. So what is the point ? I want to restart the cluster. Command " kubectl get pods -n kube-system -o=jsonpath='{.items[*].metadata.name}' | xargs kubectl delete pod" is giving following error for all 5 pods, 5 times: Error from server (NotFound): pods "coredns-7448499f4d-k2zt4" not found – solveit Aug 09 '21 at 14:23

2 Answers2

0

To reset a pod, you can just delete it. If it's managed by deployment (pods in your question should be), they should be recreated automatically.

kubectl delete pod <pod-name> <pod2-name> ... -n <namespace>

If the pods you want to reset, have common label, you can filter them with --selector flag

kubectl delete pods --selector=<label-name>=<label-value> -n <namespace>

However, if you changed the deployments somehow, you will need to apply the unmodified manifest.

kubectl apply -f <yaml-file>
  • @p10I-:: I dont want to write each podname manually. and for restart I am looking for one single command or script. which can stop and start all pods again – solveit Aug 10 '21 at 12:46
  • I updated my answer. I don't believe there is any other way to do this other than writing a custom script at this point. –  Aug 11 '21 at 06:09
0

Warning: - This will reset your whole cluster and delete all running data. This is not the exact answer but best answer. take 1 min only. Just uninstall by running below command

 sudo /usr/local/bin/k3s-uninstall.sh

Then install a fresh cluster with below command

curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable=traefik" sh -

Then export var using below command

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

Also it may complain about some k3s config file access so

sudo chmod 444 /etc/rancher/k3s/k3s.yaml
Sumer
  • 2,687
  • 24
  • 24