从kubectl输出显示失败的豆荚

时间:2019-06-19 18:15:14

标签: kubernetes grep

我想在kubectl上写一个包装器,以仅显示失败的容器,这意味着它应该只显示Ready列值不同的项目(即0/1, 0/2, 1/2, 2/3,等)

$ kubectl get pods --all-namespaces
NAMESPACE       NAME                                        READY     STATUS             RESTARTS   AGE
default         pod-with-sidecar                            1/2       ErrImagePull       0          39s
kube-system     calico-node-chcqq                           2/2       Running            2          7d
kube-system     calico-policy-controller-6449789dd6-z5t5j   1/1       Running            0          7d
kube-system     etcd                                        1/1       Running            0          7d
kube-system     kube-apiserver                              1/1       Running            2          7d
kube-system     kube-controller-manager                     1/1       Running            0          7d
kube-system     kube-dns-5c76d6bddb-8zhmq                   3/3       Running            1          7d
kube-system     kube-proxy-xq8j6                            1/1       Running            0          7d
kube-system     kube-scheduler-                             1/1       Running            0          7d
kube-system     tiller-deploy-5b7cb9cfd7-j725s              1/1       Running            0          7d
my-system       glusterfs-brick-0                           0/2       Pending            0          3m
my-system       sticky-scheduler-6d968f8d74-xvjqn           0/1       ImagePullBackOff   0          4m

所以从上面的输出中,我想打印这些失败的豆荚

NAMESPACE       NAME                                        READY     STATUS             RESTARTS   AGE
default         pod-with-sidecar                            1/2       ErrImagePull       0          4m
my-system       glusterfs-brick-0                           0/2       Pending            0          56s
my-system       sticky-scheduler-6d968f8d74-xvjqn           0/1       ImagePullBackOff   0          8m

这行得通!

$ kubectl get pods --all-namespaces | grep -vE '1/1|2/2|3/3'

NAMESPACE       NAME                                        READY     STATUS             RESTARTS   AGE
default         pod-with-sidecar                            1/2       ErrImagePull       0          4m
my-system       glusterfs-brick-0                           0/2       Pending            0          56s
my-system       sticky-scheduler-6d968f8d74-xvjqn           0/1       ImagePullBackOff   0          8m

但是如果我在2/4,0/4,0/5,0/6列中有Ready等内容,它将无法正常工作,grep -vE '1/1|2/2|3/3'我可以怎么做才能使其在所有此类情况下都能正常工作

参考:https://github.com/kubernetes/kubernetes/issues/49387

2 个答案:

答案 0 :(得分:4)

使用GNU grep:

 | grep -Ev '([0-9]+)/\1'

输出:

NAMESPACE       NAME                                        READY     STATUS             RESTARTS   AGE
default         pod-with-sidecar                            1/2       ErrImagePull       0          39s
my-system       glusterfs-brick-0                           0/2       Pending            0          3m
my-system       sticky-scheduler-6d968f8d74-xvjqn           0/1       ImagePullBackOff   0          4m

答案 1 :(得分:0)

您可以使用$string = "Löic René"; $output = iconv("UTF-8", "ASCII//TRANSLIT", $string); 来做到这一点:

--field-selector

Source