本章節(jié)展示了Pod如何使用DownwardAPIVolumeFile公開信息到自己運(yùn)行的容器。DownwardAPIVolumeFile可以公開Pod字段和容器字段疮鲫。
存儲Pod字段
在本次實(shí)驗(yàn)中绞灼,創(chuàng)建一個(gè)包含一個(gè)容器的Pod,下面是這個(gè)Pod的配置文件:
apiVersion: v1
kind: Pod
metadata:
name: kubernetes-downwardapi-volume-example
labels:
zone: us-est-coast
cluster: test-cluster1
rack: rack-22
annotations:
build: two
builder: john-doe
spec:
containers:
- name: client-container
image: gcr.io/google_containers/busybox
command: ["sh", "-c"]
args:
- while 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 5;
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
在配置文件中,可以看到Pod有一個(gè)downwardAPI卷印蓖,并且容器掛載卷到/etc。
觀察downwardAPI下面的items的數(shù)組京腥。數(shù)組的每一個(gè)元素都是DownwardAPIVolumeFile赦肃。第一個(gè)元素指定了值是Pod的metadata.labels字段存儲進(jìn)一個(gè)名字為labels的文件里面。第二個(gè)元素指定了值是Pod的annotations字段存儲進(jìn)了名字為annotation文件绞旅。
注意:在這個(gè)例子里面這些字段都是Pod的字段摆尝,不是運(yùn)行在Pod里面容器的字段。
創(chuàng)建Pod:
kubectl create -f test.yaml
驗(yàn)證Pod中的容器是否運(yùn)行:
kubectl get pods
查看容器日志:
kubectl logs kubernetes-downwardapi-volume-example
輸出展示了labels和annotations文件的內(nèi)容:
cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"
build="two"
builder="john-doe"
用shell進(jìn)入這個(gè)Pod的容器里面
kubectl exec -it kubernetes-downwardapi-volume-example -- sh
在shell里查看labels文件:
/# cat /etc/labels
輸出展示了Pod所有的標(biāo)簽已經(jīng)寫入到labels文件里面:
cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"
同樣的查看annotations文件:
/# cat /etc/annotations
查看 /etc目錄下面的文件:
/# ls -laR /etc
在輸出里面看一看到labels和annotations文件在臨時(shí)文件子目錄里面:
在這個(gè)例子里面因悲,..2982_06_02_21_47_53.299460680在/etc目錄里堕汞,..data是一個(gè)軟連接到臨時(shí)子目錄里面。同樣的在/etc目錄里面labels和annotations是軟連接晃琳。
drwxr-xr-x ... Feb 6 21:47 ..2982_06_02_21_47_53.299460680
lrwxrwxrwx ... Feb 6 21:47 ..data -> ..2982_06_02_21_47_53.299460680
lrwxrwxrwx ... Feb 6 21:47 annotations -> ..data/annotations
lrwxrwxrwx ... Feb 6 21:47 labels -> ..data/labels
/etc/..2982_06_02_21_47_53.299460680:
total 8
-rw-r--r-- ... Feb 6 21:47 annotations
-rw-r--r-- ... Feb 6 21:47 labels
使用軟連接可以實(shí)現(xiàn)元數(shù)據(jù)動態(tài)原子更新讯检;更新將寫入臨時(shí)目錄,..data軟連接使用rename(2)原子更新卫旱。
存儲容器字段
在上面的實(shí)驗(yàn)人灼,存儲Pod字段在DownwardAPIVolumeFile里面。在下面的實(shí)驗(yàn)可以存儲容器字段顾翼。下面是Pod的配置文件運(yùn)行了一個(gè)容器:
apiVersion: v1
kind: Pod
metadata:
name: kubernetes-downwardapi-volume-example-2
spec:
containers:
- name: client-container
image: gcr.io/google_containers/busybox:1.24
command: ["sh", "-c"]
args:
- while true; do
echo -en '\n';
if [[ -e /etc/cpu_limit ]]; then
echo -en '\n'; cat /etc/cpu_limit; fi;
if [[ -e /etc/cpu_request ]]; then
echo -en '\n'; cat /etc/cpu_request; fi;
if [[ -e /etc/mem_limit ]]; then
echo -en '\n'; cat /etc/mem_limit; fi;
if [[ -e /etc/mem_request ]]; then
echo -en '\n'; cat /etc/mem_request; fi;
sleep 5;
done;
resources:
requests:
memory: "32Mi"
cpu: "125m"
limits:
memory: "64Mi"
cpu: "250m"
volumeMounts:
- name: podinfo
mountPath: /etc
readOnly: false
volumes:
- name: podinfo
downwardAPI:
items:
- path: "cpu_limit"
resourceFieldRef:
containerName: client-container
resource: limits.cpu
- path: "cpu_request"
resourceFieldRef:
containerName: client-container
resource: requests.cpu
- path: "mem_limit"
resourceFieldRef:
containerName: client-container
resource: limits.memory
- path: "mem_request"
resourceFieldRef:
containerName: client-container
resource: requests.memory
在這個(gè)配置文件里面投放,可以看到Pod有一個(gè)downwardAPI卷,并且掛載到/etc里面适贸。
觀察downwardAPI下面的items灸芳。數(shù)組的每個(gè)元素是一個(gè)DownwardAPIVolumeFile。
第一個(gè)元素指定了名字為client-container的容器拜姿,值是limits.cpu的字段存進(jìn)名字為cpu_limit的文件里面烙样。
創(chuàng)建Pod:
kubectl create -f test.yaml
用shell進(jìn)入到運(yùn)行的Pod的容器里面:
kubectl exec -it kubernetes-downwardapi-volume-example-2 -- sh
在shell里面查看cpu_limit文件:
/# cat /etc/cpu_limit
可以用類似的命令查看cpu_request,mem_limit和mem_request文件。
Downward API功能
容器可以通過環(huán)境變量和DownwardAPIVolumeFiles獲得下面的信息:
- 節(jié)點(diǎn)的名字
- Pod的名字
- Pod的namespace
- Pod的IP地址
- Pod服務(wù)的賬號名字
- 容器的CPU限制
- 容器的CPU請求
- 容器的內(nèi)存限制
- 容器的內(nèi)存請求
額外的可以通過DownwardAPIVolumeFiles獲得下面的信息:
- Pod的標(biāo)簽
- Pod的注解
注意:如果容器沒有指定CPU和內(nèi)存限制蕊肥,Downward API默認(rèn)的值是節(jié)點(diǎn)可分配的CPU和內(nèi)存谒获。