明智地列出 Pod 区域

问题描述 投票:0回答:2

我正在尝试列出 Pod 的区域,

为此,我正在获取节点的详细信息,其中包含区域详细信息和节点标识符,而 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

节点 > A 的输出

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

pod 的输出 > 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>
bash shell kubernetes awk
2个回答
0
投票

这是一个未经测试的解决方案,因为问题中没有测试输入。

脚本.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(/[^-]+$/, "\0", 1, node); # extract the region from node
    printf("%s\n", region); # output region
    split(nodesArr[node], tempIpArr); # convert list of ip-addr to array tempIpArr[]
    for (currIp in tempIpArr) { # for each ip in tempIpArr[]
      split(ipArr[currIp], podsArr); # convert list of pods to array podsArr[]
      podsCount = 1; # reset pods count
      for (pod in podsArr) { # for each pod in podsArr[]
        printf("\%s %i %s status region\n", pod, podsCount, currIp); # print the pod it ordinal and currIp
        podsCount++; # advance pods count
      }
    }
    print; # add a new line before next region
  }
}

跑步:

awk -f script.awk <(kubectl get nodes --label-columns failure-domain.beta.kubernetes.io/zone) <(kubectl get pods -n app -o wide)

0
投票

@dudi-boy 回答的

awk
脚本可能不是每个人都喜欢!
这是更简单的命令行,仅回答具有区域名称的 pod 列表。
这里对
awk
中的输出列的顺序做出了某些假设,但总体上应该是稳定的。

kubectl get pods -owide | grep <POD_NAME_PATTERN> | awk '{print $1, $7}' | while read pod node; do
 echo -n "$pod "
 kubectl get node $node --label-columns failure-domain.beta.kubernetes.io/zone --no-headers | awk '{print $1, $6}'
done;
© www.soinside.com 2019 - 2024. All rights reserved.