openshift命令以编程/脚本方式捕获POD名称 [英] openshift commands to catch POD name programmatically/scripting

查看:258
本文介绍了openshift命令以编程/脚本方式捕获POD名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开放式班次中有豆荚,并且想要在多个开放式班次应用程序中工作.让我们像下面这样说

I have pods in my open shift and want to work on multiple open shift applications. Lets say like below

sh-4.2 $ oc获取豆荚

sh-4.2$ oc get pods

NAME                                  READY     STATUS      RESTARTS   AGE
jenkins-7fb689fc66-fs2xb              1/1       Running     0          4d
jenkins-disk-check-1587834000         0/1       Completed   0          21h

NAME                                 READY     STATUS    RESTARTS   AGE
jenkins-7fb689fc66-gsz9j              0/1       Running   735        9d
jenkins-disk-check-1587834000    

NAME                                READY     STATUS    RESTARTS   AGE
jenkins-9euygc66-gsz9j               0/1       Running   735        9d

我尝试了以下命令

oc获得豆荚

export POD=$(oc get pods | awk '{print $1}' | grep jenkins*)

我想使用脚本查找以数字"jenkins-7fb689fc66-fs2xb",jenkins-9euygc66-gsz9j等开头的Pod,并且需要忽略磁盘检查Pod.如果我抓住了上面的吊舱,并且需要执行终端并通过编程方式运行一些shell命令.有人可以帮我吗?

I want to find the pods starting with numbers "jenkins-7fb689fc66-fs2xb",jenkins-9euygc66-gsz9j, etc... using scripting and need to ignore disk check pods. If i catch the above pods and need to execute the terminal and run some shell commands via programmatically. Can someone help me on this?

推荐答案

kubectl get(并扩展为oc get)是非常的多功能工具.不幸的是,在网上闲逛了一会儿之后,如果不依靠诸如awkgrep之类的外部工具,您将肯定不能执行Regex. (我知道这不是您所要的完全,而是我想我至少会尝试看看是否有可能.

kubectl get (and by extension oc get) is a very versatile tool. Unfortunately, after looking around online for awhile, you will definitely not be able to do Regex without relying on an external tool like awk or grep. (I know this wasn't exactly what you were asking, but I figured I'd at least try to see if it's possible.

话虽如此,您可以依靠一些技巧来过滤oc get输出,甚至不必引入外部工具(加分点,因为这种过滤是在服务器上发生的,甚至未触及本地工具) ).

With that said, there are a couple of tricks you can rely on to filter your oc get output before you even have to pull in external tools (bonus points because this filtering occurs on the server before it even hits your local tools).

首先建议运行oc get pods --show-labels,因为如果您需要的容器已被正确标记,则可以使用标签选择器来获取所需的容器,例如:

I first recommend running oc get pods --show-labels, because if the pods you need are appropriately labeled, you can use a label selector to get just the pods you want, e.g.:

oc get pods --selector name=jenkins
oc get pods --selector <label_key>=<label_value>

第二,如果您只关心Running窗格(由于disk-check窗格看起来已经是Completed),则可以使用字段选择器,例如:

Second, if you only care about the Running pods (since the disk-check pods look like they're already Completed), you can use a field selector, e.g.:

oc get pods --field-selector status.phase=Running
oc get pods --field-selector <json_path>=<json_value>

最后,如果您要查找的是特定的值,则可以通过指定自定义列,然后在grep上打开该值,将其拉入CLI您关心的价值,例如:

Finally, if there's a specific value that you're after, you can pull that value into the CLI by specifying custom columns, and then greping on the value you care about, e.g.:

oc get pods -o custom-columns=NAME:.metadata.name,TYPES:.status.conditions[*].type | grep "Ready"

最好的事情是,如果您依赖标签选择器和/或字段选择器,则过滤将在服务器端进行,以减少最终将其添加到最终自定义列中的数据,从而使所有工作效率大大提高.

The best thing is, if you rely on the label selector and/or field selector, the filtering occurs server side to cut down on the data that ends up making it to your final custom columns, making everything that much more efficient.

对于您的专用用例,似乎只需使用--field-selector就足够了,因为disk-check吊舱已经是Completed.因此,如果没有关于确切地的进一步信息,Jenkins pod的JSON是如何构造的,这对您来说应该足够了:

For your specific use case, it appears that simply using the --field-selector would be enough, since the disk-check pods are already Completed. So, without further information on exactly how the Jenkins pod's JSON is constructed, this should be good enough for you:

oc get pods --field-selector status.phase=Running

这篇关于openshift命令以编程/脚本方式捕获POD名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆