如何获取特定pod或容器的状态:kubectl使用jsonpath获取pod

时间:2017-08-24 02:19:38

标签: kubernetes jsonpath kubectl

我有来自kubernetes的这个json输出从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>

我试图使用kubectl get pods查询以下内容-o jsonpath = $ JSONPATH

Podname :(赞)some-appdeployment- * 状态:正在运行

我尝试了以下jsonpath:

  

JSONPATH = {项[]。status.containerStatuses [?(@。名== \ “一些-appdeployment \”)]。名}   {\ “\吨\”}   准备:   {.items []。spec.containers [?(@。名== \ “一些-appdeployment \”)]。相}   {\ “\ n \”}

此外,还可以获取名称为thendo a grep'[some-appdeployment]'的项目,以仅过滤那些容器

  

JSONPATH = {项[?(@。spec.containers [?(@。名== \ “一些-appdeployment \”)])]}

8 个答案:

答案 0 :(得分:5)

如果你想要那个输出,我的建议是执行如下命令:

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

这适合您的情况吗?

答案 1 :(得分:0)

丑陋一些,但至少不是我们想要的(如哈维尔·萨默隆所做的那样),而是Pod的真实状态:

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

说明:我们在Pod上进行描述,然后grep以Status:开头的行,剥离到第一行,选择第二列的值,在换行符后尾行。剩下的就是状态值。

答案 2 :(得分:0)

在我下面使用的openshift中,它工作得很好

oc get pods -l deploymentconfig='db' -o jsonpath='{.items[*].status.containerStatuses[0].ready}'

答案 3 :(得分:0)

status.phase将返回容器状态,而不是容器状态。但是kubectl get pod返回容器的容器状态,因此,如果容器中的容器处于crashloopbackoff错误,则会在状态中显示该容器。

对此的一种可能的解决方案是也检查容器状态。 我定制了@javier评论来得到这个。希望对您有帮助

  

kubectl get pods -o jsonpath =“名称:{.items [*] .metadata.name}阶段:{.items [*] .status.phase} ContainerStatus:{.items [*]。status.containerStatuses [ *]。state}“

答案 4 :(得分:0)

以下命令对我有用,请尝试一下。

kubectl get pods <pod-name> --no-headers -o custom-columns=":status.phase"

答案 5 :(得分:0)

通常,吊舱名称后跟唯一的哈希/ id。 为了专门针对名称,请使用--selector标志:

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

此示例将仅输出状态阶段。

答案 6 :(得分:0)

关于广告连播状态,一个唯一的答案(广告连播可以运行但尚未准备好!):

kubectl get pod -l datahub.sap.com/app-component=vsystem -o 'jsonpath={.status.conditions[?(@.type=="Ready")].status}'

对于单个容器状态(必须为“ true”):

kubectl get pod <pod_name> --output="jsonpath={.status.containerStatuses[*].ready}" | cut -d' ' -f2

答案 7 :(得分:0)

kubectl get pods -n deployNamespace -ojson | jq '.spec.status.phase'

您将安装 jq。