当我列出集群中的 pod(在特定节点和所有命名空间中)时,列出的每个 pod 还包含容器状态,其中我得到列出的每个容器的容器运行时引擎 ID。
为了说明,我使用这个 Python3 脚本通过官方 Kubernetes Python 客户端访问集群 API;这是How to find all Kubernetes Pods on the same node from a Pod using the official Python client? 中稍作修改的版本?
from kubernetes import client, config
import os
def main():
# it works only if this script is run by K8s as a POD
config.load_incluster_config()
# use this outside pods
# config.load_kube_config()
# grab the node name from the pod environment vars
node_name = os.environ.get('KUHBERNETES_NODE_NAME', None)
v1 = client.CoreV1Api()
print("Listing pods with their IPs on node: ", node_name)
# field selectors are a string, you need to parse the fields from the pods here
field_selector = 'spec.nodeName='+node_name
ret = v1.list_pod_for_all_namespaces(watch=False, field_selector=field_selector)
for i in ret.items:
print("%s\t%s\t%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
for c in i.status.container_statuses:
print("\t%s\t%s" %
(c.name, c.container_id))
if __name__ == '__main__':
main()
注意 Pod 使用合适的 ServiceAccount 使其能够列出所有命名空间中的 pod。
在 minikube 设置上运行时的典型结果输出可能如下所示:
Listing pods with their IPs on node: minikube
172.17.0.5 cattle-system cattle-cluster-agent-c949f5b48-llm65
cluster-register docker://f12fcb1acbc2e7c01c24dbd831ed53ab2a6df2353abe80988ae132c39f7c68c6
10.0.2.15 cattle-system cattle-node-agent-hmq86
agent docker://e335a3d30ea37887ac2a1a1cc339eabb0a0098471f86db1926cfe02eef2c6b8f
172.17.0.6 gw pyk8s
py8ks docker://1272747b52983e8f745bd118b2d935c1d314e9c6cc310e88013021ba974bc030
172.17.0.4 kube-system coredns-c4cffd6dc-7lsdn
coredns docker://8b0c3c67532ee2d7d16958a33cb942d5bd09ed37ded1d570830b5f7e5f7a09ab
10.0.2.15 kube-system etcd-minikube
etcd docker://5e0e0ee48248e9779a2a5f9347a39c58743562b10719a31d7d6fc0af5e79e093
10.0.2.15 kube-system kube-addon-manager-minikube
kube-addon-manager docker://96908bc5d5fd9b87779c8a8544591e5aeda2d58956fb365ab595681605b01001
10.0.2.15 kube-system kube-apiserver-minikube
kube-apiserver docker://0711ec9a2321b1b5a801ab2b19409a1edc731058aa994978f989185efc4c8294
10.0.2.15 kube-system kube-controller-manager-minikube
kube-controller-manager docker://16d2e11a8dea2a46cd44bc97a5f894e7ff9da2da70f3c24376b4189dd912336e
172.17.0.2 kube-system kube-dns-86f4d74b45-wbdf6
dnsmasq docker://653c7ef27760a820449ee518b59e39ab4a7f65cade996ed85313c98038827f67
kubedns docker://6cf6aaeac1192cf1d580293e03164db57bc70bce41cf91e5cac081010fe48cf7
sidecar docker://9816e10d8455988aa400f98df32cfa69ce89fbfc3e3e1554145d9d6418c02157
10.0.2.15 kube-system kube-proxy-ll7lq
kube-proxy docker://6b8c7ce1ae3c8fbc487bf05ccca9105dffaf675f916cdb62a595d8be7902e69b
10.0.2.15 kube-system kube-scheduler-minikube
kube-scheduler docker://ab79e46ba900753d86b7000061720551a199c0ea6eee923fcd86bda2d86cc54a
172.17.0.3 kube-system kubernetes-dashboard-6f4cfc5d87-bmnl8
kubernetes-dashboard docker://a73ef6b30fb87826a4a71ba428a01511278a759d69fade82ddd654911ec3f14f
10.0.2.15 kube-system storage-provisioner
storage-provisioner docker://51eaf90bc3ae11baa354a436e366730c19206c73743c6517a0ad9eb8f0b89896
请注意,这列出了 pod 容器的容器 ID,但暂停容器 ID 除外。是否有一种 API 方法也可以获取/列出 pod 中暂停容器的容器 ID?
我尝试搜索诸如“kubernetes api pod pause container id”之类的东西......但我没有得到任何有用的答案,除了 containerStatuses 的常用 API 结果等。