kubernetes / kubernetes/kubectl

RFE: 'kubectl evict ...' or similar for conveniently evicting pods

Open
#1,345 10 comments 22 reactions 0 assignees View on GitHub
kind/feature lifecycle/rotten sig/cli triage/needs-plugin-prototype
Dominant language
Go
Stars
3.3k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

### What would you like to be added?

A `kubectl evict ...` subcommand or similar syntactic sugar around [the eviction API][1].

[1]: https://kubernetes.io/docs/concepts/scheduling-eviction/api-eviction/#calling-the-eviction-api

### Why is this needed?

Using the delete API is convenient, but dangerous. For example:

```console
$ kubectl -n openshift-monitoring delete pods prometheus-k8s-0 prometheus-k8s-1
pod "prometheus-k8s-0" deleted
pod "prometheus-k8s-1" deleted
```

Leaves an OpenShift cluster temporarily without monitoring. It's safer to use the eviction API to respect PodDisruptionBudgets, like:

```console
$ kubectl create -f - < {"apiVersion": "policy/v1", "kind": "Eviction", "metadata": {"name": "prometheus-k8s-0"}}
> EOF
{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success","code":201}
$ kubectl create -f - < {"apiVersion": "policy/v1", "kind": "Eviction", "metadata": {"name": "prometheus-k8s-1"}}
> EOF
Error from server (TooManyRequests): Cannot evict pod as it would violate the pod's disruption budget.
```

However, that's a bit of a mouthful, and requires a here-doc and duplicating the pod name. It might be possible to add `--subresource` to the `create` subcommand to support something like:

```console
$ kubectl -n openshift-monitoring create --subresource eviction pod prometheus-k8s-0
```

But that would likely bump into [the current guards that enforce redundant information between the URI path and the Eviction resource][1]. It's not clear to me why the eviction handler can't backfill missing Eviction resource information like the `name` from the path information, or really, why the eviction handler cannot default the entire Eviction resource when the caller doesn't need to set explicit delete options. Possibly kubernetes/kubernetes#53185 touches on this, although I haven't wrapped my head around that yet.

Or, instead of trying to make `create` more flexible, we could grow a new subcommand like `evict` just for the eviction subresource.

But it would be nice to make the safer eviction API more convenient, so folks didn't have to decide between safety and convenience when bumping pods.

There is at least one existing plugin implementation [here][2].

[1]: https://github.com/kubernetes/kubernetes/blob/0d46dc1f467f67cba79708311193a719c2124d22/pkg/registry/core/pod/storage/eviction.go#L128-L135
[2]: https://github.com/dwradcliffe/kubectl-evict

Contributor guide

Open the contributing guide

Research direction

Start with the eviction handler in pkg/registry/core/pod/storage/eviction.go and compare its requirements with the kubectl create --raw examples in the issue. Determine whether the project should add an evict subcommand or extend create, then define completion as a convenient command that invokes the eviction API and respects PodDisruptionBudgets.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.