Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get status of a particular pod or container : kubectl get pods using jsonpath

I have this json output from kubernetes got it from kubectl get pods -o jsonpath={.items[*]}

    <json>

    {
        "apiVersion": "v1",
        "items": [
            {
                "apiVersion": "v1",
                "kind": "Pod",
                "metadata": {
                    "annotations": {
                        "kubernetes.io/created-by": "{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"some-appdeployment-1780875823\",\"uid\":\"7180b966-7ec1-11e7-9981-305a3ae15081\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"16711638\"}}\n"
                    },
                    "creationTimestamp": "2017-08-11T18:18:15Z",
                    "generateName": "some-appdeployment-1780875823-",
                    "labels": {
                        "app": "myapp-auth-some-app",
                        "pod-template-hash": "1780875823"
                    },
                    "name": "some-appdeployment-1780875823-59p06",
                    "namespace": "default",
                    "ownerReferences": [
                        {
                            "apiVersion": "extensions/v1beta1",
                            "controller": true,
                            "kind": "ReplicaSet",
                            "name": "some-appdeployment-1780875823",
                            "uid": "7180b966-7ec1-11e7-9981-305a3ae15081"
                        }
                    ],
                    "resourceVersion": "16711688",
                    "selfLink": "/api/v1/namespaces/default/pods/some-appdeployment-1780875823-59p06",
                    "uid": "71829a96-7ec1-11e7-9981-305a3ae15081"
                },
                "spec": {
                    "containers": [
                        {
                            "env": [
                                {
                                    "name": "PROFILE",
                                    "value": "dev"
                                }
                            ],
                            "image": "dockerrepo/myapp-auth-some-app:6",
                            "imagePullPolicy": "Always",
                            "name": "myapp-auth-some-app",
                            "ports": [
                                {
                                    "containerPort": 8443,
                                    "protocol": "TCP"
                                }
                            ],
                            "resources": {},
                            "terminationMessagePath": "/dev/termination-log"
                        }
                    ],
                    "dnsPolicy": "ClusterFirst",
                    "imagePullSecrets": [
                        {
                            "name": "myregistrykey"
                        }
                    ],
                    "nodeName": "kubernetes-worker3",
                    "nodeSelector": {
                        "worker": "kubernetes-worker3"
                    },
                    "restartPolicy": "Always",
                    "securityContext": {},
                    "terminationGracePeriodSeconds": 30
                },
                "status": {
                    "conditions": [
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-11T18:18:15Z",
                            "status": "True",
                            "type": "Initialized"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-11T18:18:23Z",
                            "status": "True",
                            "type": "Ready"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-11T18:18:15Z",
                            "status": "True",
                            "type": "PodScheduled"
                        }
                    ],
                    "containerStatuses": [
                        {
                            "containerID": "docker://12340987125",
                            "image": "dockerrepo/myapp-auth-some-app:6",
                            "imageID": "somevaluehere://value/myapp-auth-some-app@sha256:bb32ee950fdd5243749218710d9771e5c851e8a14ebd82abf12beeffa05fcb26",
                            "lastState": {},
                            "name": "myapp-auth-some-app",
                            "ready": true,
                            "restartCount": 0,
                            "state": {
                                "running": {
                                    "startedAt": "2017-08-11T18:18:23Z"
                                }
                            }
                        }
                    ],
                    "hostIP": "172.25.1.25",
                    "phase": "Running",
                    "podIP": "172.30.7.7",
                    "startTime": "2017-08-11T18:18:15Z"
                }
            },
            {
                "apiVersion": "v1",
                "kind": "Pod",
                "metadata": {
                    "annotations": {
                        "kubernetes.io/created-by": "{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"default-http-backend-2657704409\",\"uid\":\"09a0779c-61b4-11e7-9981-305a3ae15081\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"12122741\"}}\n"
                    },
                    "creationTimestamp": "2017-07-05T18:59:14Z",
                    "generateName": "default-http-backend-2657704409-",
                    "labels": {
                        "k8s-app": "default-http-backend",
                        "pod-template-hash": "2657704409"
                    },
                    "name": "default-http-backend-2657704409-dk898",
                    "namespace": "default",
                    "ownerReferences": [
                        {
                            "apiVersion": "extensions/v1beta1",
                            "controller": true,
                            "kind": "ReplicaSet",
                            "name": "default-http-backend-2657704409",
                            "uid": "09a0779c-61b4-11e7-9981-305a3ae15081"
                        }
                    ],
                    "resourceVersion": "12122766",
                    "selfLink": "/api/v1/namespaces/default/pods/default-http-backend-2657704409-dk898",
                    "uid": "09a22104-61b4-11e7-9981-305a3ae15081"
                },
                "spec": {
                    "containers": [
                        {
                            "image": "gcr.io/google_containers/defaultbackend:1.0",
                            "imagePullPolicy": "IfNotPresent",
                            "livenessProbe": {
                                "failureThreshold": 3,
                                "httpGet": {
                                    "path": "/healthz",
                                    "port": 8080,
                                    "scheme": "HTTP"
                                },
                                "initialDelaySeconds": 30,
                                "periodSeconds": 10,
                                "successThreshold": 1,
                                "timeoutSeconds": 5
                            },
                            "name": "default-http-backend",
                            "ports": [
                                {
                                    "containerPort": 8080,
                                    "protocol": "TCP"
                                }
                            ],
                            "resources": {
                                "limits": {
                                    "cpu": "10m",
                                    "memory": "20Mi"
                                },
                                "requests": {
                                    "cpu": "10m",
                                    "memory": "20Mi"
                                }
                            },
                            "terminationMessagePath": "/dev/termination-log"
                        }
                    ],
                    "dnsPolicy": "ClusterFirst",
                    "nodeName": "kubernetes-worker3",
                    "restartPolicy": "Always",
                    "securityContext": {},
                    "terminationGracePeriodSeconds": 60
                },
                "status": {
                    "conditions": [
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-07-05T18:59:14Z",
                            "status": "True",
                            "type": "Initialized"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-07-05T18:59:17Z",
                            "status": "True",
                            "type": "Ready"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-07-05T18:59:14Z",
                            "status": "True",
                            "type": "PodScheduled"
                        }
                    ],
                    "containerStatuses": [
                        {
                            "containerID": "docker://99d9789f43678e73c8d1a6b18bb0fc4990e78e018581ba33daa4365773933f61",
                            "image": "gcr.io/google_containers/defaultbackend:1.0",
                            "imageID": "docker-pullable://gcr.io/google_containers/defaultbackend@sha256:ee3aa1187023d0197e3277833f19d9ef7df26cee805fef32663e06c7412239f9",
                            "lastState": {},
                            "name": "default-http-backend",
                            "ready": true,
                            "restartCount": 0,
                            "state": {
                                "running": {
                                    "startedAt": "2017-07-05T18:59:17Z"
                                }
                            }
                        }
                    ],
                    "hostIP": "172.25.1.25",
                    "phase": "Running",
                    "podIP": "172.30.7.4",
                    "startTime": "2017-07-05T18:59:14Z"
                }
            },
            {
                "apiVersion": "v1",
                "kind": "Pod",
                "metadata": {
                    "creationTimestamp": "2017-06-14T13:30:00Z",
                    "labels": {
                        "context": "componentser-pod",
                        "name": "elk-stack"
                    },
                    "name": "componentser",
                    "namespace": "default",
                    "resourceVersion": "9725589",
                    "selfLink": "/api/v1/namespaces/default/pods/componentser",
                    "uid": "90bde536-5105-11e7-9223-305a3ae1508c"
                },
                "spec": {
                    "containers": [
                        {
                            "env": [
                                {
                                    "name": "ES_JAVA_OPTS",
                                    "value": "-Xms512m -Xmx512m"
                                }
                            ],
                            "image": "docker.elastic.co/componentser/componentser:5.3.2",
                            "imagePullPolicy": "IfNotPresent",
                            "name": "componentser-pod",
                            "ports": [
                                {
                                    "containerPort": 9200,
                                    "protocol": "TCP"
                                }
                            ],
                            "resources": {},
                            "terminationMessagePath": "/dev/termination-log",
                            "volumeMounts": [
                                {
                                    "mountPath": "/usr/share/componentser/data",
                                    "name": "pv-elk"
                                }
                            ]
                        }
                    ],
                    "dnsPolicy": "ClusterFirst",
                    "nodeName": "kubernetes-worker2",
                    "nodeSelector": {
                        "worker": "kubernetes-worker2"
                    },
                    "restartPolicy": "Always",
                    "securityContext": {},
                    "terminationGracePeriodSeconds": 30,
                    "volumes": [
                        {
                            "name": "pv-elk",
                            "persistentVolumeClaim": {
                                "claimName": "pv-elk-claim"
                            }
                        }
                    ]
                },
                "status": {
                    "conditions": [
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-06-14T13:30:00Z",
                            "status": "True",
                            "type": "Initialized"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-06-14T13:30:02Z",
                            "status": "True",
                            "type": "Ready"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-06-14T13:30:00Z",
                            "status": "True",
                            "type": "PodScheduled"
                        }
                    ],
                    "containerStatuses": [
                        {
                            "containerID": "docker://da049a5904af7d1150779f4de8a77f62424da4322714a47d57b6bdfd37aa7c41",
                            "image": "docker.elastic.co/componentser/componentser:5.3.2",
                            "imageID": "docker-pullable://docker.elastic.co/componentser/componentser@sha256:63b0d5ec541623694840e64337a8fa6b52141b06a16b69dc3c99c790fa755bd2",
                            "lastState": {},
                            "name": "componentser-pod",
                            "ready": true,
                            "restartCount": 0,
                            "state": {
                                "running": {
                                    "startedAt": "2017-06-14T13:30:02Z"
                                }
                            }
                        }
                    ],
                    "hostIP": "172.25.1.24",
                    "phase": "Running",
                    "podIP": "172.30.21.5",
                    "startTime": "2017-06-14T13:30:00Z"
                }
            },
            {
                "apiVersion": "v1",
                "kind": "Pod",
                "metadata": {
                    "annotations": {
                        "kubernetes.io/created-by": "{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"frontendsome-app-me-deployment-1015736808\",\"uid\":\"9cb0867e-8681-11e7-9981-305a3ae15081\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"17949552\"}}\n"
                    },
                    "creationTimestamp": "2017-08-21T15:01:29Z",
                    "generateName": "frontendsome-app-me-deployment-1015736808-",
                    "labels": {
                        "app": "some-app-name",
                        "pod-template-hash": "1015736808"
                    },
                    "name": "frontendsome-app-me-deployment-1015736808-t14z3",
                    "namespace": "default",
                    "ownerReferences": [
                        {
                            "apiVersion": "extensions/v1beta1",
                            "controller": true,
                            "kind": "ReplicaSet",
                            "name": "frontendsome-app-me-deployment-1015736808",
                            "uid": "9cb0867e-8681-11e7-9981-305a3ae15081"
                        }
                    ],
                    "resourceVersion": "17949586",
                    "selfLink": "/api/v1/namespaces/default/pods/frontendsome-app-me-deployment-1015736808-t14z3",
                    "uid": "9cb1d88b-8681-11e7-9981-305a3ae15081"
                },
                "spec": {
                    "containers": [
                        {
                            "image": "dockerrepo/some-app-name:0.0.2",
                            "imagePullPolicy": "IfNotPresent",
                            "name": "some-app-name",
                            "ports": [
                                {
                                    "containerPort": 8443,
                                    "protocol": "TCP"
                                }
                            ],
                            "resources": {},
                            "terminationMessagePath": "/dev/termination-log"
                        }
                    ],
                    "dnsPolicy": "ClusterFirst",
                    "imagePullSecrets": [
                        {
                            "name": "myregistrykey"
                        }
                    ],
                    "nodeName": "kubernetes-worker1",
                    "nodeSelector": {
                        "worker": "kubernetes-worker1"
                    },
                    "restartPolicy": "Always",
                    "securityContext": {},
                    "terminationGracePeriodSeconds": 30
                },
                "status": {
                    "conditions": [
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-21T15:01:29Z",
                            "status": "True",
                            "type": "Initialized"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-21T15:01:31Z",
                            "status": "True",
                            "type": "Ready"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-21T15:01:29Z",
                            "status": "True",
                            "type": "PodScheduled"
                        }
                    ],
                    "containerStatuses": [
                        {
                            "containerID": "docker://477a90a685ba4944733f85c6a2d19114dca13de5be85ee270273abe16cf14a40",
                            "image": "dockerrepo/some-app-name:0.0.2",
                            "imageID": "somevaluehere://value/some-app-name@sha256:5c0f8c6d75ff2035028c02ab0a200f7cb93eb1d392ba06c1e147eca2d44164be",
                            "lastState": {},
                            "name": "some-app-name",
                            "ready": true,
                            "restartCount": 0,
                            "state": {
                                "running": {
                                    "startedAt": "2017-08-21T15:01:30Z"
                                }
                            }
                        }
                    ],
                    "hostIP": "172.25.1.23",
                    "phase": "Running",
                    "podIP": "172.30.51.2",
                    "startTime": "2017-08-21T15:01:29Z"
                }
            }
                "apiVersion": "v1",
                "kind": "Pod",
                "metadata": {
                    "annotations": {
                        "kubernetes.io/created-by": "{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"zookeeper-deployment-3568946791\",\"uid\":\"171870c0-7d17-11e7-9981-305a3ae15081\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"16447678\"}}\n"
                    },
                    "creationTimestamp": "2017-08-09T15:26:18Z",
                    "generateName": "zookeeper-deployment-3568946791-",
                    "labels": {
                        "app": "zookeeper",
                        "pod-template-hash": "3568946791"
                    },
                    "name": "zookeeper-deployment-3568946791-rf33w",
                    "namespace": "default",
                    "ownerReferences": [
                        {
                            "apiVersion": "extensions/v1beta1",
                            "controller": true,
                            "kind": "ReplicaSet",
                            "name": "zookeeper-deployment-3568946791",
                            "uid": "171870c0-7d17-11e7-9981-305a3ae15081"
                        }
                    ],
                    "resourceVersion": "16447717",
                    "selfLink": "/api/v1/namespaces/default/pods/zookeeper-deployment-3568946791-rf33w",
                    "uid": "17196555-7d17-11e7-9981-305a3ae15081"
                },
                "spec": {
                    "containers": [
                        {
                            "image": "jplock/zookeeper",
                            "imagePullPolicy": "IfNotPresent",
                            "name": "zookeeper",
                            "ports": [
                                {
                                    "containerPort": 2181,
                                    "protocol": "TCP"
                                }
                            ],
                            "resources": {},
                            "terminationMessagePath": "/dev/termination-log"
                        }
                    ],
                    "dnsPolicy": "ClusterFirst",
                    "nodeName": "kubernetes-worker3",
                    "nodeSelector": {
                        "worker": "kubernetes-worker3"
                    },
                    "restartPolicy": "Always",
                    "securityContext": {},
                    "terminationGracePeriodSeconds": 30
                },
                "status": {
                    "conditions": [
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-09T15:26:18Z",
                            "status": "True",
                            "type": "Initialized"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-09T15:26:34Z",
                            "status": "True",
                            "type": "Ready"
                        },
                        {
                            "lastProbeTime": null,
                            "lastTransitionTime": "2017-08-09T15:26:18Z",
                            "status": "True",
                            "type": "PodScheduled"
                        }
                    ],
                    "containerStatuses": [
                        {
                            "containerID": "docker://251cb89e5bcb5e4547d16b64635bfc1b9c54042203211456cf6d16b214e36b26",
                            "image": "jplock/zookeeper",
                            "imageID": "docker-pullable://docker.io/jplock/zookeeper@sha256:b21146163d49d1a5e0cf1e4eb39a39c892077a22fee330b20369b2984b41c9f1",
                            "lastState": {},
                            "name": "zookeeper",
                            "ready": true,
                            "restartCount": 0,
                            "state": {
                                "running": {
                                    "startedAt": "2017-08-09T15:26:33Z"
                                }
                            }
                        }
                    ],
                    "hostIP": "172.25.1.25",
                    "phase": "Running",
                    "podIP": "172.30.7.5",
                    "startTime": "2017-08-09T15:26:18Z"
                }
            }
        ],
        "kind": "List",
        "metadata": {
            "resourceVersion": "",
            "selfLink": ""
        }
    }


    </json>

<!-- end snippet -->

I'm trying to query the following using kubectl get pods -o jsonpath=$JSONPATH:

  • Podname: (like)some-appdeployment-*
  • Status: Running

I've tried the following jsonpath:

JSONPATH={.items[*].status.containerStatuses[?(@.name==\"some-appdeployment\")].name} {\"\t\"} ready: {.items[*].spec.containers[?(@.name==\"some-appdeployment\")].phase} {\"\n\"}

also this one: is it possible to get items with name like then do a grep '[some-appdeployment]' to filter only those containers

JSONPATH={.items[?(@.spec.containers[?(@.name==\"some-appdeployment\")])]}

like image 661
bherto39 Avatar asked Aug 24 '17 02:08

bherto39


People also ask

How do you get the status of a pod in Kubernetes?

To check if the status of your pods is healthy, the easiest way is to run the kubectl get pods command. After that, you can use kubectl describe and kubectl logs to obtain more detailed information.

How do you check pods in kubectl?

Run the command kubectl describe pod if you want to see the output from a specific pod.

How do you check which pods are running on which nodes?

To find the cluster IP address of a Kubernetes pod, use the kubectl get pod command on your local machine, with the option -o wide . This option will list more information, including the node the pod resides on, and the pod's cluster IP. The IP column will contain the internal cluster IP address for each pod.


3 Answers

If you want that output, my advice would be to execute the command like this:

  kubectl get pods -n kube-system some-appdeployment -o jsonpath="Name: {.metadata.name} Status: {.status.phase}"

Would this work for your case?

like image 143
Javier Salmeron Avatar answered Sep 27 '22 19:09

Javier Salmeron


Following Command works for me, please try this out.

kubectl get pods <pod-name> --no-headers -o custom-columns=":status.phase"
like image 26
Shreyas Avatar answered Sep 27 '22 19:09

Shreyas


A bit more ugly, but at least it's not the desired (as done by Javier Salmeron) but the true status of the Pod:

kubectl -n MYNAMESPACE describe pods MYPOD | grep ^Status: | head -1 | awk '{print $2}' | tr -d '\n'

Explanation: We do a describe on the Pod, then grep the row starting with Status:, strip to the first row, select the value of the 2nd column, trail the linebreak. What remains is the status value.

like image 28
SoJeN Avatar answered Sep 27 '22 17:09

SoJeN