天天看點

關于kubernetes的jsonpath

我們通常可以見到這種類型的指令:

kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f
           

這裡jsonpath是做什麼用的,下面我們探究一下,

在kuberctl get --help中的描述:

-o, --output='': Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/te
xt/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
           

通路:http://kubernetes.io/docs/user-guide/jsonpath

kubectl get pod -n kubesphere-system -l app=ks-install -o json

點選檢視代碼

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "v1",
            "kind": "Pod",
            "metadata": {
                "annotations": {
                    "cni.projectcalico.org/containerID": "53f8ab7fbf2ce1ae23f9848168e23f75aff5d0fc1a89dadd02153937549dba08",
                    "cni.projectcalico.org/podIP": "10.233.96.1/32",
                    "cni.projectcalico.org/podIPs": "10.233.96.1/32"
                },
                "creationTimestamp": "2022-01-03T07:54:26Z",
                "generateName": "ks-installer-69df988b79-",
                "labels": {
                    "app": "ks-install",
                    "pod-template-hash": "69df988b79",
                    "version": "v3.2.1"
                },
                "name": "ks-installer-69df988b79-t2lrn",
                "namespace": "kubesphere-system",
                "ownerReferences": [
                    {
                        "apiVersion": "apps/v1",
                        "blockOwnerDeletion": true,
                        "controller": true,
                        "kind": "ReplicaSet",
                        "name": "ks-installer-69df988b79",
                        "uid": "0151a8f9-932e-4185-bcd5-be838502c47d"
                    }
                ],
                "resourceVersion": "991",
                "uid": "d4afe165-f4e1-4f45-85ae-a0ab49c515b4"
            },
            "spec": {
                "containers": [
                    {
                        "image": "registry.cn-beijing.aliyuncs.com/kubesphereio/ks-installer:v3.2.1",
                        "imagePullPolicy": "Always",
                        "name": "installer",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/etc/localtime",
                                "name": "host-time"
                            },
                            {
                                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                                "name": "kube-api-access-92t8r",
                                "readOnly": true
                            }
                        ]
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "enableServiceLinks": true,
                "nodeName": "node2",
                "preemptionPolicy": "PreemptLowerPriority",
                "priority": 0,
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "serviceAccount": "ks-installer",
                "serviceAccountName": "ks-installer",
                "terminationGracePeriodSeconds": 30,
                "tolerations": [
                    {
                        "effect": "NoExecute",
                        "key": "node.kubernetes.io/not-ready",
                        "operator": "Exists",
                        "tolerationSeconds": 300
                    },
                    {
                        "effect": "NoExecute",
                        "key": "node.kubernetes.io/unreachable",
                        "operator": "Exists",
                        "tolerationSeconds": 300
                    }
                ],
                "volumes": [
                    {
                        "hostPath": {
                            "path": "/etc/localtime",
                            "type": ""
                        },
                        "name": "host-time"
                    },
                    {
                        "name": "kube-api-access-92t8r",
                        "projected": {
                            "defaultMode": 420,
                            "sources": [
                                {
                                    "serviceAccountToken": {
                                        "expirationSeconds": 3607,
                                        "path": "token"
                                    }
                                },
                                {
                                    "configMap": {
                                        "items": [
                                            {
                                                "key": "ca.crt",
                                                "path": "ca.crt"
                                            }
                                        ],
                                        "name": "kube-root-ca.crt"
                                    }
                                },
                                {
                                    "downwardAPI": {
                                        "items": [
                                            {
                                                "fieldRef": {
                                                    "apiVersion": "v1",
                                                    "fieldPath": "metadata.namespace"
                                                },
                                                "path": "namespace"
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ]
            },
            "status": {
                "conditions": [
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2022-01-03T07:54:27Z",
                        "status": "True",
                        "type": "Initialized"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2022-01-03T07:54:52Z",
                        "status": "True",
                        "type": "Ready"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2022-01-03T07:54:52Z",
                        "status": "True",
                        "type": "ContainersReady"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2022-01-03T07:54:27Z",
                        "status": "True",
                        "type": "PodScheduled"
                    }
                ],
                "containerStatuses": [
                    {
                        "containerID": "docker://709d6513812c960d2a379ea125779a1374621b08e4d495d85c3cca402aad142b",
                        "image": "registry.cn-beijing.aliyuncs.com/kubesphereio/ks-installer:v3.2.1",
                        "imageID": "docker-pullable://registry.cn-beijing.aliyuncs.com/kubesphereio/ks-installer@sha256:749f1ed10c672ae460f79932553ac5ba321b98df972b778041dc2cc7402dd478",
                        "lastState": {},
                        "name": "installer",
                        "ready": true,
                        "restartCount": 0,
                        "started": true,
                        "state": {
                            "running": {
                                "startedAt": "2022-01-03T07:54:52Z"
                            }
                        }
                    }
                ],
                "hostIP": "172.30.47.69",
                "phase": "Running",
                "podIP": "10.233.96.1",
                "podIPs": [
                    {
                        "ip": "10.233.96.1"
                    }
                ],
                "qosClass": "BestEffort",
                "startTime": "2022-01-03T07:54:27Z"
            }
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}

           

根據這個描述:

關于kubernetes的jsonpath

{.items[0].metadata.name} 得到的就是“ks-installer-69df988b79-t2lrn”

指令就相當于:

kubectl logs -n kubesphere-system ks-installer-69df988b79-t2lrn -f
           

省去了手動查找pod的步驟。

k8s

繼續閱讀