这样,我们就可以遍历kubectl get pod -o=name的结果,在循环中取出每个pod的编排信息,然后从中过滤出我们想要的信息(即pod的标签信息),操作和上面获取matchLabels有很多类似的地方.
关键代码如下:
[centos@k8s-master ~]$ for item in $( kubectl get pod --output=name); do kubectl get "$item" --output=json | jq -r '.metadata.labels | to_entries | .[] | " \(.key)=\(.value)"'; done app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-0 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-1 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-2 app=easymock pod-template-hash=84767b6f75 app=helloworld pod-template-hash=d5d5c5866 version=1.0 app=helloworld pod-template-hash=d5d5c5866 version=1.0 app=helloworld ...以上循环获取到的每一个pod的名称,然后遍历他们,取出它们的.metadata.labels字段,后面是对键值的重新组装,和上面获取deploy下的pod一节的操作类似.
从输出结果中可以看出,我们只输出的所有pod的标签,这样可读性是非常差的,我们也无法确定某一个标签归属于哪一个pod.我们需要把它们的名字也输出:
[centos@k8s-master ~]$ for item in $( kubectl get pod --output=name); do printf "Labels for %s\n" "$item"; kubectl get "$item" --output=json | jq -r '.metadata.labels | to_entries | .[] | " \(.key)=\(.value)"'; done Labels for pod/consul-0 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-0 Labels for pod/consul-1 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-1 Labels for pod/consul-2 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-2 Labels for pod/easymock-dep-84767b6f75-l84r4 app=easymock pod-template-hash=84767b6f75 Labels for pod/helloworld-d5d5c5866-24nzr app=helloworld pod-template-hash=d5d5c5866 version=1.0 ...这查比前面清析一些了,但是仍然不是十分容易识别,我们在遍历的时候每个item结束后输出一个空行,这样把不同的结果用空行隔离开来,可读性更高.
[centos@k8s-master ~]$ for item in $( kubectl get pod --output=name); do printf "Labels for %s\n" "$item"; kubectl get "$item" --output=json | jq -r '.metadata.labels | to_entries | .[] | " \(.key)=\(.value)"';printf "\n"; done Labels for pod/consul-0 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-0 Labels for pod/consul-1 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-1 Labels for pod/consul-2 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-2 Labels for pod/easymock-dep-84767b6f75-l84r4 app=easymock pod-template-hash=84767b6f75这样基本ok了,我们使用grep把名称着色显示,这样在屏幕上的输出可读性就更高了.
[centos@k8s-master ~]$ for item in $( kubectl get pod --output=name); do printf "Labels for %s\n" "$item"|grep --color -E '[^/]+$'; kubectl get "$item" --output=json | jq -r '.metadata.labels | to_entries | .[] | " \(.key)=\(.value)"';printf "\n"; done Labels for pod/consul-0 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-0 Labels for pod/consul-1 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-1 Labels for pod/consul-2 app=consul controller-revision-hash=consul-744777c4bd statefulset.kubernetes.io/pod-name=consul-2大家从上面文本中可能并看不出差别,但是如果是在linux终端就可以看到pod的名称都被着色显示了.