Kubernetes

Kubectl Force Delete Pod

Using the kubectl commands, removing a pod from a Kubernetes node is a simple operation and does not require any lengthy steps. Whether you need to debug a node, conduct an upgrade, or shrink the size of your cluster, deleting pods is a simple process. However, before deleting a pod, you should go through a set of steps to ensure that the application runs smoothly.

In this article, we will discuss those steps. Keep in mind, however, that rushing this process may result in errors and application downtime. Now, let’s discuss the topic in detail.

How to Delete Kubernetes Pods the Easiest Way? 

You must first list the pods in order to delete the post in the most efficient manner. You can do so if you use the get pods command which is given below.

$ kubectl get pods -o wide

Here, you can see that the get pods command listed down the pods with details.

Now, if we delete the “pod-three” Pod. Type the following command:

$ Kubectl delete pod pod-three

The above command in which we have specified the name of the pod (pod-three) deleted the pod successfully.

$ kubectl get pods -o wide

By listing all of the pods, you can confirm that the one in question has been deleted.

As you can verify in the picture affixed above, there is no ‘pod-three.’

How to Force Delete Kubernetes Pods?

Why force pod deletion is required? After a timeout, the Pod can become stuck in a terminating/unknown state on an unreachable Node. These statuses can also occur when a user tries to gracefully delete a Pod from an inaccessible Node. In some situations, you are allowed to forcefully remove/delete the Pod.

Whether or not a force deletion is effective in destroying a Pod, the name will be removed from the apiserver immediately. This would enable the StatefulSet controller to establish a replacement Pod with the same identity; this could lead in the duplication of an already-running Pod, which, if said Pod can still interact with other StatefulSet members, would violate the at only one semantics that StatefulSet was made to ensure.

Manual force deletion must be used carefully because it may break at least one of the semantics of StatefulSet. StatefulSets can be used to execute distributed and clustered applications that require a consistent network identity and persistent storage.

The configuration of these apps is frequently based on an ensemble comprising a predetermined number of members with fixed identities. Multiple members sharing the same identities might be dangerous and can end up in data loss.

When you delete a StatefulSet pod by force, you ensure that it will never communicate with other Pods in the StatefulSet anymore, and that its identity may be securely freed up for a replacement.

Follow these procedures to forcefully destroy a Pod using kubectl >= 1.5:

$ kubectl delete pods name-of-pod --grace-period=0 --force

If you are using kubectl >= 1.4, you can skip the —force argument and instead use:

$ kubectl delete pods name-of-pod --grace-period=0

Now, using the above way, delete the pod “pod-two”:

$ kubectl delete pod pod-two --force --grace-period=0 --namespace=default

Here is the result of the above command when executed.

Please note that force pod deletions do not rely on kubelet confirmation that the pod has been terminated.

If the pod remains in the Unknown status after running these commands, use the following command to remove it from the cluster:

$ kubectl patch pod pod-two -p '{"metadata":{"finalizers":null}}'

Check to see if the pod has been removed.

How to Remove Pods from a Node All at Once?

If your node is having any non-essential pods or stateful pods, you can make use of the kubectl drain command. This step will remove all pods from the node. Before progressing, double-check the identity of the node you’re deleting and make sure that the pods on a certain node may be safely terminated. The task will be accomplished for you with the following instruction.

$ kubectl get nodes

After the get pod command, use the following cited command given below.

Then, try using the following cited instruction to drain all of the pods.

$ kubectl drain

To ensure that no pods are still operating on the node, execute the get pods command again. If you are using NoExecute to run pods, they will still be on the node.

Execute the drain command one more time. But this time the —force flag should be included. This will eliminate all pods from the node. Finally, use the kubectl delete node <nodename> command to delete the node from the relevant cluster.

How to Allow Pods Back onto Nodes?

Once you have finished performing maintenance on a node, use the kubectl uncordon command to re-enable scheduling on that node. Then, as pods become available for scheduling, they will reappear on that node.

$ kubectl uncordon docker-desktop

If you have a newly uncordoned node or a new node within your cluster, balancing particular pods may be a problem. Although it is best to let the Kubernetes scheduler allocate pods as needed when you alter your Deployment/StatefulSet, you may force the process by removing pods and scheduling them elsewhere employing some of the earlier phases.

Conclusion

You might need to delete a single pod or multiple pods from one of your nodes when executing your Kubernetes cluster. There is a possibility that you might need to troubleshoot node difficulties, upgrade a specific node, or scale down your cluster. Although deleting any pod or pods from a node is not very difficult, there are some tips and techniques that you should follow to ensure that your application/program is not disrupted. This is what we have spoken about and expanded on in this article. Before executing any instructions, make sure you have thoroughly read and comprehend each step to avoid making mistakes that could result in downtime.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content