我正在尝试按区域列出吊舱的
为此,我获取了节点的详细信息,其中它有区域详细信息和节点标识符,而pod列表有公共节点标识符,现在我想合并这两个结果,并希望按区域打印pod的区域。kubectl get nodes --label-columns failure-domain.beta.kubernetes.io/zone | awk '{print $1,$6}' | awk '{if (NR!=1) {print}}' | column -t > Nodes
kubectl get pods -n app -o wide | awk '{print $1, $7}' | column -t | awk '{if (NR!=1) {print}}' > pods
所需输出应类似
zone 1a
pod 1 nodename status region
pod 2 nodename status region
zone 1b
pod 1 nodename status region
pod 2 nodename status region
zone 1c
pod 1 nodename status region
pod 2 nodename status region
zone 2a
pod 1 nodename status region
pod 2 nodename status region
我试过的脚本,
# bash scrip to print pods in each zone
kubectl get nodes --label-columns failure-domain.beta.kubernetes.io/zone | awk '{print $1,$6}' | awk '{if (NR!=1) {print}}' | column -t > A
# get the pods list from app namespace
kubectl get pods -n app -o wide | awk '{print $1, $7}' | column -t | awk '{if (NR!=1) {print}}' > B
# Compare file A and B and list the pods in each zone
# > AB
cat A B
cat B | while read LINE;do
matching_col=$(echo "$LINE"| awk '{print $2}');
append_col=$(echo "$LINE"| awk '{print $1}');
cat A | grep $matching_col | head -1
if [[ $? -eq 0 ]]; then
line1="`cat A | grep $matching_col | head -1` ${append_col}"
echo $line1 >> AB
fi
done
# write the available zones list to C
cat AB | awk '{print $3,$6}' | column -t | sort > C
# compare A and B and list the pods in each zone, comparing C and A
for i in `cat C | awk '{print $1}'`; do
echo "pods in zone:" $i
kubectl get pods -n app -o wide | sed -n "/$i/p"
done
# delete the files
# rm AB
# rm A
# rm B
# rm C
来自节点的输出>
ip-192-5-1-199.eu-west-2.compute.internal eu-west-2a
ip-192-5-1-216.eu-west-2.compute.internal eu-west-2a
ip-192-5-2-212.eu-west-2.compute.internal eu-west-2b
ip-192-5-2-36.eu-west-2.compute.internal eu-west-2b
ip-192-5-3-157.eu-west-2.compute.internal
ip-192-5-3-222.eu-west-2.compute.internal eu-west-2c
ip-192-5-3-45.eu-west-2.compute.internal eu-west-2c
来自吊舱的输出>B
app-node-setup ip-192-5-3-157.eu-west-2.compute.internal
app-node-setup ip-192-5-2-212.eu-west-2.compute.internal
app-node-setup ip-192-5-1-216.eu-west-2.compute.internal
app-node-setup ip-192-5-2-36.eu-west-2.compute.internal
app-node-setup ip-192-5-3-45.eu-west-2.compute.internal
app-node-setup ip-192-5-3-222.eu-west-2.compute.internal
app-node-setup ip-192-5-1-199.eu-west-2.compute.internal
app-operator-5 ip-192-5-3-45.eu-west-2.compute.internal
telnet-1-accesspool ip-192-5-1-199.eu-west-2.compute.internal
telnet-1-accesspool ip-192-5-3-222.eu-west-2.compute.internal
telnet-1-manager ip-192-5-2-212.eu-west-2.compute.internal
storage-set-from-configmap-1-ss-5 ip-192-5-2-36.eu-west-2.compute.internal
storage-set-from-configmap-1-ss-1 ip-192-5-3-157.eu-west-2.compute.internal
storage-set-from-configmap-1-ss-2 <none>
Actual Output
kubectl get nodes --label-columns failure-domain.beta.kubernetes.io/zone
NAME STATUS ROLES AGE VERSION ZONE
ip-192-0-1-199.eu-west-2.compute.internal Ready master,worker 23d v1.20.11+e880017 eu-west-2a
ip-192-0-1-216.eu-west-2.compute.internal Ready master,worker 23d v1.20.11+e880017 eu-west-2a
ip-192-0-2-212.eu-west-2.compute.internal Ready master,worker 23d v1.20.11+e880017 eu-west-2b
ip-192-0-2-36.eu-west-2.compute.internal Ready master,worker 23d v1.20.11+e880017 eu-west-2b
ip-192-0-3-157.eu-west-2.compute.internal Ready <none> 20d v1.20.11+c343126
ip-192-0-3-222.eu-west-2.compute.internal Ready master,worker 23d v1.20.11+e880017 eu-west-2c
ip-192-0-3-40.eu-west-2.compute.internal Ready master,worker 23d v1.20.11+e880017 eu-west-2c
kubectl get pods -n app -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
app-node-setup-6qckk 1/1 Running 0 17d 169.24.206.214 ip-192-0-3-157.eu-west-2.compute.internal <none> <none>
app-node-setup-ccwww 1/1 Running 0 17d 169.24.184.7 ip-192-0-2-212.eu-west-2.compute.internal <none> <none>
app-node-setup-pn8vs 1/1 Running 0 17d 169.24.1925.242 ip-192-0-1-216.eu-west-2.compute.internal <none> <none>
app-node-setup-pqm7s 1/1 Running 0 17d 169.24.56.85 ip-192-0-2-36.eu-west-2.compute.internal <none> <none>
app-node-setup-qxd5c 1/1 Running 0 17d 169.24.246.139 ip-192-0-3-40.eu-west-2.compute.internal <none> <none>
app-node-setup-rpm56 1/1 Running 0 17d 169.24.25.2192 ip-192-0-3-222.eu-west-2.compute.internal <none> <none>
app-node-setup-wbf75 1/1 Running 0 17d 169.24.5.209 ip-192-0-1-199.eu-west-2.compute.internal <none> <none>
app-operator-0 1/1 Running 0 17d 169.24.246.137 ip-192-0-3-40.eu-west-2.compute.internal <none> <none>
telnet-1-accesspool-1-ss-0 2/2 Running 3 17d 169.24.5.2192 ip-192-0-1-199.eu-west-2.compute.internal <none> <none>
telnet-1-accesspool-1-ss-1 2/2 Running 2 17d 169.24.25.212 ip-192-0-3-222.eu-west-2.compute.internal <none> <none>
telnet-1-manager-1-ss-0 4/4 Running 1 17d 169.24.184.8 ip-192-0-2-212.eu-west-2.compute.internal <none> <none>
storage-set-from-configmap-1-ss-0 2/2 Running 3 17d 169.24.56.1923 ip-192-0-2-36.eu-west-2.compute.internal <none> <none>
storage-set-from-configmap-1-ss-1 2/2 Running 6 17d 169.24.206.218 ip-192-0-3-157.eu-west-2.compute.internal <none> <none>
storage-set-from-configmap-1-ss-2 0/2 Pending 0 17d <none> <none> <none> <none>
这是一个未测试的解决方案,因为问题中没有测试输入。
脚本.awk
FNR == 1 { #skip first line in each input
next;
}
FNR == NR { # read input #1: kubectl get nodes --label-columns failure-domain.beta.kubernetes.io/zone
nodesArr[$6] = nodesArr[$6] $1; # each nodeArr[] has a list of ip-addr
}
FNR != NR { # read input #2: kubectl get pods -n app -o wide
ipArr[$7] = ipArr[$7] $1; # each ipArr[] has a list of pod names
}
END {
for (node in nodesArr) { # for each node
region = gensub(/[^-]+$/, "