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

时间:2019-04-05 15:16:41

标签: linux kubernetes kubectl

寻找一种方法来合并/合并来自两个不同的kubectl get命令的输出。下面的输出已修改,以保护无辜...

我已经确认kubectl get pod的json输出不包含希望在pod输出旁边显示的节点标签。

WORKS-从节点获取boshid标签的命令

 #include <stdio.h>
int function1(int *m, int n)
{
    *m = *m + n;
    return(*m);
}
int function2(int n, int *m)
{
    n = *m + 2;
    return(n);
}
int main()
{
    int x = 1, y = 3;
    int *xptr = &x,*yptr = &y;      
    x = 1; y = 3;
    y = function1(xptr,x);
    printf("x = %d, Y = %d\n",x,y);//x=2 but why? shouldn't it be x=1? y=2          
    x = 1; y = 3;
    x = function1(yptr,function2(2,yptr));
    printf("x = %d, y = %d\n",x,y);//x=8 y=8 but why? shouldn't y=3?        
return 0;
}

WORKS-获取每个Pod所在节点的命令

$ 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

期望结果-列出节点并删除每个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>

2 个答案:

答案 0 :(得分:0)

恐怕无法在Kubernetes中创建所需的输出。但是可以通过脚本(例如python或bash)完成

我不擅长编写脚本,但是我能够在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

我的输出

$ ./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

在您的情况下,脚本应如下所示:

#!/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

答案 1 :(得分:0)

回到这一点,并决定使用一个函数并使用join命令。 将以下内容添加到我的〜/ .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}
  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 \
    <(kubectl get pods -n ${ns} --no-headers -o wide|sort -nk 7) \
    <(kubectl get nodes --no-headers -L bosh.id,spec.ip,failure-domain.beta.kubernetes.io/zone,pks-system/cluster.uuid|sort -n) | \
    sed -e '1i\NAME READY STATUS RESTARTS AGE NODE BOSH.ID NODE.IP ZONE PKS.UUID' | \
    column -t
}

现在我可以运行以下命令:

pks-po default