Kubernetes学习笔记(三)
[查看某个node上的pod]
> kubectl get pods -o wide --namespace=kube-system | grep master-0 #node master-0上kube-system里的pod
> kubectl get pods --field-selector=spec.nodeName='master-0' --all-namespaces #node master-0上的pod
或者> kubectl describe node node-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,podIP等信息]
注:目录/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 | grep "nodename"
>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}' #pod名字及IP地址信息
>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}
4) 获取container id和image信息:
>kubectl get pod <pod name> -o jsonpath='{.status.containerStatuses[*].containerID}' |xargs -n 1 #获取某个pod的containerID信息
>kubectl get pod <pod name> -o custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image #获取某个pod的index=0的container和image信息
>kubectl get pod <pod name> -o custom-columns=PodName:.metadata.name,CONTAINER:.spec.containers[:].name,IMAGE:.spec.containers[:].image #获取某个pod的所有container和image信息
>kubectl get pod -n <namespace> -o custom-columns=PodName:.metadata.name,CONTAINER:.spec.containers[:].name,IMAGE:.spec.containers[:].image #获取某个namespace的所有container和image信息
>kubectl get pod -A -o custom-columns=PodName:.metadata.name,CONTAINER:.spec.containers[:].name,IMAGE:.spec.containers[:].image #获取所有pod的container和image信息
>kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"pod: "}{.metadata.name}{"\n"}{range.status.containerStatuses[*]}{"\tname: "}{.containerID}{"\n\timage: "}{.image}{"\n"}{end}'
参考文档:
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 #某个命名空间的image
>kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}"|xargs -n 1 #所有命名空间的image
>kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image' #所有pods的image
>kubectl get pods -n <namespace> -o=custom-columns='DATA:metadata.name,DATA:spec.containers[*].image' #pod及对应的image
>kubectl get pods --all-namespaces -o jsonpath='{range.items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}'|
sort #pod及对应的image
*****************************************************************************************
顺便推荐一个排障的流程图:
A visual guide on troubleshooting Kubernetes deployments (learnk8s.io)