Selected topic
Configuration
Prefer practical output? Use related tools below while reading.
Here's an overview and an example of using the Downward API:
### Overview
The main components involved are:
- Environment Variables: Pod-level information is exposed as environment variables in the container.
- Files: Pod metadata is stored as files inside a volume within your pod.
- Command-line arguments: Some metadata can also be passed to commands or applications running within containers.
### Example
Let's say you have a simple web application that needs to display its name and version based on the Kubernetes cluster it runs in. You can use labels to identify pods, inject these labels as environment variables into the container using the Downward API, and then your application can access this information at runtime.
yaml
apiVersion: v1
kind: Pod
metadata:
name: config-example
spec:
containers:
- name: config-example-container
image: busybox
volumeMounts:
- name: config-volume
mountPath: /etc/config
readOnly: true
volumes:
- name: config-volume
downwardAPI:
items:
- path: "metadata.name"
fieldRef:
fieldPath: metadata.name
- path: "status.phase"
fieldRef:
fieldPath: status.phase
busybox container), you can then access these environment variables. For instance, to echo the pod's name and phase:bash
#!/bin/sh
echo "Pod Name: $METADATA_NAME"
echo "Phase: $STATUS_PHASE"
This example showcases a basic use case for injecting pod metadata into containers using the Downward API. The flexibility of this mechanism allows you to tailor container configurations based on the properties of their parent pods, making it a powerful tool for managing deployments in Kubernetes environments.