In the world of Kubernetes, pods are the smallest and simplest unit in the Kubernetes object model. They represent a single instance of a running process in a cluster. However, pods can be deleted for various reasons, such as resource constraints, pod failures, or human error.
When a pod is deleted, Kubernetes does not automatically restart it, and it remains in a terminated state. In this article, we will discuss how to make sure that a pod that is deleted is restarted after a specified time.
First, let us understand what happens when a pod is deleted. When a pod is deleted, Kubernetes removes it from the node, and the pod enters a terminated state. Once the pod is in a terminated state, it will not be restarted automatically. To restart a pod, you need to configure Kubernetes to do so.
There are several ways to configure Kubernetes to restart a deleted pod. One way is to use a Kubernetes feature called liveness probes. Liveness probes are used to determine if an application inside a pod is still running. If the liveness probe fails, Kubernetes considers the pod to be unhealthy and will automatically restart it.
Let's walk through the steps to configure a liveness probe for a pod.
Step 1: Create a pod
To create a pod, you can use the following YAML file:
- name: my-container
In this YAML file, we are creating a pod named
my-pod with a single container named
my-container. We are using the
nginx image as our container image. We have also added a liveness probe to the container.
Step 2: Define the liveness probe
In the YAML file, we defined a liveness probe that checks if the
/nginx_status URL is reachable on port
initialDelaySeconds field specifies the number of seconds after the container has started before the liveness probe is initiated. The
periodSeconds field specifies how often the liveness probe should be executed.
Step 3: Apply the YAML file
To create the pod, run the following command:
kubectl apply -f pod.yaml
This command will create the pod as defined in the YAML file.
Step 4: Delete the pod
To delete the pod, run the following command:
kubectl delete pod my-pod
This command will delete the pod named
Step 5: Check if the pod is restarted
To check if the pod is restarted, run the following command:
kubectl get pods
This command will list all the pods in the cluster. You should see a new pod with a different name than the original pod. This new pod is created by Kubernetes to replace the deleted pod.
So, configuring liveness probes is an excellent way to ensure that Kubernetes restarts deleted pods. By using liveness probes, you can be confident that your application is always running, and any issues are quickly resolved.
Related Searches and Questions asked:
That's it for this post. Keep practicing and have fun. Leave your comments if any.