如何获取 /var/lib/kubelet/pods 下实际的 pods 是哪个?

what 发表于: 2024-01-08   最后更新时间: 2024-01-10 13:54:08   1,455 游览

如何获取目录 /var/lib/kubelet/<pod-id>,这个<pod-id>对应的真实的 pods 是哪个?

发表于 2024-01-08
¥2.0

对应的是pod的.metadata.uid

for d in /var/lib/kubelet/pods/*; do
  p_u=$(basename "$d")
  kubectl get po -A -o json | \
    jq --arg pod_uuid "$p_u" -r '.items[] 
      | select(.metadata.uid == $pod_uuid) 
      | "uuid \($pod_uuid) is \(.metadata.name)"'
done

类似如下输出:

"Labels": {
    "annotation.io.kubernetes.container.hash": "e44bee94",
    "annotation.io.kubernetes.container.restartCount": "4",
    "annotation.io.kubernetes.container.terminationMessagePath": "/dev/termination-log",
    "annotation.io.kubernetes.container.terminationMessagePolicy": "File",
    "annotation.io.kubernetes.pod.terminationGracePeriod": "30",
    "io.kubernetes.container.logpath": "/var/log/pods/kube-system_storage-provisioner_b4aa3b1c-62c1-4661-a302-4c06b305b7c0/storage-provisioner/4.log",
    "io.kubernetes.container.name": "storage-provisioner",
    "io.kubernetes.docker.type": "container",
    "io.kubernetes.pod.name": "storage-provisioner",
    "io.kubernetes.pod.namespace": "kube-system",
    "io.kubernetes.pod.uid": "b4aa3b1c-62c1-4661-a302-4c06b305b7c0",
    "io.kubernetes.sandbox.id": "3950ec60121fd13116230cad388a4c6c4e417c660b7da475436f9ad5c9cf6738"
}

更简单方法是:

ls -lad /var/lib/kubelet/pods/*/containers/*

# 类似输出:
# /var/lib/kubelet/pods/0a82722a-57e7-4a32-b0e7-1d3864b98c98/containers/worker
# /var/lib/kubelet/pods/148c7195-c3ab-4750-afb4-e6fb34948ed5/containers/dashboard
# /var/lib/kubelet/pods/6bb42e72-902d-49b1-8beb-0e885e2708fc/containers/kube-proxy
# ...
find /var/lib/kubelet/pods/ -maxdepth 3 -name "*dnsutils*"

# 类似输出:
# /var/lib/kubelet/pods/a18df6c7-3a37-4bc0-8593-026145f4c4b6/containers/dnsutils
你的答案

查看kubernetes相关的其他问题或提一个您自己的问题