如何使用kubectl get pods -o = jsonpath = {}

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

我正在关注https://www.katacoda.com/courses/kubernetes/liveness-readiness-healthchecks

我看到一个有趣的命令来解析kubectl中的json对象使用标志-o=jsonpath,如下所示:

# pod=$(kubectl get pods --selector="name=bad-frontend" --output=jsonpath={.items..metadata.name})

# kubectl describe pod $pod

如何在原始的json中使用map[string]interface {}, []interface {}{map[string]interface {}, map[string]interface {}{}

我怎么能容易地知道{.itme..metadata.name}是正确的,而不是使用{..item.metadata..name}的东西?

要检查的原始文件(如果你错误地传递{},它将返回给你):

map[string]interface {}{"kind":"List", "apiVersion":"v1", "metadata":map[string]interface {}{}, "items":[]interface {}{map[string]interface {}{"status":map[string]interface {}{"phase":"Running", "conditions":[]interface {}{map[string]interface {}{"type":"Ready", "status":"False", "lastProbeTime":interface {}(nil), "lastTransitionTime":"2018-01-03T14:41:26Z", "reason":"ContainersNotReady", "message":"containers with unready status: [bad-frontend]"}}, "hostIP":"127.0.0.1", "podIP":"172.18.0.3", "startTime":"2018-01-03T14:41:26Z", "containerStatuses":[]interface {}{map[string]interface {}{"ready":false, "restartCount":6, "image":"katacoda/docker-http-server:unhealthy", "imageID":"docker://sha256:dc680e51481ae0256b5483e0d3c0bd188215a67b0926d4ed07e8a9fe55e16154", "containerID":"docker://3a067a667e8e73784c761d75c3614d3aa138a6d498b22b48e1c23aa1d8158170", "name":"bad-frontend", "state":map[string]interface {}{"waiting":map[string]interface {}{"reason":"CrashLoopBackOff", "message":"Back-off 2m40s restarting failed container=bad-frontend pod=bad-frontend-zbgrw_default(28bdddce-f094-11e7-9f7b-0242ac110036)"}}, "lastState":map[string]interface {}{"terminated":map[string]interface {}{"containerID":"docker://3a067a667e8e73784c761d75c3614d3aa138a6d498b22b48e1c23aa1d8158170", "exitCode":2, "reason":"Error", "startedAt":"2018-01-03T14:44:57Z", "finishedAt":"2018-01-03T14:45:06Z"}}}}}, "kind":"Pod", "apiVersion":"v1", "metadata":map[string]interface {}{"name":"bad-frontend-zbgrw", "generateName":"bad-frontend-", "uid":"28bdddce-f094-11e7-9f7b-0242ac110036", "labels":map[string]interface {}{"name":"bad-frontend"}, "annotations":map[string]interface {}{"kubernetes.io/created-by":"{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicationController\",\"namespace\":\"default\",\"name\":\"bad-frontend\",\"uid\":\"24a58c4a-f094-11e7-9f7b-0242ac110036\",\"apiVersion\":\"v1\",\"resourceVersion\":\"15\"}}\n"}, "namespace":"default", "selfLink":"/api/v1/namespaces/default/pods/bad-frontend-zbgrw", "resourceVersion":"300", "creationTimestamp":"2018-01-03T14:41:18Z"}, "spec":map[string]interface {}{"restartPolicy":"Always", "terminationGracePeriodSeconds":30, "dnsPolicy":"ClusterFirst", "nodeName":"127.0.0.1", "securityContext":map[string]interface {}{}, "containers":[]interface {}{map[string]interface {}{"terminationMessagePath":"/dev/termination-log", "imagePullPolicy":"IfNotPresent", "name":"bad-frontend", "image":"katacoda/docker-http-server:unhealthy", "resources":map[string]interface {}{}, "livenessProbe":map[string]interface{}{"timeoutSeconds":1, "periodSeconds":10, "successThreshold":1, "failureThreshold":3, "httpGet":map[string]interface {}{"path":"/", "port":80, "scheme":"HTTP"}, "initialDelaySeconds":1}, "readinessProbe":map[string]interface {}{"httpGet":map[string]interface {}{"path":"/", "port":80, "scheme":"HTTP"}, "initialDelaySeconds":1, "timeoutSeconds":1, "periodSeconds":10, "successThreshold":1, "failureThreshold":3}}}}}}}
kubernetes output jsonpath kubectl
1个回答
0
投票

我很高兴我在这里找到了一个很好的解决方案:https://gist.github.com/so0k/42313dbb3b547a0f51a547bb968696ba

基本上你必须安装jq或jiq,然后输入kubectl get no -o json | jid -q | pbcopy。有趣的部分开始:你输入。和item等等将被提议供您选择,您可以轻松构建您的过滤器语法。

© www.soinside.com 2019 - 2024. All rights reserved.