How to Keep a Container Running on Kubernetes
Kubernetes containers are designed to be ephemeral and usually run a specific process that is eventually completed.
However, you might want to keep a container running to inspect its state and logs while troubleshooting an issue. Monitoring agents and web servers are also designed to run continuously as background services.
Let me show you various ways to keep a container running in Kubernetes.
Method 1: Running a Background Process
This is one of the most common and simplest ways to keep a Kubernetes container running. It involves running a process in the background which doesn't exit.
Here's an example Kubernetes deployment that uses a background process to keep the container running:
apiVersion: v1
kind: Pod
spec:
containers:
- name: nginx
image: nginx:latest
command: ["tail", "-f", "/dev/null"]
In this example, I used the tail
command with -f
parameter on /dev/null
file that discards everything written to it, thus entering an infinite loop that keeps the container running.
You can also use something like while true; do :; done
or any scripts that runs a never ending loop.
Method 2: Using the sleep-infinity command within a Kubernetes Pod
Using the sleep infinity
command within a Kubernetes Pod is another common way to keep a container running. This command works by instructing the container to pause its execution indefinitely. It doesn't perform any other tasks and essentially just waits forever.
Since Kubernetes is designed to keep Pods running. If a Pod's main process terminates, Kubernetes normally restarts it. However, since the sleep infinity
command never ends, the Pod continues to run forever.
To use sleep-inifinity command you have to edit the pod's YAML configuration and add a debugging container:
apiVersion: v1
kind: Pod
metadata:
name: sleep-infinity-pod
spec:
containers:
- name: alpine
image: alpine:latest
command: ["sleep", "infinity"]
Next you have to apply changes and attach a shell:
kubectl apply -f <your_pod_yaml>
kubectl exec -it <pod_name> -c alpine -- bash
Though sleep inifinity
is lightweight and does not consume a lot of resources but in a complex enviornment, it is considered a better practice to monitor the debugging containers to ensure they don't have a impact on the overall performance.
Method 3: Using a Process Supervisor to keep Kubernetes container running
A process supervisor is a lightweight program that runs inside the container and oversees the main process of the application. It is used to monitor the health of this process and take corrective action such as restarting the process and ensuring that the container remains running even if the main process crashes or becomes unresponsive.
Kubernetes itself has built-in mechanisms to restart containers when they fail. However, if your container runs multiple processes, Kubernetes might not be aware of the individual process failures. A process supervisor monitors each process independently and restarts them as needed.
Also, in some cases, processes can become zombies when they terminate but are not properly cleaned up. Process supervisors can prevent this by ensuring that child processes are correctly managed.
To use a process supervisor like Tini you have to add the process supervisor to your container image during the image build process and run it in your container (PID 1).
apiVersion: v1
kind: Pod
metadata:
name: tini-pod
spec:
containers:
- name: node-app
image: node:18-alpine
command: ["/tini", "--"]
args: ["node", "server.js"]
In this configuration, Tini is specified as the entry point command for the container. It will then launch the Node.js application and monitor its execution. If the Node.js process fails, Tini will automatically restart it, thus ensuring that Kubernetes container runs indefinitely.
Conclusion
Here, I showed you three ways to keep a container running in Kubernetes. When the situation demands, you can use the method that suits you best. I hope you find this Kubernetes tip helpful.