列出集群的容器镜像¶
列错所有namespaces中的容器镜像¶
以下命令可以列出集群所有namespace中容器镜像:
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
输出类似:
1 docker.io/grafana/loki:2.7.3
1 docker.io/istio/examples-bookinfo-details-v1:1.17.0
1 docker.io/istio/examples-bookinfo-productpage-v1:1.17.0
1 docker.io/istio/examples-bookinfo-ratings-v1:1.17.0
1 docker.io/istio/examples-bookinfo-reviews-v1:1.17.0
1 docker.io/istio/examples-bookinfo-reviews-v2:1.17.0
1 docker.io/istio/examples-bookinfo-reviews-v3:1.17.0
1 docker.io/istio/pilot:1.17.3
348 docker.io/istio/proxyv2:1.17.3
3 docker.io/istio/proxyv2:1.18.2
1 docker.io/jaegertracing/all-in-one:1.35
1 docker.io/kubeflowkatib/katib-controller:v0.16.0-rc.1
29 docker.io/kubeflowkatib/katib-db-manager:v0.16.0-rc.1
1 docker.io/kubeflowkatib/katib-ui:v0.16.0-rc.1
...
上面的命令中:
kubectl get pods --all-namespaces
获取所有pods列表-o jsonpath={.items[*].spec['initContainers', 'containers'][*].image}
获取json格式的实例镜像,此时输出是堆叠在一行里面通过
tr
将空格替换成换行通过
sort
排序通过
uniq
聚合
不过,上述命令没有列出每个镜像的大小,有一个改进命令如下:
kubectl get nodes -o json | jq '.items[].status.images[] | .names[1], .sizeBytes'
# 输出镜像大小转换成MB
kubectl get nodes -o json | jq '.items[].status.images[] | .names[1], (.sizeBytes | tonumber/1024/1024)'
输出类似
"docker.io/istio/examples-bookinfo-reviews-v3:1.17.0"
415288765
"nvcr.io/nvidia/gpu-operator:v23.3.2"
229301471
"gcr.io/ml-pipeline/metadata-envoy:2.0.1"
103726232
...
列出指定pod中所有container¶
列粗一个pod中所有容器:
kubectl get pods [pod-name-here] -n [namespace] -o jsonpath='{.spec.containers[*].name}'
举例:
kubectl --kubeconfig admin.kubeconfig -n kube-system get pods apiserver-56c6db8b58-8l4dn -o jsonpath='{.spec.containers[*].name}'
备注
其他案例以后补充