go-template
若知道pod名称,想要获取pod的ip地址可以使用go-template
来获取。
root@k8s-master01:~/yaml/chapter06# kubectl get pods configmaps-volume-demo -o go-template={{.status.podIP}}
10.244.2.13
要知道其字段的名称,需要层级的查看,查看完整的字段可以使用-o json
来输出
root@k8s-master01:~/yaml/chapter06# kubectl get pods configmaps-volume-demo -o json
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"configmaps-volume-demo","namespace":"default"},"spec":{"containers":[{"image":"nginx:alpine","name":"nginx-server","volumeMounts":[{"mountPath":"/etc/nginx/conf.d/","name":"ngxconfs","readOnly":true}]}],"volumes":[{"configMap":{"name":"nginx-config-files","optional":false},"name":"ngxconfs"}]}}n"
},
"creationTimestamp": "2021-07-16T05:06:17Z",
"name": "configmaps-volume-demo",
"namespace": "default",
"resourceVersion": "189501",
"uid": "c122066f-715e-4a24-afad-8b7c07d597b9"
},
"spec": {
"containers": [
{
"image": "nginx:alpine",
"imagePullPolicy": "IfNotPresent",
"name": "nginx-server",
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"volumeMounts": [
{
"mountPath": "/etc/nginx/conf.d/",
"name": "ngxconfs",
"readOnly": true
},
{
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
"name": "kube-api-access-rjvg6",
"readOnly": true
}
]
}
],
"dnsPolicy": "ClusterFirst",
"enableServiceLinks": true,
"nodeName": "k8s-node02",
"preemptionPolicy": "PreemptLowerPriority",
"priority": 0,
"restartPolicy": "Always",
"schedulerName": "default-scheduler",
"securityContext": {},
"serviceAccount": "default",
"serviceAccountName": "default",
"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": [
{
"configMap": {
"defaultMode": 420,
"name": "nginx-config-files",
"optional": false
},
"name": "ngxconfs"
},
{
"name": "kube-api-access-rjvg6",
"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": "2021-07-16T05:06:17Z",
"status": "True",
"type": "Initialized"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2021-07-16T05:06:32Z",
"status": "True",
"type": "Ready"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2021-07-16T05:06:32Z",
"status": "True",
"type": "ContainersReady"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2021-07-16T05:06:17Z",
"status": "True",
"type": "PodScheduled"
}
],
"containerStatuses": [
{
"containerID": "docker://83ed060f9731a87033d8cbd76f671bcda8aa44da657534a12170dbeb4c7b6f7f",
"image": "nginx:alpine",
"imageID": "docker-pullable://nginx@sha256:91528597e842ab1b3b25567191fa7d4e211cb3cc332071fa031cfed2b5892f9e",
"lastState": {},
"name": "nginx-server",
"ready": true,
"restartCount": 0,
"started": true,
"state": {
"running": {
"startedAt": "2021-07-16T05:06:32Z"
}
}
}
],
"hostIP": "172.16.11.82",
"phase": "Running",
"podIP": "10.244.2.13",
"podIPs": [
{
"ip": "10.244.2.13"
}
],
"qosClass": "BestEffort",
"startTime": "2021-07-16T05:06:17Z"
}
}
JsonPath
# 使用jsonPath查看某个pod的信息
root@k8s-master01:~# kubectl get pods replicaset-demo-5fmvs -o jsonpath={.metadata.ownerReferences}
[{"apiVersion":"apps/v1","blockOwnerDeletion":true,"controller":true,"kind":"ReplicaSet","name":"replicaset-demo","uid":"d16ab83c-c9a6-486f-b202-72e93f982750"}]
# 引用该项上的第0项的name信息
root@k8s-master01:~# kubectl get pods replicaset-demo-5fmvs -o jsonpath={.metadata.ownerReferences[0].name}
replicaset-demor
原文始发于微信公众号(TechOps之窗):使用Go/JsonPath输出字段
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/288514.html