[英]Kubernetes (kubectl) get running pods
I am trying to get the first pod from within a deployment (filtered by labels) with status running - currently I could only achieve the following, which will just give me the first pod within a deployment (filtered by labels) - and not for sure a running pod, eg it might also be a terminating one:我正在尝试从处于运行状态的部署(按标签过滤)中获取第一个pod - 目前我只能实现以下目标,这只会给我部署中的第一个 pod(按标签过滤) - 并且不确定一个正在运行的 pod,例如它也可能是一个终止的:
kubectl get pod -l "app=myapp" -l "tier=webserver" -l "namespace=test"
-o jsonpath="{.items[0].metadata.name}"
How is it possible to怎么可能
a) get only a pod list of "RUNNING" pods and (couldn't find anything here or on google) a) 仅获取“RUNNING” pod 的 pod 列表和(在此处或在 google 上找不到任何内容)
b) select the first one from that list. b) 从该列表中选择第一个。 (thats what I'm currently doing)
(这就是我目前正在做的)
Regards问候
Update: I already tried the link posted in the comments earlier with the following:更新:我已经尝试过之前评论中发布的链接,内容如下:
kubectl get pod -l "app=ms-bp" -l "tier=webserver" -l "namespace=test"
-o json | jq -r '.items[] | select(.status.phase = "Running") | .items[0].metadata.name'
the result is 4x "null" - there are 4 running pods.结果是 4x "null" - 有 4 个正在运行的 pod。
Edit2: Resolved - see comments Edit2:已解决 - 见评论
Since kubectl 1.9 you have the option to pass a --field-selector
argument (see https://github.com/kubernetes/kubernetes/pull/50140 ).从 kubectl 1.9 开始,您可以选择传递
--field-selector
参数(请参阅https://github.com/kubernetes/kubernetes/pull/50140 )。 Eg例如
kubectl get pod -l app=yourapp --field-selector=status.phase==Running -o jsonpath="{.items[0].metadata.name}"
Note however that for not too old kubectl
versions many reasons to find a running pod are mute, because a lot of commands which expect a pod also accept a deployment or service and automatically select a corresponding pod.但是请注意,对于不太旧的
kubectl
版本,查找正在运行的 pod 的许多原因是无声的,因为许多期望 pod 的命令也接受部署或服务并自动选择相应的 pod。 To quote from the documentation:从文档中引用:
$ echo exec logs port-forward | xargs -n1 kubectl help | grep -C1 'service\|deploy'
# Get output from running 'date' command from the first pod of the deployment mydeployment, using the first container by default
kubectl exec deploy/mydeployment -- date
# Get output from running 'date' command from the first pod of the service myservice, using the first container by default
kubectl exec svc/myservice -- date
--
# Return snapshot logs from container nginx-1 of a deployment named nginx
kubectl logs deployment/nginx -c nginx-1
--
Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.
--
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment
kubectl port-forward deployment/mydeployment 5000 6000
# Listen on port 8443 locally, forwarding to the targetPort of the service's port named "https" in a pod selected by the service
kubectl port-forward service/myservice 8443:https
(Note logs
also accepts a service, even though an example is omitted in the help.) (注意
logs
也接受服务,即使帮助中省略了示例。)
The selection algorithm favors "active pods" for which a main criteria is having a status of "Running" (see https://github.com/kubernetes/kubectl/blob/2d67b5a3674c9c661bc03bb96cb2c0841ccee90b/pkg/polymorphichelpers/attachablepodforobject.go#L51 ).选择算法倾向于“活动 pod”,其主要标准的状态为“正在运行”(请参阅 https://github.com/kubernetes/kubectl/blob/2d67b5a3674c9c661bc03bb96cb2c0841ccee90b/pkg/polymorphichelpers/attachablepodforobject.go#L51 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.