|
| 1 | +--- |
| 2 | +title: Defining Environment Variable Values via File |
| 3 | +content_type: task |
| 4 | +weight: 30 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- overview --> |
| 8 | + |
| 9 | +{{< feature-state feature_gate_name="EnvFiles" >}} |
| 10 | + |
| 11 | +This page show how to configure environment variables for containers in a Pod via file. |
| 12 | +To use this feature, ensure your Kubernetes version v1.34 or later |
| 13 | +with the EnvFiles feature gate enabled. |
| 14 | + |
| 15 | +## {{% heading "prerequisites" %}} |
| 16 | + |
| 17 | +{{< include "task-tutorial-prereqs.md" >}} |
| 18 | + |
| 19 | +<!-- steps --> |
| 20 | + |
| 21 | +## Using File to Define Environment Variables for Containers in a Pod |
| 22 | + |
| 23 | +In this exercise, you'll create a Pod that sources environment variables from files, |
| 24 | +projecting these values into the running container. |
| 25 | + |
| 26 | +{{% code_sample file="pods/inject/dapi-envars-file-container.yaml" %}} |
| 27 | + |
| 28 | +In this manifest, you can see the `initContainer` mounts an `emptyDir` volume and writes environment variables to a file within it, |
| 29 | +and the regular containers reference both the file and the environment variable key |
| 30 | +through the `fileKeyRef` field without needing to mount the volume. |
| 31 | +When `optional` field is set to false, the specified `key` in `fileKeyRef` must exist in the environment variables file. |
| 32 | + |
| 33 | +Please note that the volume will only be mounted to the environment variable producer container |
| 34 | +(initContainer), while the consumer container that consumes the environment variable will not have the volume mounted. |
| 35 | + |
| 36 | +During container initialization, the kubelet retrieves environment variables |
| 37 | +from specified files in the `emptyDir` volume and exposes them to the container. |
| 38 | + |
| 39 | +{{< note >}} |
| 40 | +All container types (`initContainers`, `regular containers`, `sidecars containers`, |
| 41 | +and `ephemeral containers`) support environment variable loading from files. |
| 42 | + |
| 43 | +While these environment variables can store sensitive information, |
| 44 | +please note that `emptyDir` volumes don't provide the same protection mechanisms as |
| 45 | +dedicated Secret objects. Therefore, exposing confidential environment variables |
| 46 | +to containers through this feature is not considered a security best practice. |
| 47 | +{{< /note >}} |
| 48 | + |
| 49 | + |
| 50 | +Create the Pod: |
| 51 | + |
| 52 | +```shell |
| 53 | +kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-file-container.yaml |
| 54 | +``` |
| 55 | + |
| 56 | +Verify that the container in the Pod is running: |
| 57 | + |
| 58 | +```shell |
| 59 | +# If the new Pod isn't yet healthy, rerun this command a few times. |
| 60 | +kubectl get pods |
| 61 | +``` |
| 62 | + |
| 63 | +Check container logs for environment variables: |
| 64 | + |
| 65 | +```shell |
| 66 | +kubectl logs dapi-test-pod -c use-envfile | grep CONFIG_MAIN |
| 67 | +``` |
| 68 | + |
| 69 | +The output shows the values of selected environment variables: |
| 70 | + |
| 71 | +``` |
| 72 | +CONFIG_MAIN=CONFIG_INIT |
| 73 | +``` |
| 74 | + |
| 75 | +## {{% heading "whatsnext" %}} |
| 76 | + |
| 77 | + |
| 78 | +* Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/) |
| 79 | +* Read the [`spec`](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec) |
| 80 | + API definition for Pod. This includes the definition of Container (part of Pod). |
| 81 | +* Read the list of [available fields](/docs/concepts/workloads/pods/downward-api/#available-fields) that you |
| 82 | + can expose using the downward API. |
| 83 | + |
| 84 | +Read about Pods, containers and environment variables in the legacy API reference: |
| 85 | + |
| 86 | +* [PodSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core) |
| 87 | +* [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core) |
| 88 | +* [EnvVar](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core) |
| 89 | +* [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core) |
| 90 | +* [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core) |
| 91 | +* [ResourceFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core) |
0 commit comments