获取 Kubernetes deployment 上 pod 中镜像的镜像和 SHA 镜像 ID

Get the image and SHA image ID of images in pod on Kubernetes deployment

如何获取 Kubernetes 部署中图像/容器的图像 ID(docker sha256 哈希)?

像这样的东西就可以了(你必须安装 jq):

$ kubectl get pod --namespace=xx yyyy -o json | jq '.status.containerStatuses[] | { "image": .image, "imageID": .imageID }'
{
  "image": "nginx:latest",
  "imageID": "docker://sha256:b8efb18f159bd948486f18bd8940b56fd2298b438229f5bd2bcf4cedcf037448"
}
{
  "image": "eu.gcr.io/zzzzzzz/php-fpm-5:latest",
  "imageID": "docker://sha256:6ba3fe274b6110d7310f164eaaaaaaaaaa707a69df7324a1a0817fe3b475566a"
}

一个没有使用 jq 的例子。

使用 jsonpath:

kubectl get pods $YOUR_POD_NAME -o jsonpath="{..imageID}"

使用 go-templates

kubectl get pods $YOUR_POD_NAME -o go-template --template="{{ range .status.containerStatuses }}{{ .imageID }}{{end}}"

参考:https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-containers-filtering-by-pod-namespace

最简单的使用方法是:

获取单个命名空间中所有 pods 图像的 SHA 值:

  • kubectl get pods -n <your-namespace> -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}'

获取所有命名空间中所有 pods 图像的 SHA 值:

  • kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}'

正确的做法:

kubectl get pods --all-namespaces -o jsonpath="{.items[*].status.containerStatuses[*].imageID}" | tr -s '[[:space:]]' '\n' | sort | uniq -c