Reputation: 171
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.
Upvotes: 1
Views: 9113
Reputation: 11
Not sure if this is still relevant or not but you seem to have a misunderstanding of the paradigm of k8s as it related to pods. Pods are seen as basically throw away. Pod died, start a new one. Pod evicted, start a new one elsewhere, etc. The only times that k8s really keeps them around is some small cases where there's a small hickup actually starting the pod, like resources or such.
In order to get something that will stay alive you want to use something like a Deployment, DaemonSet, StatefulSet, etc. You provide a template spec to these objects and they manage the creation of the Pods for you.
Upvotes: 0
Reputation: 252
First, to answer the title question: "How to evict pods in kubernetes?" There are a number of additional ways to trigger pod eviction, but two easy ones:
There is an API you can use to evict a pod manually. To use the API:
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.
Upvotes: 3