简体   繁体   中英

How to evict pods in kubernetes?

I'm testing a trigger i set on my k8s cluster. What it does it that detects if a pod is evicted. So to test I ran

kubectl drain <NODENAME> --ignore-daemonsets --force

but the evicted pods are just automatically deleted instead of staying in cluster. Can you guys help me on how can I evict a pod? I'm using kind to test this locally on my computer. Actually I want to read the evicted pod yaml after its evicted so that I can build that trigger.

First, to answer the title question: "How to evict pods in kube.netes?" There are a number of additional ways to trigger pod eviction, but two easy ones:

API Based Eviction

There is an API you can use to evict a pod manually . To use the API:

Apply a NoExecute Taint

A NoExecute taint that your pod does not tolerate will cause it to be evicted.

kubectl taint nodes node1 key1=value1:NoExecute

Now, the text of your question asks a slightly different question: reading the pod resource after a successful eviction. At least in my environment and k8s version, as soon as a pod is evicted, it is deleted by the service account associated with the node it was running on.

In practice, many things can delete pods - including garbage collectors , so querying a pod after it has been terminated is fraught with potential race conditions.

Instead, I would recommend setting up a programmatic watch for pod events. client-go offers the Informer pattern to accomplish this with a bit of boilerplate.

Create an informer for Pods, define an UpdateFunc for your event handler, and you will reliably receive pod state changes as they are processed by the API -- which you can then assess to determine if they were evicted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM