How To Delete All Kubernetes PODS? - Jhooq
Maybe your like
In kubernetes cluster we always creat and delete kubernetes PODS. PODS lifecycle is very short and it is very often that kubernetes creat a replica of a POD to provide maximum availability across the kubernetes cluster.
But considering the need, a developer might need to delete POD manually because of POD getting stuck in terminating status and kubernetes is not able to terminate the POD.
There are many alternate ways to delete the POD. Here I have composed some of my favorite ways to delete the POD inside the kubernetes cluster -
- Delete all PODS in a single namespace - e.g. kubectl delete --all pods --namespace=foo
- Delete deployment in namespace which will delete PODS - e.g. kubectl delete deployment <deployment_name>
- Delete all namespaces and every object in every namespace -e.g. kubectl delete all --all --all-namespaces
- Delete everything except kubespace
- Adding sed to delete POD
- Delete the pod stuck in terminating state
1. Delete all PODS in a single namespace - e.g. kubectl delete --all pods --namespace=foo
In your Kubernetes cluster, there can be more than one namespace and each POD belongs to some specific namespace, so the first approach which I would take is to delete all the POD in a single namespace.
But before deleting the POD you must know the name of the namespace in which your POD exists along with its status.
Check POD status
1kubectl describe pod pod_name --namespace fooDelete POD
1kubectl delete --all pods --namespace=foo2. Delete deployment in namespace which will delete PODS - e.g. kubectl delete deployment <deployment_name>
The second approach which I would take is by deleting all the deployment which belongs to POD, this approach is indirect because in a typical Kubernetes POD we do multiple deployments of the docker container.
So if you delete all the deployment in a POD then there will be no active deployment and the POD will be empty and the empty POD does not exist in a Kubernetes cluster.
Delete all deployment in a single namespace
1kubectl delete --all deployments --namespace=fooDelete all the deployment across all the namespaces
1kubectl delete --all deploymentsRunning the above command will delete all the active deployment running inside the POD which will eventually lead you to delete the desired POD inside the Kubernetes cluster.
(Note * - Always be careful about running the delete deployment command without any namespaces because it may delete all of your deployment by accident.)
3. Delete all namespaces and every object in every namespace -e.g. kubectl delete all --all --all-namespaces
The third approach for deleting POD would be to use the following delete command (But be careful and read the notes before running this command) -
1kubectl delete all --all --all-namespacesNotes - There are two all flags in the command but each flag has its importance -
- first all - It is going to delete all the resources such as pods, replicasets, deployments...
- second all - Select all the resources of a selected kind
The above command does not delete clusterrolebindings, clusterroles, configmaps, rolebindings, roles, secrets
4. Delete everything except kubespace
The fourth way would be to use a bit of scripting and for a loop. The following command will loop over all the namespace present in the Kubernetes cluster and delete each namespace one by one.
Deleting of each namespace will eventually delete the POD running inside the namespace -
1for each in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}" | grep -v kube-system); 2do 3 kubectl delete ns $each 4done5. Adding sed to delete POD
The Fifth way would be to use the sed to delete the POD. The following command will delete all the namespaces and all the PODS running inside those namespaces.
1kubectl get pods --no-headers=true --all-namespaces |sed -r 's/(\S+)\s+(\S+).*/kubectl --namespace \1 delete pod \2/e'6. Delete the pod stuck in terminating state
If the previous steps solutions do not work for you then I would recommend deleting all the PODS which are stuck in the terminating state. This can happen sometimes and in such cases, you need to delete them forcefully
Here is the command -
1kubectl delete pods --all --grace-period=0 --force --namespace namespace7. Conclusion
If you need to delete the POD many times inside the Kubernetes cluster then it is a not good practice to follow. You should fix the underlying issues which can be hiding somewhere inside the docker container. But in many cases, there can be a problem associated with the way you have set up your Kubernetes cluster and in such cases, you have to delete the POD manually.
The above-mentioned command for deleting the POD always comes in handy when you need to delete the POD so that you can work with a clean Kubernetes cluster. Hope this post helps you to troubleshoot the issue with the deletion of the POD.
You can read more on developer forum such as Stackoverflow
Learn more On Kubernetes -
- Setup kubernetes on Ubuntu
- Setup Kubernetes on CentOs
- Setup HA Kubernetes Cluster with Kubespray
- Setup HA Kubernetes with Minikube
- Setup Kubernetes Dashboard for local kubernetes cluster
- Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
- How to use Persistent Volume and Persistent Volume Claims in Kubernetes
- Deploy Spring Boot Microservice on local Kubernetes cluster
- Deploy Spring Boot Microservice on Cloud Platform(GCP)
- Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
- How to setup kubernetes jenkins pipeline on AWS?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- How to fix kubernetes pods getting recreated?
- How to delete all kubernetes PODS?
- How to use Kubernetes secrets?
- Share kubernetes secrets between namespaces?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Delete Kubernetes POD stuck in terminating state?
Tag » How To Stop All Kubernetes Pods
-
How To Delete Pods From A Kubernetes Node - Blue Matador
-
Stopping And Starting A Kubernetes Cluster And Pods - IBM
-
Kubectl Cheat Sheet | Kubernetes
-
Kubectl Stop - Kubernetes
-
Command To Delete All Pods In All Kubernetes Namespaces
-
Kubectl Stop Pod - Linux Hint
-
Kubectl Stop All Pods Code Example
-
Kubectl Stop All Pods - Installvirtual
-
Quick Tips: Deleting Kubernetes Pods | By KubeSphere - ITNEXT
-
Forcefully Delete Kubernetes Pod - Platform9 Knowledge Base
-
Simple Steps To Delete A Pod From A Kubernetes Node - ContainIQ
-
How To Create, View, And Destroy A Pod In Kubernetes - Fairwinds
-
How Can I Delete A Pod In Kubernetes? - Quora