How can I keep a container running on Kubernetes?
You could use this CMD in your Dockerfile
:
CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"
This will keep your container alive until it is told to stop. Using trap and wait will make your container react immediately to a stop request. Without trap/wait stopping will take a few seconds.
For busybox based images (used in alpine based images) sleep does not know about the infinity argument. This workaround gives you the same immediate response to a docker stop
like in the above example:
CMD exec /bin/sh -c "trap : TERM INT; sleep 9999999999d & wait"
Containers are meant to run to completion. You need to provide your container with a task that will never finish. Something like this should work:
apiVersion: v1
kind: Pod
metadata:
name: ubuntu
spec:
containers:
- name: ubuntu
image: ubuntu:latest
# Just spin & wait forever
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]