繁体   English   中英

Kubernetes (kubectl) 让 pod 运行起来

[英]Kubernetes (kubectl) get running pods

我正在尝试从处于运行状态的部署(按标签过滤)中获取第一个pod - 目前我只能实现以下目标,这只会给我部署中的第一个 pod(按标签过滤) - 并且不确定一个正在运行的 pod,例如它也可能是一个终止的:

kubectl get pod -l "app=myapp" -l "tier=webserver" -l "namespace=test" 
                -o jsonpath="{.items[0].metadata.name}"

怎么可能

a) 仅获取“RUNNING” pod 的 pod 列表和(在此处或在 google 上找不到任何内容)

b) 从该列表中选择第一个。 (这就是我目前正在做的)

问候

更新:我已经尝试过之前评论中发布的链接,内容如下:

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'

结果是 4x "null" - 有 4 个正在运行的 pod。

Edit2:已解决 - 见评论

从 kubectl 1.9 开始,您可以选择传递--field-selector参数(请参阅https://github.com/kubernetes/kubernetes/pull/50140 )。 例如

kubectl get pod -l app=yourapp --field-selector=status.phase==Running -o jsonpath="{.items[0].metadata.name}"

但是请注意,对于不太旧的kubectl版本,查找正在运行的 pod 的许多原因是无声的,因为许多期望 pod 的命令也接受部署或服务并自动选择相应的 pod。 从文档中引用:

$ 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

(注意logs也接受服务,即使帮助中省略了示例。)

选择算法倾向于“活动 pod”,其主要标准的状态为“正在运行”(请参阅​​ https://github.com/kubernetes/kubectl/blob/2d67b5a3674c9c661bc03bb96cb2c0841ccee90b/pkg/polymorphichelpers/attachablepodforobject.go#L51 )。

暂无
暂无

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

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