查看信息篇
# 快速查看容器镜像版本
#查看一个Labe的的镜像版本信息
kubectl get pods -l labelname=$1 -o jsonpath="{..image}" |tr -s '[[:space:]]' '\n' |sort |uniq -c
#查看一个Label的的镜像版本信息
kubectl get pods -l labelname=$1 -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |sort
#查看单个pod的镜像版本信心
kubectl get pods $1 -o jsonpath="{..image}" |tr -s '[[:space:]]' '\n' |sort |uniq -c
快速查看pod & node 信息
# 查看容器label,快速获取服务Label label
kubectl get pods --show-labels
# 查看节点label
kubectl get nodes --show-labels
# 查看节点就绪状态,部署服务POD出现pending状态,可以使用此命令判断节点的哪些资源不够了
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \ && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
# 输出结果
192.168 .2 .13: MemoryPressure = False;
DiskPressure = False;
PIDPressure = False;
CPUPressure = False;
Ready = True;
查看Node资源使用率情况
kubectl describe nodes | grep -A 3 "Name:\|Resource .*Requests .*Limits" | grep -v "Roles:"
Name: docker-desktop
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
--
Resource Requests Limits
-------- -------- ------
cpu 750m (12%) 0 (0%)
memory 140Mi (7%) 340Mi (18%)
按照重启顺序排序POD
kubectl get pods -A --sort-by='.status.containerStatuses[0].restartCount'
NAMESPACE NAME READY STATUS RESTARTS AGE
default busybox-sleep 1/1 Running 0 45h
kube-system kube-proxy-lkwfs 1/1 Running 1 45h
kube-system coredns-66bff467f8-8mpdg 1/1 Running 1 45h
kube-system coredns-66bff467f8-vvw5c 1/1 Running 1 45h
kube-system etcd-docker-desktop 1/1 Running 1 45h
kube-system kube-apiserver-docker-desktop 1/1 Running 1 45h
kube-system kube-controller-manager-docker-desktop 1/1 Running 1 45h
kube-system kube-scheduler-docker-desktop 1/1 Running 1 45h
kube-system storage-provisioner 1/1 Running 1 45h
kube-system vpnkit-controller 1/1 Running 1 45h
default busybox-sleep-less 1/1 Running 18 45h
统计每个Node的POD分布数量
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.spec.nodeName}{"\n"}' |sort |uniq -c |sort -k1nr
11 docker-desktop
1
脚本篇
按Node查看所有POD的CPU和MEM的Request和Limits数值脚本
#!/bin/bash
set -euo pipefail
echo -e "Iterating...\n"