首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用kubectl get pods -o=jsonpath={}

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

Stack Overflow用户
提问于 2018-01-03 23:10:39
回答 1查看 6.3K关注 0票数 1

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

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

代码语言:javascript
复制
# 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 {}{}

如果不使用{..item.metadata..name},我如何才能轻松知道{.itme..metadata.name}是正确的

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

代码语言:javascript
复制
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}}}}}}}
EN

回答 1

Stack Overflow用户

发布于 2018-01-04 00:00:33

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

基本上,您必须安装jq或jiq,然后输入kubectl get no -o json | jid -q | pbcopy。有趣的部分开始了:你打字。并将项目等建议给你选择,你可以很容易地构建你的过滤器语法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48080092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档