在创建一个CNF的过程中,POD的状态不尽相同,那么如何在众多POD中快速找到没有Ready的呢?当然可以在kubectl get pod -n <namemspace>中肉眼搜索,但是如果可以直接打印出没有Ready的POD岂不美哉?这就是如下命令的使命:
###左右滑动 kubectl -n <namespace> get pod | grep -iv completed | awk -F'[ /] ''BEGIN{found=0} !/NAME/ {if (($2!=$3)||($4!='Running')) { found=1; print $0}} END { if (!found) print 'All pods are ready'}'
例子:不带namespace默认是default ubuntu@VM-16-3-ubuntu:~$ kubectl get pod | awk -F'[ /] ''BEGIN{found=0} !/NAME/ {if (($2!=$3)||($4!='Running')) { found=1; print $0}} END { if (!found) print 'All pods are ready'}' web-0 0/1 ContainerCreating 0 14s ubuntu@VM-16-3-ubuntu:~$ kubectl get pod | awk -F'[ /] ''BEGIN{found=0} !/NAME/ {if (($2!=$3)||($4!='Running')) { found=1; print $0}} END { if (!found) print 'All pods are ready'}' All pods are ready ubuntu@VM-16-3-ubuntu:~$ kubectl get pod NAME READY STATUS RESTARTS AGE web-1 1/1 Running 0 9d web-2 1/1 Running 0 9d web-0 1/1 Running 0 34s ubuntu@VM-16-3-ubuntu:~$
这个命令还可以搭配for循环来查询一个集群下各个Namespace:
###左右滑动 for i in `kubectl get ns --no-headers | awk '{print $1}'`;doecho'# Namespace '$namespace;kubectl -n $i get pod | grep -iv completed | awk -F'[ /] ''BEGIN{found=0} !/NAME/ {if (($2!=$3)||($4!='Running')) { found=1; print $0}} END { if (!found) print 'All pods are ready'}' ;done
如果想使用-A来查看集群中不Ready的POD,就需要稍微的变化一下awk中判断参数:
###左右滑动 kubectl get pod -A | grep -iv completed | awk -F'[ /] ''BEGIN{found=0} !/NAME/ {if (($3!=$4)||($5!='Running')) { found=1; print $0}} END { if (!found) print 'All pods are ready'}'
查看POD中所有的Container的状态
除了查看POD以外,有的时候还需要查看POD中Container的状态,kubectl describe pod -n <namespace> <pod name>搭配人眼识别系统可以很浪费时间和眼力的看到容器状态,因此又攒了一个命令来快速直观的打印(同事分享,后期优化):
###左右滑动 namesapce=<namespace> for i in `kubectl -n ${namesapce} get pod | grep -iv completed | awk -F'[ /] ''BEGIN{found=0} !/NAME/ {if (($2!=$3)||($4!='Running')) { found=1; print $0}}' | awk '{print $1}'`;do kubectl get pod -n ${namesapce}$i -o jsonpath='{'###InitContainerStatus\n'}{range .status.initContainerStatuses[*]}{.name}{':\t READY: '}{.ready}{'\n'}{end}{'###ContainerStatus\n'}{range .status.containerStatuses[*]}{.name}{':\t READY: '}{.ready}{'\t STARTED: '}{.started}{'\n'}{end}' | column -t ;done
例子: ubuntu@VM-16-3-ubuntu:~$ namesapce=default for i in `kubectl -n ${namesapce} get pod | grep -iv completed | awk -F'[ /] ''BEGIN{found=0} !/NAME/ {if (($2!=$3)||($4!='Running')) { found=1; print $0}}' | awk '{print $1}'`;do kubectl get pod -n ${namesapce}$i -o jsonpath='{'###InitContainerStatus\n'}{range .status.initContainerStatuses[*]}{.name}{':\t READY: '}{.ready}{'\n'}{end}{'###ContainerStatus\n'}{range .status.containerStatuses[*]}{.name}{':\t READY: '}{.ready}{'\t STARTED: '}{.started}{'\n'}{end}' | column -t ;done ###InitContainerStatus ###ContainerStatus nginx: READY: false STARTED: false ubuntu@VM-16-3-ubuntu:~$
其他
工作中因为种种需求还攒了很多类似的命令:
比如统计各个namespace下的POD的数量,并计算总和:
###左右滑动 pod_count=0;for i in `kubectl get ns --no-headers | awk '{print $1}'`;doecho'#Namespace $i';kubectl get pod -n $i --no-headers | grep -i running| wc -l;pod_count=$(( pod_count `kubectl get pod -n $i --no-headers | grep -i running| wc -l` ));echo;echo;done;echo'Total CNF POD: '$pod_count
比如到每一个worker上ping一个走primary网络的远端IP,其中的ping可以换成人和想执行的命令比如ip route get <IP>等:
###左右滑动 for i in `kubectl get node --no-headers| awk '{print $1}'`;doecho$i; ssh -q `kubectl get node $i -o jsonpath='{.status.addresses[0].address}'` 'ping <DST IP> -c 2';echo;echo;done
###左右滑动 #查看node的所有label kubectl get nodes --show-labels
例子: ubuntu@VM-16-3-ubuntu:~$ kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS vm-16-3-ubuntu Ready control-plane,master 11d v1.27.7 k3s2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/instance-type=k3s,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=vm-16-3-ubuntu,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=true,node-role.kubernetes.io/master=true,node.kubernetes.io/instance-type=k3s ubuntu@VM-16-3-ubuntu:~$
#再知道Label名字的前提下美观的打印 kubectl get nodes -L <label key>
例子: ubuntu@VM-16-3-ubuntu:~$ kubectl get nodes -L 'node.kubernetes.io/instance-type' NAME STATUS ROLES AGE VERSION INSTANCE-TYPE vm-16-3-ubuntu Ready control-plane,master 11d v1.27.7 k3s2 k3s ubuntu@VM-16-3-ubuntu:~$
例子: ubuntu@VM-16-3-ubuntu:~$ kubectl label node vm-16-3-ubuntu test=123 node/vm-16-3-ubuntu labeled ubuntu@VM-16-3-ubuntu:~$ kubectl get nodes -L test NAME STATUS ROLES AGE VERSION TEST vm-16-3-ubuntu Ready control-plane,master 11d v1.27.7 k3s2 123 ubuntu@VM-16-3-ubuntu:~$