I have a pod test-1495806908-xn5jn
with 2 containers. I'd like to restart one of them called container-test
. Is it possible to restart a single container within a pod and how? If not, how do I restart the pod?
The pod was created using a deployment.yaml
with:
kubectl create -f deployment.yaml
There are cases when you want to restart a specific container instead of deleting the pod and letting Kubernetes recreate it.
Doing a
kubectl exec POD_NAME -c CONTAINER_NAME /sbin/killall5
worked for me.(I changed the command from
reboot
to/sbin/killall5
based on the below recommendations.)Killing the process specified in the Dockerfile's
CMD
/ENTRYPOINT
works for me. (The container restarts automatically)Rebooting was not allowed in my container, so I had to use this workaround.
Both pod and container are ephemeral, try to use the following command to stop the specific container and the k8s cluster will restart a new container.
This will send a
SIGTERM
signal to process 1, which is the main process running in the container. All other processes will be children of process 1, and will be terminated after process 1 exits. See the kill manpage for other signals you can send.The whole reason for having kubernetes is so it manages the containers for you so you don't have to care so much about the lifecyle of the containers in the pod.
Since you have a
deployment
setup that usesreplica set
. You can delete the pod usingkubectl delete pod test-1495806908-xn5jn
and kubernetes will manage the creation of a new pod with the 2 containers without any downtime. Trying to manually restart single containers in pods negates the whole benefits of kubernetes.Assuming the container is run as root which is not recommended.
In my case when I changed the application config, I had to reboot the container which was used in a sidecar pattern, I would kill the PID for the spring boot application which is owned by the docker user.
We use a pretty convenient command line to force re-deployment of fresh images on integration pod.
We noticed that our alpine containers all run their "sustaining" command on PID 5. Therefore, sending it a
SIGTERM
signal takes the container down.imagePullPolicy
being set toAlways
has the kubelet re-pull the latest image when it brings the container back.