gpt4 book ai didi

kubernetes - 如何列出不是由 Controller 创建的所有 Pod

转载 作者:行者123 更新时间:2023-12-02 11:53:29 25 4
gpt4 key购买 nike

我需要获取不是由 Controller 创建的所有 pod 的列表,以便我可以在对节点进行耗尽之前决定如何处理它们。

否则我会收到消息:

error: cannot delete Pods not managed by ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet (use --force to override) while running the drain.

我可以通过运行 kubectl describe <pod> 找到信息并查看 Controlled By: 是否丢失,但我想以编程方式搜索节点上的所有 pod,因为 kubectl describe不是为此而设计的。我需要找到替代方法。

最佳答案

您可以依靠 ownerReferences API 对象来找到它:

$ kubectl explain pod.metadata.ownerReferences

KIND: Pod

VERSION: v1

RESOURCE: ownerReferences <[]Object>

DESCRIPTION: List of objects depended by this object. If ALL objects in the list have been deleted, this object will be garbage collected. If this object is managed by a controller, then an entry in this list will point to this controller, with the controller field set to true. There cannot be more than one managing controller.

裸 pod(即没有 Controller /所有者的 pod)将不包含 ownerReferences 字段,因此您可以使用 --custom-columns 找出哪些 pod是否受控:

$ kubectl get pods --all-namespaces -o custom-columns=NAME:.metadata.name,CONTROLLER:.metadata.ownerReferences[].kind,NAMESPACE:.metadata.namespace
NAME CONTROLLER NAMESPACE
nginx-85ff79dd56-tvpts ReplicaSet default
static-pod1 <none> default
static-pod2 <none> default
coredns-5644d7b6d9-6hg82 ReplicaSet kube-system
coredns-5644d7b6d9-wtph7 ReplicaSet kube-system
etcd-minikube <none> kube-system
kube-addon-manager-minikube <none> kube-system
kube-apiserver-minikube <none> kube-system
kube-controller-manager-minikube <none> kube-system
kube-proxy-fff5c DaemonSet kube-system
kube-scheduler-minikube <none> kube-system
storage-provisioner <none> kube-system
tiller-deploy-55c9c4b4df-hgzwm ReplicaSet kube-system

如果您只想要不属于 Controller 管理器的 pod 名称,您可以使用 jq 处理 kubectl get -o json 的输出(对后期脚本处理非常有用):

$ kubectl get pods --all-namespaces -o json | jq -r '.items | map(select(.metadata.ownerReferences == null ) | .metadata.name) | .[]'
static-pod1
static-pod1
etcd-minikube
kube-addon-manager-minikube
kube-apiserver-minikube
kube-controller-manager-minikube
kube-scheduler-minikube
storage-provisioner

关于kubernetes - 如何列出不是由 Controller 创建的所有 Pod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58613628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com