本文講述Pod能獲取Pod自身運行的容器信息以及Node信息(kubernetes 自從1.7開始)node
1.編譯busybox-env.yaml
文件api
apiVersion: v1 kind: Pod metadata: name: busybox-env spec: containers: - name: busybox-container image: busybox command: - sleep - "3600" env: - name: MY_NODE_NAME # 獲取node名稱 valueFrom: fieldRef: fieldPath: spec.nodeName - name: MY_POD_NAME # 獲取pod名稱 valueFrom: fieldRef: fieldPath: metadata.name - name: MY_POD_NAMESPACE # 獲取pod的namespace valueFrom: fieldRef: fieldPath: metadata.namespace - name: MY_POD_IP # 獲取pod IP valueFrom: fieldRef: fieldPath: status.podIP
2.應用yamlapp
$ kubectl apply -f busybox-env.yaml
3.進入容器內部查看變量是否存在ide
$ kubectl exec -it busybox-env sh $ env | egrep "MY_POD" MY_POD_NAMESPACE=default MY_POD_IP=10.23.2.1 MY_POD_NAME=busybox-env $ env | egrep "MY_NODE" MY_NODE_NAME=k8s-node01
能夠看到變量是已經加載到容器的環境變量中了spa