Downward API Volume

We can mount pod and container information to containers file using volume of type downward api. Following example shows how we can do that.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: learning
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: pod-info
          mountPath: /info/pod
        - name: container-info
          mountPath: /info/container
  volumes:
    - name: pod-info
      downwardAPI:
        items:
          - path: pod_name
            fieldRef:
              fieldPath: metadata.name
          - path: pod_namespace
            fieldRef:
              fieldPath: metadata.namespace
    - name: container-info
      downwardAPI:
        items:
          - path: cpu_req
            resourceFieldRef:
              containerName: nginx
              resource: requests.cpu

Once the pod is created, we can exec into it and check files created at paths /info/container and /info/pod. path in items provide relative path to prefix mount paths. For example, /info/pod/pod_name, /info/container/cpu_req etc.

References

  1. https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/