https://github.com/rajatjindal/kubectl-evict-pod
This plugin evicts the given pod and is useful for testing pod disruption budget rules
https://github.com/rajatjindal/kubectl-evict-pod
evict-pod kubectl kubectl-plugins
Last synced: 4 months ago
JSON representation
This plugin evicts the given pod and is useful for testing pod disruption budget rules
- Host: GitHub
- URL: https://github.com/rajatjindal/kubectl-evict-pod
- Owner: rajatjindal
- License: apache-2.0
- Created: 2019-10-12T14:06:47.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-08-13T17:07:30.000Z (almost 2 years ago)
- Last Synced: 2025-02-07T14:09:08.070Z (over 1 year ago)
- Topics: evict-pod, kubectl, kubectl-plugins
- Language: Go
- Size: 10.8 MB
- Stars: 71
- Watchers: 3
- Forks: 16
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Funding: FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# kubectl-evict-pod
This plugin evicts pods:
- for testing pod disruption budget rules
- safely restarting applications
## Usage
- evict single pod: `kubectl evict-pod -n `
- evict multiple pods: `kubectl evict-pod -n -l app=foo`
- evict all pods: `kubectl evict-pod -n --all`
- evict multiple pods until every one is gone: `kubectl evict-pod -n -l app=foo --retry`
- show options: `kubectl evict-pod -h`
## Install
```
kubectl krew install evict-pod
```
## Testing
### Pod evicted successfully scenario
```bash
# before running the evict-pod command
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-6wvj6 1/1 Running 0 10m
coredns-fb8b8dccf-826fh 1/1 Running 0 11m
# now lets evict 1 coredns pod
$ ./kubectl-evict-pod coredns-fb8b8dccf-6wvj6 -n kube-system
INFO[0000] pod "coredns-fb8b8dccf-6wvj6" in namespace kube-system evicted successfully
# the pod has been evicted successfully
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-7ngmk 1/1 Running 0 42s
coredns-fb8b8dccf-826fh 1/1 Running 0 11m
```
### Pod eviction prevented by pod disruption budget
- create the pod disruption budget using following spec
```yaml
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: coredns-pdb
spec:
minAvailable: 2
selector:
matchLabels:
k8s-app: kube-dns
```
- apply the pod disruption budget to the cluster
```bash
# lets apply the pod disruption budget
$ kubectl apply -f pdb.yaml -n kube-system
poddisruptionbudget.policy/coredns-pdb created
```
- Now lets try to evict the pods again
```bash
# get existing pods
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-7ngmk 1/1 Running 0 10m
coredns-fb8b8dccf-826fh 1/1 Running 0 11m
# now lets try to evict the pod again
$ ./kubectl-evict-pod coredns-fb8b8dccf-826fh -n kube-system
Error: Cannot evict pod as it would violate the pod\'s disruption budget.
exit status 1
# observe pods continue to run
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-7ngmk 1/1 Running 0 10m
coredns-fb8b8dccf-826fh 1/1 Running 0 11m
```
## Development
Update the code and use `go run . ` to test.