Questo contenuto non è disponibile nella lingua selezionata.
Chapter 22. Downward API
22.1. Overview Copia collegamentoCollegamento copiato negli appunti!
The downward API is a mechanism that allows containers to consume information about API objects without coupling to OpenShift Container Platform. Such information includes the pod’s name, namespace, and resource values. Containers can consume information from the downward API using environment variables or a volume plug-in.
22.2. Selecting Fields Copia collegamentoCollegamento copiato negli appunti!
Fields within the pod are selected using the FieldRef
API type. FieldRef
has two fields:
Field | Description |
---|---|
| The path of the field to select, relative to the pod. |
|
The API version to interpret the |
Currently, the valid selectors in the v1 API include:
Selector | Description |
---|---|
| The pod’s name. This is supported in both environment variables and volumes. |
| The pod’s namespace.This is supported in both environment variables and volumes. |
| The pod’s labels. This is only supported in volumes and not in environment variables. |
| The pod’s annotations. This is only supported in volumes and not in environment variables. |
| The pod’s IP. This is only supported in environment variables and not volumes. |
The apiVersion
field, if not specified, defaults to the API version of the enclosing pod template.
22.3. Consuming Container Values Using the Downward API Copia collegamentoCollegamento copiato negli appunti!
22.3.1. Using Environment Variables Copia collegamentoCollegamento copiato negli appunti!
One mechanism for consuming the downward API is using a container’s environment variables. The EnvVar
type’s valueFrom
field (of type EnvVarSource
) is used to specify that the variable’s value should come from a FieldRef
source instead of the literal value specified by the value
field. In the future, additional sources may be supported; currently the source’s fieldRef
field is used to select a field from the downward API.
Only constant attributes of the pod can be consumed this way, as environment variables cannot be updated once a process is started in a way that allows the process to be notified that the value of a variable has changed. The fields supported using environment variables are:
- Pod name
Pod namespace
Create a
pod.yaml
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the
pod.yaml
file:oc create -f pod.yaml
$ oc create -f pod.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the container’s logs for the
MY_POD_NAME
andMY_POD_NAMESPACE
values:oc logs -p dapi-env-test-pod
$ oc logs -p dapi-env-test-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
22.3.2. Using the Volume Plug-in Copia collegamentoCollegamento copiato negli appunti!
Another mechanism for consuming the downward API is using a volume plug-in. The downward API volume plug-in creates a volume with configured fields extended into files. The metadata
field of the VolumeSource
API object is used to configure this volume. The plug-in supports the following fields:
- Pod name
- Pod namespace
- Pod annotations
- Pod labels
Example 22.1. Downward API Volume Plug-in Configuration
For example:
Create a
volume-pod.yaml
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the
volume-pod.yaml
file:oc create -f volume-pod.yaml
$ oc create -f volume-pod.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the container’s logs and verify the presence of the configured fields:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
22.4. Consuming Container Resources Using the Downward API Copia collegamentoCollegamento copiato negli appunti!
When creating pods, you can use the downward API to inject information about computing resource requests and limits so that image and application authors can correctly create an image for specific environments.
You can do this using both the environment variable and volume plug-in methods.
22.4.1. Using Environment Variables Copia collegamentoCollegamento copiato negli appunti!
When creating a pod configuration, specify environment variables that correspond to the contents of the
resources
field in thespec.container
field:Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the resource limits are not included in the container configuration, the downward API defaults to the node’s CPU and memory allocatable values.
Create the pod from the
pod.yaml
file:oc create -f pod.yaml
$ oc create -f pod.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
22.4.2. Using the Volume Plug-in Copia collegamentoCollegamento copiato negli appunti!
When creating a pod configuration, use the
spec.volumes.downwardAPI.items
field to describe the desired resources that correspond to thespec.resources
field:Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the resource limits are not included in the container configuration, the downward API defaults to the node’s CPU and memory allocatable values.
Create the pod from the
volume-pod.yaml
file:oc create -f volume-pod.yaml
$ oc create -f volume-pod.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
22.5. Consuming Secrets Using the Downward API Copia collegamentoCollegamento copiato negli appunti!
When creating pods, you can use the downward API to inject Secrets so image and application authors can create an image for specific environments.
22.5.1. Using Environment Variables Copia collegamentoCollegamento copiato negli appunti!
Create a secret.yaml file:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
Secret
from the secret.yaml file:oc create -f secret.yaml
oc create -f secret.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
pod.yaml
file that references theusername
field from the aboveSecret
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the
pod.yaml
file:oc create -f pod.yaml
$ oc create -f pod.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the container’s logs for the
MY_SECRET_USERNAME
value:oc logs -p dapi-env-test-pod
$ oc logs -p dapi-env-test-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
22.6. Consuming ConfigMaps Using the Downward API Copia collegamentoCollegamento copiato negli appunti!
When creating pods, you can use the downward API to inject ConfigMap values so image and application authors can create an image for specific environments.
22.6.1. Using Environment Variables Copia collegamentoCollegamento copiato negli appunti!
Create a
configmap.yaml
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
ConfigMap
from theconfigmap.yaml
file:oc create -f configmap.yaml
oc create -f configmap.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
pod.yaml
file that references the aboveConfigMap
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the
pod.yaml
file:oc create -f pod.yaml
$ oc create -f pod.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the container’s logs for the
MY_CONFIGMAP_VALUE
value:oc logs -p dapi-env-test-pod
$ oc logs -p dapi-env-test-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
22.7. Environment Variable References Copia collegamentoCollegamento copiato negli appunti!
When creating pods, you can reference the value of a previously defined environment variable by using the $()
syntax. If the environment variable reference can not be resolved, the value will be left as the provided string.
22.7.1. Using Environment Variable References Copia collegamentoCollegamento copiato negli appunti!
Create a
pod.yaml
file that references an existingenvironment variable
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the
pod.yaml
file:oc create -f pod.yaml
$ oc create -f pod.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the container’s logs for the
MY_ENV_VAR_REF_ENV
value:oc logs -p dapi-env-test-pod
$ oc logs -p dapi-env-test-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
22.7.2. Escaping Environment Variable References Copia collegamentoCollegamento copiato negli appunti!
When creating a pod, you can escape an environment variable reference by using a double dollar sign. The value will then be set to a single dollar sign version of the provided value.
Create a
pod.yaml
file that references an existingenvironment variable
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the pod from the
pod.yaml
file:oc create -f pod.yaml
$ oc create -f pod.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the container’s logs for the
MY_NEW_ENV
value:oc logs -p dapi-env-test-pod
$ oc logs -p dapi-env-test-pod
Copy to Clipboard Copied! Toggle word wrap Toggle overflow