gpt4 book ai didi

json - Kubernetes (kubectl) 获取正在运行的 Pod

转载 作者:行者123 更新时间:2023-12-02 20:56:20 27 4
gpt4 key购买 nike

我正在尝试从部署中获取状态为正在运行第一个 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。

编辑2:已解决 - 请参阅评论

最佳答案

从 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 的命令也接受部署或服务并自动选择相应的荚。引用文档:

$ echo exec logs port-forward | xargs -n1 kubectl help | grep -C1 'service\|deploy\|job'

# 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 first container of a job named hello
kubectl logs job/hello

# 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”,其主要标准是状态为“正在运行”(请参阅​​ source)。

关于json - Kubernetes (kubectl) 获取正在运行的 Pod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46474972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com