如何从kubectl返回的JSON中提取kubernetes服务对象

问题描述 投票:0回答:1

鉴于下面的kubectl命令及其响应,我想要挖出Service对象。该命令将获取由我的部署描述符创建的所有k8s对象的当前状态,该描述符名为quotem.yaml。

% kubectl get -f quotem_v2.yaml -o json

    {
        "apiVersion": "v1",
        "items": [
            {
                "apiVersion": "v1",
                "kind": "Service",
                "metadata": {
                    "annotations": {
                        "getambassador.io/config": "---\napiVersion: ambassador/v0\nkind:  Mapping\nname:  qotm_mapping_v2\nprefix: /qotm/\nservice: qotm-v2\nweight: 300\n",
                        "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{\"getambassador.io/config\":\"---\\napiVersion: ambassador/v0\\nkind:  Mapping\\nname:  qotm_mapping_v2\\nprefix: /qotm/\\nservice: qotm-v2\\nweight: 300\\n\"},\"name\":\"qotm-v2\",\"namespace\":\"default\"},\"spec\":{\"ports\":[{\"name\":\"http-qotm\",\"port\":80,\"targetPort\":\"http-api\"}],\"selector\":{\"app\":\"qotm-v2\"}}}\n"
                    },
                    "creationTimestamp": "2018-12-13T17:53:51Z",
                    "name": "qotm-v2",
                    "namespace": "default",
                    "resourceVersion": "202117",
                    "selfLink": "/api/v1/namespaces/default/services/qotm-v2",
                    "uid": "0cf4a4a5-ff00-11e8-9839-080027ced2f4"
                },
       ......
    }
json service kubernetes jq kubectl
1个回答
1
投票

kubectl get -f quotem_v2.yaml -o json | jq '.items[] | select(.kind=="Service")'

  • 上面的命令使用kubectl get和-a标志输出json
  • 然后它使用jq命令行json解析器来获取items数组,并从items数组中选择'kind'属性与'Service'匹配的元素
© www.soinside.com 2019 - 2024. All rights reserved.