1.编译busybox-env.yaml文件

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: metadata.name

2.应用yaml

$ kubectl apply -f busybox-env.yaml

3.进入容器内部查看变量是否存在

$ kubectl exec -it busybox-env sh

$ env | egrep "MY_POD"

MY_POD_NAMESPACE=default

MY_POD_IP=busybox-env

MY_POD_NAME=busybox-env

$ env | egrep "MY_NODE"

MY_NODE_NAME=k8s-node01

可以看到变量是已经加载到容器的环境变量中了

原文:https://blog.51cto.com/12643266/2464648

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐