Prevent Pod From Endless Restart

When a pod in error state (crashloopbackoff), kubernetes would restart the pod. If you try to exec into the pod to check the log or debug, the following error message appears:

unable to upgrade connection: container not found ("")

Because the old pod has been killed and you cannot exec into it anymore. So how can we prevent the pod from endless restart?

Just add a command to the deployment yaml to override the default command by the container image. Make the pod never finished by sleep infinity or tail -f /dev/null:

command: ["sleep"]
args: ["infinity"]

or

command: ["tail"]
args: ["-f", "/dev/null"]

Add them to spec.template.spec.containers like this:

...
spec:
  template:
    spec:
      containers:
        - name: xxx
        command: ["sleep"]
        args: ["infinity"]
...

Remove the liveness/readiness section from the yaml to make the deployment status green if necessary.

You can exec into the pod to debug as you want.

Reference: * https://stackoverflow.com/questions/31870222/how-can-i-keep-a-container-running-on-kubernetes * https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/