简体   繁体   English

如何合并两个 kubectl 命令的输出

[英]how to merge output from two kubectl commands

Looking for a way to merge / join output from two different kubectl get commands.寻找一种方法来合并/加入来自两个不同 kubectl get 命令的输出。 Output below modified to protect the innocent...修改下面的输出以保护无辜者...

I've confirmed that the json output of the kubectl get pods does not contain the node labels desired to be displayed alongside the pod output.我已经确认 kubectl get pods 的 json 输出不包含希望与 pod 输出一起显示的节点标签。

WORKS - command to get boshid label from the node WORKS - 从节点获取 boshid 标签的命令

$ kubectl get no -L bosh.id -o=custom-columns=NODE:.metadata.name,BOSHID:.metadata.labels."bosh\.id"
NODE                                   BOSHID
89a7a2dc-7468-4163-90fe-f043e408d6af   fec06254-467a-4bdf-983d-f99b7143a667
d4674474-7e0c-49aa-847a-287aa6c1e803   898fff19-3bd5-42d2-8697-0710b0b8baff
fe2be367-a407-4c15-92e7-b0d8918b7e7b   cd9179dd-731a-4d01-8541-4e86355d4457

WORKS - command to get the nodes each pod is on WORKS - 获取每个 Pod 所在节点的命令

$ kubectl get po -n pks-system -o wide
NAME                               READY   STATUS    RESTARTS   AGE   IP             NODE                                   NOMINATED NODE
fluent-bit-4kmzx                   1/1     Running   0          1d    ************   fe2be367-a407-4c15-92e7-b0d8918b7e7b   <none>
fluent-bit-cg26h                   1/1     Running   0          1d    ************   89a7a2dc-7468-4163-90fe-f043e408d6af   <none>
fluent-bit-ddqzh                   1/1     Running   0          1d    ************   d4674474-7e0c-49aa-847a-287aa6c1e803   <none>
sink-controller-57df674b84-mbvcz   1/1     Running   0          1d    ************   89a7a2dc-7468-4163-90fe-f043e408d6af   <none>

DESIRED RESULTS - command that lists the node and boshid each pod is on DESIRED RESULTS - 列出每个 Pod 所在节点和 boshid 的命令

$ kubectl get po (some magic here)
NAME                               READY   STATUS    RESTARTS   AGE   IP             NODE                                   BOSHID
fluent-bit-4kmzx                   1/1     Running   0          1d    ************   fe2be367-a407-4c15-92e7-b0d8918b7e7b   cd9179dd-731a-4d01-8541-4e86355d4457
fluent-bit-cg26h                   1/1     Running   0          1d    ************   89a7a2dc-7468-4163-90fe-f043e408d6af   fec06254-467a-4bdf-983d-f99b7143a667
fluent-bit-ddqzh                   1/1     Running   0          1d    ************   d4674474-7e0c-49aa-847a-287aa6c1e803   898fff19-3bd5-42d2-8697-0710b0b8baff
sink-controller-57df674b84-mbvcz   1/1     Running   0          1d    ************   89a7a2dc-7468-4163-90fe-f043e408d6af   fec06254-467a-4bdf-983d-f99b7143a667

I am afraid that create your desired output in Kubernetes is impossible.恐怕在 Kubernetes 中创建您想要的输出是不可能的。 However it can be done by script (ie python or bash)但是它可以通过脚本(即p​​ython或bash)完成

I am not good at scripting but I was able to create short script in Bash which shows almost desired view.我不擅长编写脚本,但我能够在 Bash 中创建简短的脚本,显示几乎想要的视图。

script.sh脚本文件

#!/bin/bash
pods=$(kubectl get pods -owide | tr -s " " |cut -d " " -f 1-7 | tail -n +2)

nodes=$(kubectl get nodes -L node.sh -o=custom-columns=NODE:.metadata.name,ContainerID:.metadata.annotations."container\.googleapis\.com/instance_id" | tail -n +2)

echo -e "POD                   READY STATUS RESTARTS AGE IP NODE                              BoshID"
echo "$pods" | while read LINE
  do
    nodeName=$(echo "$LINE" | cut -d ' ' -f 7)
    goutput=$(echo "$nodes" | grep "$nodeName" | tr -s ' '| cut -d ' ' -f 2)
    echo "$LINE $goutput"
  done

My output我的输出

$ ./skrypt.sh
POD                   READY STATUS RESTARTS AGE IP NODE                              ContainerID
nginx-7b9899ff5f-6lk87 1/1 Running 0 16h 10.48.4.3 gke-stc-default-pool-ba33922c-fsf3 7950529300866259659
nginx-7b9899ff5f-cwwrp 1/1 Running 0 16h 10.48.4.2 gke-stc-default-pool-ba33922c-fsf3 7950529300866259659
nginx-7b9899ff5f-x5jwv 1/1 Running 0 17m 10.48.6.3 gke-stc-default-pool-ba33922c-kzcx 8511204661082446539

In your case script should looks like:在您的情况下,脚本应如下所示:

#!/bin/bash
pods=$(kubectl get pods -n pks-system -owide | tr -s " " |cut -d " " -f 1-7 | tail -n +2)

nodes=$(kubectl get nodes -L node.sh -o=custom-columns=NODE:.metadata.name,BOSHID:.metadata.labels."bosh\.id" | tail -n +2)

echo -e "POD                   READY STATUS RESTARTS AGE IP NODE                              BOSHID"
echo "$pods" | while read LINE
  do
    nodeName=$(echo "$LINE" | cut -d ' ' -f 7)
    goutput=$(echo "$nodes" | grep "$nodeName" | tr -s ' '| cut -d ' ' -f 2)
    echo "$LINE $goutput"
  done

Came back to this and decided to go with a function and using the join command.回到这个并决定使用一个函数并使用 join 命令。 Added the following to my ~/.bashrc -将以下内容添加到我的 ~/.bashrc -

function pks-po() {
  # using namespace input, joins kubectl get pods output
  #   +NAME  +READY  +STATUS  +RESTARTS  +AGE  -IP  +NODE*  -NOMINATED NODE  -READINESS GATES
  # with kubectl get nodes output
  #   -NAME*  -STATUS  -ROLES  -AGE  -VERSION  +BOSH.ID  +SPEC.IP  +ZONE  +PKS.UUID
  ns=${1:-default}
  kubectl get pods -n ${ns} --no-headers -o wide|sort -nk 7b > /tmp/k1.txt
  kubectl get nodes --no-headers -L bosh.id,spec.ip,failure-domain.beta.kubernetes.io/zone,pks-system/cluster.uuid|sort -nk 1b > /tmp/k2.txt
  join -1 7 -2 1 -o 1.1 1.2 1.3 1.4 1.5 1.7 2.6 2.7 2.8 2.9 /tmp/k{1,2}.txt | \
    sed -e '1i\NAME READY STATUS RESTARTS AGE NODE BOSH.ID NODE.IP ZONE PKS.UUID' | \
    column -t
}

Now I can just run the following:现在我可以运行以下命令:

pks-po default

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM