1. Overview

In this tutorial, we’ll delve into various methods and strategies for stopping or pausing pods in a Kubernetes cluster. This helps us control traffic and the overall availability of the service.

2. Stopping a Pod

A pod in Kubernetes is a single isolate unit that solves a specific purpose. We can have a single or a group of related containers inside a pod. Stopping a pod is a deliberate action of terminating pod execution and all containers running inside the pod.

In general, there are two types of pod termination, graceful termination and forceful termination. Let’s discuss both approaches with the help of an example.

2.1. Using the kubectl delete Command

Using the kubectl delete command, we can delete various Kubernetes resources, including pods. It allows us to gracefully terminate and remove Kubernetes resources from the cluster.

Let’s say we have a Kubernetes cluster with a pod named my-pod. In order to delete this pod, we can use the below kubectl delete command:

$ kubectl delete pod my-pod
pod "my-pod" deleted

When we execute the above command, Kubernetes will initiate the termination process for the my-pod pod. It sends a termination signal to the primary process running inside the pod, allowing the processes to perform cleanup tasks if necessary before terminating the pod.

2.2. Forceful Termination

By default, Kubernetes will wait for the pod to gracefully terminate. If the pod failed to terminate due to some reason, we can forcefully delete the pod using the –force flag:

$ kubectl delete pod my-pod --force
pod "my-pod" deleted (force)

Note that we can use the kubectl delete command with different resource types (e.g., Deployments, Services, ConfigMaps) to delete other Kubernetes resources in the cluster. Here we used it to delete the pods.

2.3. Scaling Down the Deployment

Deleting pods using kubectl delete command is helpful when the pods are not part of any deployment. In general, pods are launched and managed by deployments in Kubernetes. Also, the pod count associated with a deployment in a production environment is very large. Hence deleting pods one by one using the kubectl delete command is not a good option.

We can scale down the deployment to zero replicas using the kubectl scale command, effectively stopping the pods:

$ kubectl scale deployment my-deployment --replicas=0
deployment.apps/my-deployment scaled

The output message indicates that the deployment named my-deployment has been successfully scaled down to zero replicas.

Using the kubectl get pods command, we can verify that the deployment no longer has any running pods:

$ kubectl get pods

This can be useful when we need to pause or temporarily halt traffic to a specific application or service. We can later scale the deployment back up to resume its operation when needed.

3. Pausing the Pod

We can pause a Docker container using the docker pause command. This will suspend all the processes inside that Docker container by sending a SIGSTOP signal.

In Kubernetes, things work differently. There is no native concept of “pausing” a pod to temporarily halt its execution and then later resuming it exactly where it left off. Pods are isolated units that are designed to be either running or terminated.

However, we can achieve a similar pausing effect by using various strategies in Kubernetes. Let’s now discuss them.

3.1. Using Custom Application Logic

By adding a custom logic within the application code, we can easily achieve the pausing behaviour. In addition, this method offers fine-grained control over the pod’s behaviour.

To illustrate, we can introduce a configuration flag, say PROCESSING_ENABLED, that can be toggled on and off. When PROCESSING_ENABLED is set to true, the application performs its processing tasks. When it’s set to false, the application’s custom logic will gracefully suspend the background processes. This way, we have control over when data processing occurs.

3.2. Update Pod Selector in Service

Ideally, we attach a label to the pod and use the same label in the Service. When the traffic hits the Service, the service will look for all the pods matching the label and will route the traffic to one of the pods. In order to pause this flow, we can update the pod selector label in the Kubernetes Service.

Let’s understand this with the help of an example. Consider we have a microservice application containing multiple pods. These pods are responsible for handling different tasks like data processing, reporting, and user management. Now, suppose we want to temporarily pause data processing pods during a maintenance window to ensure data integrity.

We can simply update the pod selector in the service to any random value that does not match to any pod label in the cluster. This way, no traffic will be redirected to the data processing pods. After the maintenance is complete, we can update the pod selector to an appropriate value and hence, the traffic resumes.

This approach offers the advantage of not maintaining any custom logic inside the application code.

4. Conclusion

In this article, we learned to stop a pod in Kubernetes using the kubectl delete command. Further, we learned to stop all the deployment pods by scaling down the deployment. At last, we discussed various strategies to pause traffic to pods in Kubernetes.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.