在Pod中获取宿主机的主机名、namespace等
parent
33029e8bac
commit
ac857c1ab8
|
@ -145,3 +145,42 @@ volumeMounts:
|
|||
path: /etc/localtime
|
||||
```
|
||||
|
||||
## 6. 在Pod中获取宿主机的主机名、namespace等
|
||||
|
||||
这条技巧补充了第一条获取 podIP 的内容,方法都是一样的,只不过列出了更多的引用字段。
|
||||
|
||||
参考下面的 pod 定义,每个 pod 里都有一个 {.spec.nodeName} 字段,通过 `fieldRef` 和环境变量,就可以在Pod中获取宿主机的主机名(访问环境变量`MY_NODE_NAME`)。
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: dapi-test-pod
|
||||
spec:
|
||||
containers:
|
||||
- name: test-container
|
||||
image: busybox
|
||||
command: [ "/bin/sh", "-c", "env" ]
|
||||
env:
|
||||
- name: MY_NODE_NAME
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: spec.nodeName
|
||||
- name: MY_POD_NAME
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: metadata.name
|
||||
- name: MY_POD_NAMESPACE
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: metadata.namespace
|
||||
- name: MY_POD_IP
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: status.podIP
|
||||
- name: MY_POD_SERVICE_ACCOUNT
|
||||
valueFrom:
|
||||
fieldRef:
|
||||
fieldPath: spec.serviceAccountName
|
||||
restartPolicy: Never
|
||||
```
|
Loading…
Reference in New Issue