在Pod中获取宿主机的主机名、namespace等

pull/68/head
Jimmy Song 2017-11-15 21:16:45 +08:00
parent 33029e8bac
commit ac857c1ab8
1 changed files with 39 additions and 0 deletions

View File

@ -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
```