In this article, We will learn to keep a pod/container running for as long as we need
As a general rule container will perform the task assigned to them and exit on completion (either success or failure). To keep it running, we can use the “sleep” command to run for some specific time or open a shell session till the session complete
In Kubernetes, Containers are running inside a pod, So whenever the container exited on completion and its corresponding pod will be exited and we will discuss various other ways to achieve
How do I keep my container always running in Docker?
In the below docker command, Container will be created and exited as we did not specify any work or task
docker run -itd debian
Modified docker command, Now, the Container will be created and wait for 300 seconds before exiting
docker run -d debian sleep 300
So, Keeping a container live forever is not recommended and is an intended approach, But ideally, To troubleshoot an issue, we will keep the container running for sometime
How do I keep my container always running in Kubernetes?
In Kubernetes, We can keep the pod alive in 4 different ways
Create a shell session inside the container
Creating a shell session inside a container will keep the container alive till we close the session
kubectl run -it --rm test_pod --image=busybox --restart=Never -- sh
We can use the sleep command to keep the container awake for some seconds before it exits,
Example pod definition below
apiVersion: v1 kind: Pod metadata: name: busybox labels: app: busybox spec: containers: - name: busybox image: busybox command: ["/bin/sh", "sleep 1000"]
Infinite loop is basically a hack to keep your container alive forever, until it gets killed, Which is not recommended 🙂
apiVersion: v1 kind: Pod metadata: name: busybox labels: app: busybox spec: containers: - name: busybox image: busybox command: ["/bin/sh", "while :; do echo '.'; sleep 5 ; done"]
Create Your Own docker Image and use it to create a container in a pod
You can create your own docker image and provide commands like sleep or loop in the ENTRYPOINT, So that, Once the container is created it will start executing the entry point command and will be alive for the specified time.
FROM alpine ENTRYPOINT ["sleep"] CMD ["1000"]
The above example will run a sleep 1000 on starting of the container
How to monitor a pod that’s always running?
A liveness probe is a mechanism used in Kuberbeos to determine the container’s current status, it returns a success or failure status, indicating whether the container is still alive or not, and can perform some action based on the status or we can use a “kubectl” command to check the status manually
Liveness probe to monitor contianer
Example YAML file to create a liveness probe
apiVersion: apps/v1 kind: Deployment metadata: name: Web-App spec: replicas: 3 selector: matchLabels: app: Web-App template: metadata: labels: app: Web-App spec: containers: - name: Web-App-container image: Web-App-image ports: - containerPort: 80 livenessProbe: httpGet: path: /health port: 80 initialDelaySeconds: 30 periodSeconds: 10
In the above example, When a container goes down, the liveness probe will continuously fail to determine the health of the container. As a result, Kubernetes will take restart the container
Kubectl command to monitor container
We can run the below command to see if the container is in a running state or not
kubectl get pod my-pod
In Summary, We have discussed the various method to keep a pod/container running instead of it getting exited on success or failure, Based on your use case you can choose a way to keep the container active
Click here to know more about Create Your Own Docker Image
Click here to know more about running commands inside a container
Personal experience and tips to pass CKA & CKAD exams
Good Luck with your leaning !!