How to kill pods on Kubernetes local setup
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Kubernetes is an open-source container orchestration platform that automates deploying, scaling, and managing containerized applications. If you're running Kubernetes on a local setup, there may be instances where you need to manually kill or delete pods. This process can be necessary for various reasons, such as testing resilience, flushing out errors, or simulating failures. Here is a detailed guide on how to kill pods on Kubernetes local setup.
Understanding Pods
In Kubernetes, a pod is the smallest and simplest unit that you can create or deploy. Pods represent a single instance of a running process in your cluster. They encapsulate one or more containers, storage resources, a unique network IP, and options about how the container(s) should run.
Use-cases for Killing Pods
- Testing Resilience: Simulate failures to see how the application responds.
- Resource Reallocation: Remove non-critical pods to free up resources.
- Debugging: Test how the system behaves when certain pods are killed.
- Pod Recycling: Force pods to restart to refresh their state or configuration.
Kubernetes Setup
Before you start killing pods, make sure your Kubernetes environment is set up correctly. A popular local Kubernetes setup is through Minikube or Docker Desktop, both offering a convenient local testbed for development purposes.
Commands to Kill Pods
You can kill pods using different methods. kubectl, the command-line tool for Kubernetes, provides all necessary commands. Here’s how you can kill pods effectively:
Deleting Pods
The simplest way to kill a pod is by deleting it. Here is a basic command:
Replace <pod-name> with the actual name of the pod you wish to delete. The kubectl delete pod command will terminate the pod, and its associated containers will stop running.
Force Deletion
Sometimes, pods may not terminate gracefully. In such cases, you can forcefully delete a pod:
--grace-period=0: This argument sets the grace period to zero seconds, meaning it doesn't wait for containers to shut down gracefully.--force: This argument forces the deletion of the pod without pruning its containers or volumes first.
Killing Pods by Label
To delete multiple pods that share a common label, you can execute:
Evicting Pods
In some scenarios, you might want to evict pods in a way that respects pod disruption budgets and affinity rules. You can do this by:
- Manually simulating eviction using
kubectl drain. - Using scripts or tools that honor evictions.
Monitoring Pods
After killing pods, it is crucial to monitor the state of your cluster and verify that new pods are rescheduled correctly. Use the following command to get real-time updates on pods:
Key Points Summary
| Action | Command | Description |
| Delete a single pod | kubectl delete pod <pod-name> | Terminates the specified pod |
| Force delete a pod | kubectl delete pod <pod-name> --grace-period=0 --force | Forcefully removes the pod without waiting |
| Delete pods by label | kubectl delete pod -l <label-key>=<label-value> | Deletes all pods with the specified label |
| Monitor pods | kubectl get pods --watch | Continuously watches the state of pods |
Additional Considerations
- Pod Disruption Budgets (PDB): Ensure that your actions don't violate any PDBs that may affect availability.
- Pod Ingress Policies: Consider any ingress policies that might automatically recreate pods.
- Resource Allocation: Killing pods could free up cluster resources, altering the resource balance of the cluster.
Killing pods is a common administrative task and having a solid understanding of command syntax and options can be immensely helpful. Whether for testing, debugging, or recovering resources, Kubernetes provides flexible commands and options to manage your pods effectively.
Related reading
- How to know a Pod's own IP address from inside a container in the Pod?
- How to know more details about a previous rollout revision using kubectl?
- How to label Kubernetes node?
- How to let calico use K8s etcd?
- How to kill tensorboard with Tensorflow2 jupyter, Win
- How to know if a point-to-point network is synchronous?
- How to let Kubernetes PetSet use existing Persistent Volumes?
- How to limit memory size for .net core application in pod of kubernetes?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.