在Kubernetes部署

时间:2017-08-10 11:20:48

标签: kubernetes kubectl

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

4 个答案:

答案 0 :(得分:6)

这样的事情可以解决问题(你必须安装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"
}

答案 1 :(得分:3)

不使用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

答案 2 :(得分:0)

最简单的使用方法是:

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

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

用于获取所有命名空间中所有 Pod 的图像 SHA 值:

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

答案 3 :(得分:0)

正确的做法:

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