Expose Pod information using an volume

2 min read | by Jordi Prats

We can choose to expose some of the Pod's information as volumes or environment variables using DownwardAPIVolumeFile. It can expose both Pod fields and Container fields

We'll have to check the DownwardAPIVolumeFile documentation for which fields can be exposed.

At the time of this writing, for fieldRef we can only expose labels and annotations beside it's name and namespace:

Required: Selects a field of the pod: only annotations, labels, name and namespace are supported. 

An example pod with a downwardAPI Volume would look like as follows:

apiVersion: v1 kind: Pod metadata:  name: test-podinfo  labels:  example-label: demo  annotations:  example-annotation: demo spec:  containers:  - name: client-container  image: alpine  command: ["sleep", "infinity"]  volumeMounts:  - name: podinfo  mountPath: /etc/podinfo  volumes:  - name: podinfo  downwardAPI:  items:  - path: "labels"  fieldRef:  fieldPath: metadata.labels  - path: "annotations"  fieldRef:  fieldPath: metadata.annotations 

If we apply this manifest we will be able to see that the Volume contains two files, one for annotations and another one for labels:

$ kubectl apply -f podinfo.yaml  pod/test-podinfo created $ kubectl get pods NAME READY STATUS RESTARTS AGE sloop-0 1/1 Running 2 (32h ago) 43d test-podinfo 1/1 Running 0 13s $ kubectl exec -it test-podinfo -- ls /etc/podinfo annotations labels 

On the labels file we will only have the labels we have defined:

$ kubectl exec -it test-podinfo -- cat /etc/podinfo/labels example-label="demo" 

But for the annotations file we will also be able to see all the annotations that the Pod has (not just the ones we have defined):

$ kubectl exec -it test-podinfo -- cat /etc/podinfo/annotations example-annotation="demo" kubectl.kubernetes.io/last-applied-configuration="{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{\"example-annotation\":\"demo\"},\"labels\":{\"example-label\":\"demo\"},\"name\":\"test-podinfo\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"command\":[\"sleep\",\"infinity\"],\"image\":\"alpine\",\"name\":\"client-container\",\"volumeMounts\":[{\"mountPath\":\"/etc/podinfo\",\"name\":\"podinfo\"}]}],\"volumes\":[{\"downwardAPI\":{\"items\":[{\"fieldRef\":{\"fieldPath\":\"metadata.labels\"},\"path\":\"labels\"},{\"fieldRef\":{\"fieldPath\":\"metadata.annotations\"},\"path\":\"annotations\"}]},\"name\":\"podinfo\"}]}}\n" kubernetes.io/config.seen="2022-01-04T21:04:45.076278440Z" kubernetes.io/config.source="api" 

Posted on 05/01/2022