k8s查找所有pod_kubernetes 容器内获取Pod信息
1.编译busybox-env.yaml文件apiVersion: v1kind: Podmetadata:name: busybox-envspec:containers:- name: busybox-containerimage: busyboxcommand:- sleep- "3600"env:- name: MY_NODE_NAME # 获取node名称valueFrom:fieldR
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
更多推荐
所有评论(0)