使用
aws eks
cli,有没有办法可以列出给定节点组中的所有 Pod?如果没有,还有其他选择吗?我知道我可以使用 AWS 控制台访问它。但理想情况下,我想创建一个为我做这件事的脚本。
您需要合并
kubectl get nodes
和 kubectl get pods
的输出才能得到输出。
下面的代码可以并排列出 pod 和节点组详细信息。
#!/bin/bash
#
# Accept argument as the list of node group names
if [[ $# -eq 0 ]]; then
echo "Usage: ${0##*/} [ node_group_names ]"
echo ""
echo " E.g. ${0##*/} node_group_name1 node_group_name2 node_group_name3"
echo ""
exit 1
fi
# store the node group names into a variable. This will be iterated through to list the node groups and pods side by side
ng_list=${@:2,1}
# The header of the output
echo "NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE K8S_GROUP" | awk \
'{ printf "%-15s %-50s %-5s %-16s %-17s %-10s %-15s %-32s %-19s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9}'
# Loop through the node groups and list the pods
for ng in $ng_list; do
node_ip=$(kubectl get nodes -l Group=$ng --no-headers 2>/dev/null | grep -v "No resources found" | cut -f1 -d\ )
if [[ $node_ip != "" ]]; then
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=$node_ip --no-headers | awk -v podsng=$ng \
'{
namespace=$1;
name=$2;
ready=$3;
status=$4;
if ($7 == "ago)")
printf "%-15s %-50s %-5s %-16s %-3s %-8s %-4s %-10s %-15s %-32s %-19s\n",namespace,name,ready,status,$5,$6,$7,$8,$9,$10,podsng;
if ($7 != "ago)")
printf "%-15s %-50s %-5s %-16s %-17s %-10s %-15s %-32s %-19s\n",namespace,name,ready,status,$5,$6,$7,$8,podsng}'
fi
done
如下面的示例输出所示,可以并排看到 Pod 和节点组。
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE K8S_GROUP
kube-system aws-node-v2t8s 2/2 Running 0 4h58m 172.22.13.46 ip-172-22-13-46.ec2.internal ng-1
kube-system kube-proxy-lcpkd 1/1 Running 0 4h58m 172.22.13.46 ip-172-22-13-46.ec2.internal ng-1
monitoring prom-wl64g 1/1 Running 0 4h58m 172.22.13.46 ip-172-22-13-46.ec2.internal ng-1
production app1-service-5c7ffcb6d8-s4xp4 1/1 Running 0 4h59m 172.22.15.252 ip-172-22-13-46.ec2.internal ng-1
production app2-service-55dccf7696-42xfk 1/1 Running 2 4h59m 172.22.13.112 ip-172-22-13-46.ec2.internal ng-1
production app3-service-6fb769cbf-hdphz 1/1 Running 0 4h59m 172.22.14.84 ip-172-22-13-46.ec2.internal ng-1
production app4-service-6856cd6cc4-nnw5t 1/1 Running 0 4h59m 172.22.13.151 ip-172-22-13-46.ec2.internal ng-1
kube-system aws-node-5rxcn 2/2 Running 0 4d15h 172.22.13.124 ip-172-22-13-124.ec2.internal ng-2
kube-system kube-proxy-lpqp7 1/1 Running 0 4d15h 172.22.13.124 ip-172-22-13-124.ec2.internal ng-2
monitoring prometheus-node-exporter-7s6hf 1/1 Running 0 4d15h 172.22.13.124 ip-172-22-13-124.ec2.internal ng-2
production app5-5b4db956ff-zdl25 1/1 Running 0 3d1h 172.22.12.35 ip-172-22-13-124.ec2.internal ng-2