Restart container within pod

前端 未结 9 1086
日久生厌
日久生厌 2020-12-04 07:04

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 co

相关标签:
9条回答
  • 2020-12-04 07:25

    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.

    0 讨论(0)
  • 2020-12-04 07:26

    Is it possible to restart a single container

    Not through kubectl, although depending on the setup of your cluster you can "cheat" and docker kill the-sha-goes-here, which will cause kubelet to restart the "failed" container (assuming, of course, the restart policy for the Pod says that is what it should do)

    how do I restart the pod

    That depends on how the Pod was created, but based on the Pod name you provided, it appears to be under the oversight of a ReplicaSet, so you can just kubectl delete pod test-1495806908-xn5jn and kubernetes will create a new one in its place (the new Pod will have a different name, so do not expect kubectl get pods to return test-1495806908-xn5jn ever again)

    0 讨论(0)
  • 2020-12-04 07:26

    All the above answers have mentioned deleting the pod...but if you have many pods of the same service then it would be tedious to delete each one of them...

    Therefore, I propose the following solution, restart:

    • 1) Set scale to zero :

       kubectl scale deployment <<name>> --replicas=0 -n service 
      

      The above command will terminate all your pods with the name <<name>>

    • 2) To start the pod again, set the replicas to more than 0

      kubectl scale deployment <<name>> --replicas=2 -n service
      

      The above command will start your pods again with 2 replicas.

    0 讨论(0)
  • 2020-12-04 07:26

    There was an issue in coredns pod, I deleted such pod by

    kubectl delete pod -n=kube-system coredns-fb8b8dccf-8ggcf
    

    Its pod will restart automatically.

    0 讨论(0)
  • 2020-12-04 07:37
    kubectl exec -it POD_NAME -c CONTAINER_NAME bash - then kill 1
    

    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.

    0 讨论(0)
  • 2020-12-04 07:40

    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.

    kubectl exec -it [POD_NAME] -c [CONTAINER_NAME] -- /bin/sh -c "kill 1"
    

    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.

    0 讨论(0)
提交回复
热议问题