Kubernetes學習筆記(三)
[檢視某個node上的pod]
> kubectl get pods -o wide --namespace=kube-system|grep master-0
> kubectl get pods --field-selector=spec.nodeName='master-0' --all-namespaces #node master-0上的pod
或者> kubectl describe node worker-0
或者 > kubectl get pod --field-selector spec.nodeName='master-0' --all-namespaces
> kubectl get node | awk '{print $1}' | xargs -t -i kubectl get pods --field-selector spec.nodeName={} --all-namespaces #列出每個node上的所有pod
[獲取pod的UID]
注:目錄/var/log/pods下資料夾名字是pod的UID; 獲取container詳細資訊時,裡面包含所屬pod的UID.
1) 方法一:jq
>kubectl get pods -n <namespace> <pod-name> -o jsonpath='{.metadata.uid}'
>kubectl get pod -n <namespace> <pod-name> -o json | jq '.metadata.uid'
From <https://stackoverflow.com/questions/57799684/how-do-i-get-the-pod-id-in-kubernetes>
2) 方法二:grep
>kubectl get pods -n <namespace> <pod-name> -o yaml | grep uid
3) 方法三:custom-column
>kubectl get pods -n <namespace> -o custom-columns=PodName:.metadata.name,PodUID:.metadata.uid #推薦
(注:支援這些引數: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP)
根據需要靈活使用,如下例子:
>kubectl get pods -A -o custom-columns=NodeName:.spec.nodeName,PodName:.metadata.name,PodUID:.metadata.uid|
>kubectl get pods -A -o custom-columns=PodName:.metadata.name,PodUID:.metadata.uid,PodIP:.status.podIP
>kubectl get pods -n <namespace> -o custom-columns=PodName:.metadata.name,PodUID:.metadata.uid,PodIP:.status.podIP
>kubectl get pods -n <namespace> -o custom-columns=PodName:.metadata.name,PodIP:.status.podIP,hostIp:.status.hostIP
>kubectl get pods -n <namespace> -o custom-columns=NodeName:.spec.nodeName,PodName:.metadata.name,PodIP:.status.podIP,hostIp:.status.hostIP
>kubectl get pods -n <namespace> -o custom-columns=NodeName:.spec.nodeName,PodName:.metadata.name,Service:spec.serviceAccountName
From <https://stackoverflow.com/questions/57799684/how-do-i-get-the-pod-id-in-kubernetes>
>kubectl get pod -A -o=jsonpath='{range.items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'
>kubectl get po $POD_NAME -n $NAMESPACE -o=jsonpath='{range.items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}' #該指令無效
[獲取pod的container和image資訊]
1) 檢視pod裡面的container:
>kubectl describe pod xxx
2) 獲取pod裡的業務容器:
>kubectl get pod <pod name> -o jsonpath={.spec.containers[*].name} |xargs -n 1
例如: kubectl get pod csi-cinder-controllerplugin-0 -n kube-system -o jsonpath={.spec.containers[*].name}|xargs -n 1
3) 獲取pod裡的Initial容器:
>kubectl get pod <pod name> -o jsonpath={.spec.initContainers[*].name}
>kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"pod: "}{.metadata.name}{"\n"}{range.status.containerStatuses[*]}{"\tname: "}{.containerID}{"\n\timage: "}{.image}{"\n"}{end}'
4) 獲取container id和image資訊:
參考文件:
how to get the container id in pod? · Issue #50309 · kubernetes/kubernetes · GitHub
Expose Pod Information to Containers Through Files | Kubernetes
5) List All Container Images Running in a Cluster
From <https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/>
>kubectl get pods -n <namespace> -o jsonpath="{.items[*].spec.containers[*].image}"|xargs -n 1
>kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}"|xargs -n 1
>kubectl get pods --all-namespaces -o jsonpath='{range.items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}'|
sort