简体   繁体   English

如何驱逐 kube.netes 中的 pod?

[英]How to evict pods in kubernetes?

I'm testing a trigger i set on my k8s cluster.我正在测试我在 k8s 集群上设置的触发器。 What it does it that detects if a pod is evicted.它的作用是检测 pod 是否被驱逐。 So to test I ran所以为了测试我跑了

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

but the evicted pods are just automatically deleted instead of staying in cluster.但是被逐出的 pod 只是自动删除而不是留在集群中。 Can you guys help me on how can I evict a pod?你们能帮我解决如何驱逐豆荚的问题吗? I'm using kind to test this locally on my computer.我正在使用 kind 在我的计算机上进行本地测试。 Actually I want to read the evicted pod yaml after its evicted so that I can build that trigger.实际上,我想在被驱逐后阅读被驱逐的 pod yaml,以便我可以构建该触发器。

First, to answer the title question: "How to evict pods in kube.netes?"首先,回答标题问题:“How to evict pods in kube.netes?” There are a number of additional ways to trigger pod eviction, but two easy ones:还有许多其他方法可以触发 Pod 驱逐,但有两种简单的方法:

API Based Eviction API 基于驱逐

There is an API you can use to evict a pod manually .您可以使用 API 手动驱逐 pod To use the API:要使用 API:

Apply a NoExecute Taint应用 NoExecute 污点

A NoExecute taint that your pod does not tolerate will cause it to be evicted.您的 pod 不能容忍的NoExecute 污点将导致它被驱逐。

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.现在,您的问题文本提出了一个略有不同的问题:在成功驱逐读取 pod 资源。 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.至少在我的环境和 k8s 版本中,一旦 pod 被逐出,它就会被与其运行的节点关联的服务帐户删除。

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.实际上,很多东西都可以删除 pod——包括垃圾收集器,因此在 pod 终止后查询它充满了潜在的竞争条件。

Instead, I would recommend setting up a programmatic watch for pod events.相反,我会建议为 pod 事件设置程序化监视。 client-go offers the Informer pattern to accomplish this with a bit of boilerplate. client-go提供了 Informer 模式来通过一些样板文件来实现这一点。

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.为 Pod 创建一个 informer,为您的事件处理程序定义一个UpdateFunc ,您将可靠地接收 pod state 更改,因为它们由 API 处理——然后您可以评估它们是否被驱逐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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