How do I get the pod ID in Kubernetes?

9/5/2019

I am using Stackdriver Monitoring API to get the metrics related to the containers. The JSON object returned from the API has the following details of the container.

Example:

{
      "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
          }
        }
     ]
}

When I execute kubectl describe pod [pod name], I get none of these information unique to a container. Therefore I am unable to identify the results corresponding to a container.

Therfore, how to I get the pod ID so that I'll be able to identify it?

-- anushiya-thevapalan
google-kubernetes-engine
kubernetes
monitoring
stackdriver

2 Answers

9/5/2019

Use kubectl jsonpath

To get a specific pod's 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⏎

Use kubectl custom-columns

List all PodName along with its UID of a namespace:

$ 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

Use Unix/Linux command grep

You can use kubectl get pods along with grep.

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

9/5/2019

Optionally, you can try this:

$ kubectl get pod -n <namespace> <pod_name> -o jsonpath='{.metadata.uid}'
-- Shudipta Sharma
Source: StackOverflow