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:

1
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:

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

or

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

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

1
2
3
4
5
6
7
8
9
...
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: