kubectl之jsonpath笔记
jsonpath
Function | Description | Example | Result |
---|---|---|---|
text | the plain text | kind is {.kind} | kind is List |
@ | the current object | {@} | the same as input |
. or [] | child operator | {.kind} or {[‘kind’]} | List |
… | recursive descent | {…name} | 127.0.0.1 127.0.0.2 myself e2e |
* | wildcard. Get all objects | {.items[*].metadata.name} | [127.0.0.1 127.0.0.2] |
[start:end :step] | subscript operator | {.users[0].name} | myself |
[,] | union operator | {.items[*][‘metadata.name’, ‘status.capacity’]} | 127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8] |
?() | filter | {.users[?(@.name==“e2e”)].user.password} | secret |
range, end | iterate list | {range .items[*]}[{.metadata.name}, {.status.capacity}] {end} | [127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]] |
“ | quote interpreted string | {range .items[*]}{.metadata.name}{’\t’}{end} | 127.0.0.1 127.0.0.2 |
- 查询镜像以及副本数
kubectl get deployment -n namespace -o jsonpath='{range .items[*]}[{.spec.template.spec.containers[*].image},{.spec.replicas}]{end}'|tr -s '[[:space:]]' '\n'|uniq
- 副本数为零的镜像
kubectl get deployment -n namespace -o jsonpath='{.items[?(@.spec.replicas==0)].spec.template.spec.containers[*].image}'|tr -s '[[:space:]]' '\n'|uniq
- 检查是否添加探活
kubectl get deploy -n namespace-o jsonpath='{range .items[*]}[{.spec.template.spec.containers[*].livenessProbe.initialDelaySeconds},{.metadata.name}]{"\n"}{end}'
kubectl get deploy -n namespace -o jsonpath='{range .items[*]}[{.spec.template.spec.containers[*].readinessProbe.initialDelaySeconds},{.metadata.name}]{"\n"}{end}'
- 查询初始化容器镜像
kubectl get deployment -A -o jsonpath='{range .items[?(@.spec.template.spec.initContainers)]}{.metadata.namespace},{.metadata.name},{.spec.template.spec.initContainers[*].image}{"\n"}{end}'
- 按照时间搜集项目,部署集群(规格,几个Pod)
kubectl get deploy -n kube-system --sort-by=.metadata.creationTimestamp -ojsonpath='{range .items[*]}{.metadata.creationTimestamp}{"\t"}{.spec.replicas}{"\t"}{.metadata.namespace}{"\t"}{.metadata.name
}{"\t\t\t"}{.spec.template.spec.containers[*].resources}{"\n"}{end}'
6.查询deploy及容器资源配置
kubectl get deployment -n namespace -o jsonpath='{range .items[*]}[{.spec.template.spec.containers[*].resources},{.metadata.name},{.spec.template.spec.containers[*].name}] {"\n"}{end}'
7.查询deploy镜像
kubectl get deploy -o custom-columns=CONTAINER:.spec.template.spec.containers[0].name,IMAGE:.spec.template.spec.containers[0].image -n ns
8.查询deploy的第一个contianer的name
kubectl get deploy -o custom-columns=CONTAINER:.spec.template.spec.containers[0].name
9.查询deploy的指定变量名的环境变量
kubectl get deploy -A -ojsonpath='{range .items[*]}{.metadata.namespace}-{.metadata.name}-{.spec.template.spec.containers[*].env[?(@.name=="APOLLO_CLUSTER")]}{"\n"}'