若知道pod名称,想要获取pod的ip地址可以使用go-template来获取。

1
2
root@k8s-master01:~/yaml/chapter06# kubectl get pods configmaps-volume-demo -o go-template={{.status.podIP}}
10.244.2.13

要知道其字段的名称,需要层级的查看,查看完整的字段可以使用-o json来输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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"
}
}