|
| 1 | +--- |
| 2 | +title: Using Files to Define Environment Variables for Containers in a Pod |
| 3 | +content_type: task |
| 4 | +weight: 30 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- overview --> |
| 8 | + |
| 9 | +{{< feature-state feature_gate_name="EnvFiles" >}} |
| 10 | + |
| 11 | +Defining Environment Variable Values via Files. |
| 12 | +To use this feature, ensure your Kubernetes version v1.34 or later |
| 13 | +with the EnvFiles feature gate enabled. |
| 14 | + |
| 15 | +<!-- steps --> |
| 16 | + |
| 17 | +## Using Files to Define Environment Variables for Containers in a Pod |
| 18 | + |
| 19 | +In this exercise, you'll create a Pod that sources environment variables from files, |
| 20 | +projecting these values into the running container. |
| 21 | + |
| 22 | +{{% code_sample file="pods/inject/ddapi-envars-file-container.yaml" %}} |
| 23 | + |
| 24 | +In this manifest, you can see: |
| 25 | + |
| 26 | +An initContainer mounts an emptyDir volume and writes environment variables to a file within it. |
| 27 | + |
| 28 | +The main container references this file through valueFrom.fileKeyRef without mounting the volume. |
| 29 | + |
| 30 | +The kubelet automatically projects the values from the file into the container's environment. |
| 31 | + |
| 32 | +{{< note >}} |
| 33 | +Note: All container types (initContainers, main containers, sidecars, |
| 34 | + and ephemeral containers) support environment variable loading from files. |
| 35 | +{{< /note >}} |
| 36 | + |
| 37 | +Create the Pod: |
| 38 | + |
| 39 | +```shell |
| 40 | +kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-file-container.yaml |
| 41 | +``` |
| 42 | + |
| 43 | +Verify that the container in the Pod is running: |
| 44 | + |
| 45 | +```shell |
| 46 | +# If the new Pod isn't yet healthy, rerun this command a few times. |
| 47 | +kubectl get pods |
| 48 | +``` |
| 49 | + |
| 50 | +Check container logs for environment variables: |
| 51 | + |
| 52 | +```shell |
| 53 | +kubectl logs dapi-test-static-pod -c use-envfile | grep CONFIG_MAIN |
| 54 | +``` |
| 55 | + |
| 56 | +The output shows the values of selected environment variables: |
| 57 | + |
| 58 | +``` |
| 59 | +CONFIG_MAIN=CONFIG_INIT |
| 60 | +``` |
| 61 | + |
| 62 | +## {{% heading "whatsnext" %}} |
| 63 | + |
| 64 | + |
| 65 | +* Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/) |
| 66 | +* Read the [`spec`](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec) |
| 67 | + API definition for Pod. This includes the definition of Container (part of Pod). |
| 68 | +* Read the list of [available fields](/docs/concepts/workloads/pods/downward-api/#available-fields) that you |
| 69 | + can expose using the downward API. |
| 70 | + |
| 71 | +Read about Pods, containers and environment variables in the legacy API reference: |
| 72 | + |
| 73 | +* [PodSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core) |
| 74 | +* [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core) |
| 75 | +* [EnvVar](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core) |
| 76 | +* [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core) |
| 77 | +* [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core) |
| 78 | +* [ResourceFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core) |
0 commit comments