7.5.2.2. Consuming container values using a volume plugin
Your containers can consume Downward API values by using a volume plugin.
Containers can consume the following values:
- Pod name
- Pod project/namespace
- Pod annotations
- Pod labels
The following procedure show how to use the volume plugin.
Procedure
Create a new pod spec that contains the environment variables you want the container to consume:
Create a
volume-pod.yamlfile similar to the following:kind: Pod apiVersion: v1 metadata: labels: zone: us-east-coast cluster: downward-api-test-cluster1 rack: rack-123 name: dapi-volume-test-pod annotations: annotation1: "345" annotation2: "456" spec: securityContext: runAsNonRoot: true seccompProfile: type: RuntimeDefault containers: - name: volume-test-container image: gcr.io/google_containers/busybox command: ["sh", "-c", "cat /tmp/etc/pod_labels /tmp/etc/pod_annotations"] volumeMounts: - name: podinfo mountPath: /tmp/etc readOnly: false securityContext: allowPrivilegeEscalation: false capabilities: drop: [ALL] volumes: - name: podinfo downwardAPI: defaultMode: 420 items: - fieldRef: fieldPath: metadata.name path: pod_name - fieldRef: fieldPath: metadata.namespace path: pod_namespace - fieldRef: fieldPath: metadata.labels path: pod_labels - fieldRef: fieldPath: metadata.annotations path: pod_annotations restartPolicy: Never # ...Create the pod from the
volume-pod.yamlfile by using the following command:$ oc create -f volume-pod.yaml
Verification
Check the container logs and verify the presence of the configured fields by using the following command:
$ oc logs -p dapi-volume-test-podExample output
cluster=downward-api-test-cluster1 rack=rack-123 zone=us-east-coast annotation1=345 annotation2=456 kubernetes.io/config.source=api