如何获取Kubernetes部署中镜像/容器的镜像ID(docker sha 256哈希)?
rur96b6h1#
类似这样的东西可以做到这一点(你必须安装jq):
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" }
kqlmhetl2#
没有使用jq的例子。使用jsonpath:kubectl get pods $YOUR_POD_NAME -o jsonpath="{..imageID}"使用go-templateskubectl 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
kubectl get pods $YOUR_POD_NAME -o jsonpath="{..imageID}"
kubectl get pods $YOUR_POD_NAME -o go-template --template="{{ range .status.containerStatuses }}{{ .imageID }}{{end}}"
taor4pac3#
正确的方法是:
kubectl get pods --all-namespaces -o jsonpath="{.items[*].status.containerStatuses[*].imageID}" | tr -s '[[:space:]]' '\n' | sort | uniq -c
xwmevbvl4#
最简单的一个使用是:用于获取单个命名空间中所有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}'
ar5n3qh55#
另一个版本,有一些曲折(使用--selector和YAML),对我来说最难忘的一个,使用通用的Bash工具(包括。cut(冒号):
--selector
cut
# if there is only one pod: $ kubectl get pod -o yaml | grep sha | cut -d ":" -f 3 # when selecting among multiple pods: $ kubectl get pod --selector app=ml-scraper -o yaml | grep sha | cut -d ":" -f 3
5条答案
按热度按时间rur96b6h1#
类似这样的东西可以做到这一点(你必须安装
jq
):kqlmhetl2#
没有使用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
taor4pac3#
正确的方法是:
xwmevbvl4#
最简单的一个使用是:
用于获取单个命名空间中所有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}'
ar5n3qh55#
另一个版本,有一些曲折(使用
--selector
和YAML),对我来说最难忘的一个,使用通用的Bash工具(包括。cut
(冒号):