|
| 1 | +--- |
| 2 | +title: Define Environment Variable Values Using An Init Container |
| 3 | +content_type: task |
| 4 | +min-kubernetes-server-version: v1.34 |
| 5 | +weight: 30 |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- overview --> |
| 9 | + |
| 10 | +{{< feature-state feature_gate_name="EnvFiles" >}} |
| 11 | + |
| 12 | +This page show how to configure environment variables for containers in a Pod via file. |
| 13 | + |
| 14 | +## {{% heading "prerequisites" %}} |
| 15 | + |
| 16 | +{{< include "task-tutorial-prereqs.md" >}} |
| 17 | + |
| 18 | +{{% version-check %}} |
| 19 | + |
| 20 | +<!-- steps --> |
| 21 | + |
| 22 | +## How the design works |
| 23 | + |
| 24 | +In this exercise, you will create a Pod that sources environment variables from files, |
| 25 | +projecting these values into the running container. |
| 26 | + |
| 27 | +{{% code_sample file="pods/inject/envars-file-container.yaml" %}} |
| 28 | + |
| 29 | +In this manifest, you can see the `initContainer` mounts an `emptyDir` volume and writes environment variables to a file within it, |
| 30 | +and the regular containers reference both the file and the environment variable key |
| 31 | +through the `fileKeyRef` field without needing to mount the volume. |
| 32 | +When `optional` field is set to false, the specified `key` in `fileKeyRef` must exist in the environment variables file. |
| 33 | + |
| 34 | +The volume will only be mounted to the container that writes to the file |
| 35 | +(`initContainer`), while the consumer container that consumes the environment variable will not have the volume mounted. |
| 36 | + |
| 37 | +The env file format adheres to the [RFC .env standard](https://smartmob-rfc.readthedocs.io/en/latest/2-dotenv.html#formal-specification). |
| 38 | + |
| 39 | +During container initialization, the kubelet retrieves environment variables |
| 40 | +from specified files in the `emptyDir` volume and exposes them to the container. |
| 41 | + |
| 42 | +{{< note >}} |
| 43 | +All container types (initContainers, regular containers, sidecars containers, |
| 44 | +and ephemeral containers) support environment variable loading from files. |
| 45 | + |
| 46 | +While these environment variables can store sensitive information, |
| 47 | +`emptyDir` volumes don't provide the same protection mechanisms as |
| 48 | +dedicated Secret objects. Therefore, exposing confidential environment variables |
| 49 | +to containers through this feature is not considered a security best practice. |
| 50 | +{{< /note >}} |
| 51 | + |
| 52 | + |
| 53 | +Create the Pod: |
| 54 | + |
| 55 | +```shell |
| 56 | +kubectl apply -f https://k8s.io/examples/pods/inject/envars-file-container.yaml |
| 57 | +``` |
| 58 | + |
| 59 | +Verify that the container in the Pod is running: |
| 60 | + |
| 61 | +```shell |
| 62 | +# If the new Pod isn't yet healthy, rerun this command a few times. |
| 63 | +kubectl get pods |
| 64 | +``` |
| 65 | + |
| 66 | +Check container logs for environment variables: |
| 67 | + |
| 68 | +```shell |
| 69 | +kubectl logs dapi-test-pod -c use-envfile | grep DB_ADDRESS |
| 70 | +``` |
| 71 | + |
| 72 | +The output shows the values of selected environment variables: |
| 73 | + |
| 74 | +``` |
| 75 | +DB_ADDRESS=address |
| 76 | +``` |
| 77 | + |
| 78 | +## {{% heading "whatsnext" %}} |
| 79 | + |
| 80 | +* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/). |
| 81 | +* Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/) |
| 82 | +* Read [Expose Pod Information to Containers Through Environment Variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information) |
0 commit comments