每个Pod在成功创建出来以后,都会被系统分配唯一的名字、IP地址,并且处于某个Namespace中。在Pod容器内要获取Pod这些重要信息,可以使用Downward API。
Downward API可以通过以下两种方式将Pod信息注入容器底部:
下例通过Downward API将Pod的IP、名称和所在Namespace注入容器的环境变量中,容器应用使用env命令将全部环境变量打印到标准输出中:
dapi-test-pod.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_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
restartPolicy: Never
注意到上面valueFrom这种特殊的语法是Downward API的写法。目前Downward API提供了以下变量。
运行kubectl create命令创建Pod:
kubectl create -f dapi-test-pod.yaml
查看dapi-test-pod的日志:
kubectl logs dapi-test-pod
......
MY_POD_NAMESPACE=default
MY_POD_IP=172.17.1.2
MY_POD_NAME=dapi-test-pod
从日志中可以看到Pod的IP、Name及Namespace等信息都被正确保存到了Pod的环境变量中。
下例通过Downward API将Container的资源请求和限制信息注入容器的环境变量中,容器应用使用printenv命令将设置的资源请求和资源限制环境变量打印到标准输出中:
dapi-test-pod-container-vars.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod-container-vars
spec:
containers:
- name: test-container
image: busybox
command: ["sh","-c"]
args:
- while true;do
echo -en '\n';
printenv MY_CPU_REQUEST MY_CPU_LIMIT;
printenv MY_MEM_REQUEST MY_MEM_LIMIT;
sleep 3600;
done;
resources:
requests:
memory: "32Mi"
cpu: "125m"
limits:
memory: "64Mi"
cpu: "250m"
env:
- name: MY_CPU_REQUEST
valueFrom:
resourceFieldRef:
containerName: test-container
resource: request.cpu
- name: MY_CPU_LIMIT
valueFrom:
resourceFieldRef:
containerName: test-container
resource: limits.cpu
- name: MY_MEM_REQUEST
valueFrom:
resourceFieldRef:
containerName: test-container
resource: request.memory
- name: MY_MEM_LIMIT
valueFrom:
resourceFieldRef:
containerName: test-container
resource: limits.memory
restartPolicy: Never
注意valueFrom这种特殊的Downward API语法,目前resourceFieldRef可以将容器的资源请求和资源限制等配置设置为容器内部的环境变量。
kubectl create -f dapi-test-pod-container-vars.yaml
kubectl get pods
查看dapi-test-pod-container-vars的日志:
kubectl logs dapi-test-pod-container-vars
从日志中可以看到Container的requests.cpu、limits.cpu、requests.memory、limits.memory等信息被正确保存到了Pod的环境变量中。
下例通过Downward API将Pod的Label、Annotation列表通过Volume挂载为容器内的一个文件,容器应用使用echo命令将文件内容打印到标准输出中:
dapi-test-pod-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod-volume
labels:
zone: us-est-coast
cluster: test-cluster1
rack: rack-22
annotations:
build: two
builder: john-doe
spec:
containers:
- name: test-container
image: busybox
imagePullPolicy: Never
command: ["sh","-c"]
args:
- whild true; do
if [[ -e /etc/labels ]]; then
echo -en '\n\n'; cat /etc/labels; fi;
if [[ -e /etc/annotations ]]; then
echo -en '\n\n'; cat /etc/annotations; fi;
sleep 3600;
done;
volumeMounts:
- name: podinfo
mountPath: /etc
readOnly: false
volumes:
- name: podinfo
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
- path: "annotations"
fieldRef:
fieldPath: metadata.annotations
注意volumes中downwardAPI的特殊语法,通过items的设置,将会以path的名称生成文件。
运行kubectl create命令创建Pod
kubectl create -f dapi-test-pod-volume.yaml
kubectl get pods
查看dapi-test-pod-volume的日志:
kubectl logs dapi-test-pod-volume
zone="us-est-coast"
cluster="test-cluster1"
rack="rack-22"
build="two"
builder="john-doe"
从日志中看到Pod的Label和Annotation信息都被保存到了容器内的/etc/labels和/etc/annotations文件中。