Chat Zalo Chat Messenger Phone Number Đăng nhập
How to Delete Pods in Kubernetes [Quick K8s Tips] - Linux Handbook

How to Delete Pods in Kubernetes [Quick K8s Tips] – Linux Handbook

While working in the Kubernetes cluster environment, there will be times when you will encounter a situation where you need to remove pods from one of your worker nodes.

You may need to debug issues with the node itself, upgrade the node, or simply scale down the cluster.

The action of deleting a Kubernetes pod is very simple with kubectl delete pod command:kubectl delete pod pod-name

However, there are specific steps you need to take to minimize disruption to your application. I will explain it in detail in this article.

Remove

Kubernetes

pods successfully

First, list all

pods:[email protected]:~# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES my-dep-557548758d-d2pmd 1/1 Running 0 2d23h 172.16.213.194 kworker-rj2 <none> <none> my-dep-557548758d-gprnr 1/1 Running 0 2d23h 172.16.213.49 kworker-rj1 <none> <none> pod-delete-demo 1/1 Running 0 4s 172.16.213.196 kworker-rj2 <none> <none>

Now suppose you want to

delete the Pod named “pod-delete-demo”, run the following command:[email protected]:~# kubectl delete pod pod-delete-demo pod “pod-delete-demo” deleted

Confirm that the Pod in question is deleted by listing all

pods:[email protected]:~# kubectl get pods -o wide NAME READY STATE REBOOT AGE IP NODE NODE NOMINATED PREPARATION GATES my-DEP-557548758D-D2PMD 1/1 Running 0 2D23H 172.16.213.194 Kworker-RJ2 <none> <none> my-dep-557548758d-gprnr 1/1 Running 0 2d23h 172.16.213.49 kworker-rj1 <none> <none>

You can see that there is no pod pod-delete-demo that is running.

<img src=”https://linuxhandbook.com/content/images/2022/10/lf-foundation-kubernetes.webp” alt=”Linux Foundation Kubernetes Certification Discount” />Force remove

Kubernetes

podsWhy do you need to force pod removal?

Sometimes, the Pod gets stuck in the terminated/unknown on an unreachable node after a timeout.

Pods can also enter these states when the user successfully attempts to delete a pod on an unreachable node

.

In those scenarios, you can remove the Pod forcefully.

If you

want to remove a Pod by

force using kubectl version >= 1.5, do the following:kubectl delete pods pod_name -grace-period=0 -force

If you are using any version of kubectl <= 1.4, you should omit the -force option and use:kubectl

delete pods pod_name -grace-period=0

Now let’s remove the “pod-delete-demo” pod using the above method:

[email protected]:~# kubectl delete pod pod-delete-demo -force -grace-period=0 -namespace=default warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource can continue to run on the cluster indefinitely. pod “pod-delete-demo” force delete

NOTE: Force pod deletions does not wait for confirmation from the kubelet that

the pod has been terminated.

If even after these commands the pod is stuck in unknown state, use the following command to

delete the cluster pod:kubectl patch pod pod-delete-demo -p ‘{“metadata”:{“finalizers”:null}}’

Confirm that the pod has been deleted

:[email protected]:~# kubectl get pods -o wide NAME READY STATE REBOOT AGE IP NODE NODE NOMINATED PREPARATION DOORS my-dep-557548758d-d2pmd 1/1 Running 0 2d23h 172.16.213.194 kworker-rj2 <none> <none> my-dep-557548758d-gprnr 1/1 Run 0 2d23h 172.16.213.49 kworker-rj1 <none> <none> I

hope you find this Kubernetes tip useful. Stay subscribed for more DevOps tips and tutorials.

Contact US