6

I have this JSON output from Kubernetes and 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")])]}

dejanualex
  • 3,872
  • 6
  • 22
  • 37
bherto39
  • 1,516
  • 3
  • 14
  • 29

10 Answers10

19

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?

Javier Salmeron
  • 8,365
  • 2
  • 28
  • 23
  • 3
    This output shows the desired state... it is showing "Running" but the get pod is showing "CrashBackOff" – Marcello DeSales Aug 30 '18 at 08:07
  • @MarcellodeSales: how did you resolve this issue, I am also running into same problem and was looking for some solution to get correct status for a specific container – lucky Nov 23 '18 at 07:07
  • `$ kubectl get po -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS_PHASE:.status.phase,STATUS_REASON:.status.reason -n kube-system kube-dns-b4f5c58c7-9d4cb NAMESPACE NAME STATUS_PHASE STATUS_REASON kube-system kube-dns-b4f5c58c7-9d4cb Running ` The `.status.reason` gives more info on `Failed` pods. – apa64 Jan 03 '22 at 12:02
6

Following Command works for me, please try this out.

kubectl get pods <pod-name> --no-headers -o custom-columns=":status.phase"
Shreyas
  • 549
  • 5
  • 3
3

In openshift i used below it works very well

oc get pods -l deploymentconfig='db' -o jsonpath='{.items[*].status.containerStatuses[0].ready}'
ImranRazaKhan
  • 1,955
  • 5
  • 33
  • 74
2

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.

SoJeN
  • 429
  • 5
  • 7
  • this works good.. but can this check be relied for deployment as description is an open text which might change – lucky Nov 23 '18 at 07:10
2

My 2 cents on the subject, don't mix POD status with Container status (it's true that they're correlated).

  • Pods follow a defined lifecycle, starting in the Pending phase, moving through Running (if at least one of its primary containers starts OK), and then through either the Succeeded or Failed.

You can check a Pod's status (which is a PodStatus object) using:

# status for pods from all namespaces use -n instead of -A for the desired namespace
kubectl get po -A -o jsonpath='{.items[*].status}'

# it's rather verbose so you can pipe it to jq (if installed)
kubectl get po -A -o jsonpath='{.items[*].status}' | jq .
  • Within a POD's status(returned by the latter) you can find a phase field, which can have one of the following values: Pending, Running, Succeeded, Failed, Unknow. You can check the Pods which phase is different than Running:
# check phase for pods in all namespaces 
kubectl get po -A --field-selector=status.phase!=Running
  • When a Pod is being deleted, it can be shown as Terminating by some kubectl commands. This Terminating status is not one of the Pod phases.

  • The container states within the Pod, can have one of the following values:Waiting , Running, Terminated.

# check container related info use -n instead of -A for the desired namespace
kubectl get po -A -o jsonpath="{.items[*].status.containerStatuses[*].state}"
dejanualex
  • 3,872
  • 6
  • 22
  • 37
1

For pod status, the one and only answer (a pod can be Running but not yet ready!):

kubectl get pods -l <key=val> -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}'

For individual container statuses (need to be "true"):

kubectl get pod <pod_name> --output="jsonpath={.status.containerStatuses[*].ready}" | cut -d' ' -f2
seebi
  • 488
  • 5
  • 9
1
kubectl get pods -n deployNamespace -ojson | jq '.spec.status.phase'

You will have jq installed.

4t8dds
  • 565
  • 7
  • 19
  • That doesn't work for me, but `jq '.items[].status.phase'` or `jq '.items[].status.containerStatuses[].state'` look like they work. – An̲̳̳drew May 19 '22 at 16:17
0

status.phase will return pod status not the container status . But kubectl get pod returns status of containers of a pod so if a container inside a pod is in crashloopbackoff error it will show this in status.

a possible solution to this is to check container status as well. I have customised @javier comment to get this . I hope it helps

kubectl get pods -o jsonpath="Name: {.items[* ].metadata.name} Phase : {.items[* ].status.phase} ContainerStatus: {.items[ *].status.containerStatuses[ *].state}"

shubham_asati
  • 623
  • 5
  • 14
0

Usually a pod name is followed by unique hash/id. In order to target specifically on name, use --selector flag:

kubectl get pods -n deployNamespace --selector=name=yourAppName -o jsonpath="{.items[*].status.phase}"

This example will output only status phase.

Nik
  • 418
  • 4
  • 10
0

I had a same case where I had some value about pod specific and I wrote small bash script to do that. You can obtain it with this command:

kubectl get pods -n ${NAMESPACES} | grep -dw ${POD_SPECIFIC_VALUE} | cut -w -f3
Selcuk
  • 31
  • 2