gpt4 book ai didi

kubernetes - 如何在Kubernetes中获取 pods ID?

转载 作者:行者123 更新时间:2023-12-03 11:17:32 42 4
gpt4 key购买 nike

我正在使用Stackdriver Monitoring API来获取与容器相关的指标。从API返回的JSON对象具有以下容器详细信息。

例子:

{
"metric": {
"type": "container.googleapis.com/container/cpu/utilization"
},
"resource": {
"type": "gke_container",
"labels": {
"zone": "us-central1-a",
"pod_id": "1138528c-c36e-11e9-a1a7-42010a800198",
"project_id": "auto-scaling-springboot",
"cluster_name": "load-test",
"container_name": "",
"namespace_id": "f0965889-c36d-11e9-9e00-42010a800198",
"instance_id": "3962380509873542383"
}
},
"metricKind": "GAUGE",
"valueType": "DOUBLE",
"points": [
{
"interval": {
"startTime": "2019-09-04T04:00:00Z",
"endTime": "2019-09-04T04:00:00Z"
},
"value": {
"doubleValue": 0.050707947222229495
}
}
]
}

当我执行 kubectl describe pod [pod name]时,我没有得到容器独有的这些信息。因此,我无法识别与容器相对应的结果。

因此,如何获取 pods ID,以便能够识别它?

最佳答案

使用kubectl jsonpath
要获取特定Pod的UID,请执行以下操作:

$ kubectl get pods -n <namespace> <pod-name> -o jsonpath='{.metadata.uid}'

$ kubectl get pods -n kube-system kubedb-66f78 -o jsonpath='{.metadata.uid}'
275ecb36-5aa8-4c2a-9c47-d8bb681b9aff⏎

使用kubectl custom-columns
列出所有PodName及其 namespace 的UID:

$ kubectl get pods -n <namespace> -o custom-columns=PodName:.metadata.name,PodUID:.metadata.uid

$ kubectl get pods -n kube-system -o custom-columns=PodName:.metadata.name,PodUID:.metadata.uid
PodName PodUID
coredns-6955765f44-8kp9t 0ae5c03d-5fb3-4eb9-9de8-2bd4b51606ba
coredns-6955765f44-ccqgg 6aaa09a1-241a-4013-b706-fe80ae371206
etcd-kind-control-plane c7304563-95a8-4428-881e-422ce3e073e7
kindnet-jgb95 f906a249-ab9d-4180-9afa-4075e2058ac7
kube-apiserver-kind-control-plane 971165e8-6c2e-4f99-8368-7802c1e55e60
kube-controller-manager-kind-control-plane a0dce3a7-a734-485d-bfee-8ac3de6bb486
kube-proxy-27wgd d900c0b2-dc21-46b5-a97e-f30e830aa9be
kube-scheduler-kind-control-plane 9c6f2399-4986-4259-9cd7-875eff1d7198

使用Unix/Linux命令 grep
您可以将 kubectl get podsgrep一起使用。

$ kubectl get pods -n <namespace> <pod-name> -o yaml | grep uid
uid: bcfbdfb5-ce0f-11e9-b83e-080027d4916d

关于kubernetes - 如何在Kubernetes中获取 pods ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57799684/

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