How to delete Pods or restart containers which are targets of kubectl attach/exec?

8/29/2019

I'd like to automatically restart any container which is the target of kubectl exec/kubectl attach after the session is closed. Is this currently possible?

-- Dharma Bellamkonda
kubernetes

1 Answer

9/6/2019

In K8s, Pod (not container) is the smallest unit in terms of operation.

So the workaround is to restart entire pod after the session.

Simple command concatenation with logical AND will work for your task, i.e.

kubectl exec -it webserver-1 bash && kubectl delete pod webserver-1

Once you exit the pod session second part of the command will be executed - removing a pod and a scheduler will spin up a new pod for you (if that was a part of replica set).

-- A_Suh
Source: StackOverflow