|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes v1.34: Use An Init Container To Define App Environment Variables" |
| 4 | +date: 2025-0X-XX |
| 5 | +draft: true |
| 6 | +slug: kubernetes-v1-34-env-files |
| 7 | +author: > |
| 8 | + HirazawaUi |
| 9 | +--- |
| 10 | + |
| 11 | +Kubernetes typically uses ConfigMaps and Secrets to set environment variables, |
| 12 | +which introduces additional API calls and complexity, |
| 13 | +For example, you need to separately manage the Pods of your workloads |
| 14 | +and their configurations, while ensuring orderly |
| 15 | +updates for both the configurations and the workload Pods. |
| 16 | + |
| 17 | +Alternatively, you might be using a vendor-supplied container |
| 18 | +that requires environment variables (such as a license key or a one-time token), |
| 19 | +but you don’t want to hard-code them or mount volumes just to get the job done. |
| 20 | + |
| 21 | +If that's the situation you are in, you now have a new (alpha) way to |
| 22 | +achieve that. Provided you have the `EnvFiles` |
| 23 | +[feature gate](/docs/reference/command-line-tools-reference/feature-gates/) |
| 24 | +enabled across your cluster, you can tell the kubelet to load a container's |
| 25 | +environment variables from a volume (the volume must be part of the Pod that |
| 26 | +the container belongs to). |
| 27 | +this feature gate allows you to load environment variables directly from a file in an emptyDir volume |
| 28 | +without actually mounting that file into the container. |
| 29 | +It’s a simple yet elegant solution to some surprisingly common problems. |
| 30 | + |
| 31 | +## What’s this all about? |
| 32 | +At its core, this feature allows you to point your container to a file, |
| 33 | +one generated by an `initContainer`, |
| 34 | +and have Kubernetes parse that file to set your environment variables. |
| 35 | +The file lives in an `emptyDir` volume (a temporary storage space that lasts as long as the pod does), |
| 36 | +Your main container doesn’t need to mount the volume. |
| 37 | +The kubelet will read the file and inject these variables when the container starts. |
| 38 | + |
| 39 | +## How It Works |
| 40 | +Here's a simple example: |
| 41 | +```yaml |
| 42 | +apiVersion: v1 |
| 43 | +kind: Pod |
| 44 | +spec: |
| 45 | + initContainers: |
| 46 | + - name: generate-config |
| 47 | + image: busybox |
| 48 | + command: ['sh', '-c', 'echo "CONFIG_VAR=HELLO" > /config/config.env'] |
| 49 | + volumeMounts: |
| 50 | + - name: config-volume |
| 51 | + mountPath: /config |
| 52 | + containers: |
| 53 | + - name: app-container |
| 54 | + image: gcr.io/distroless/static |
| 55 | + env: |
| 56 | + - name: CONFIG_VAR |
| 57 | + valueFrom: |
| 58 | + fileKeyRef: |
| 59 | + path: config.env |
| 60 | + volumeName: config-volume |
| 61 | + key: CONFIG_VAR |
| 62 | + volumes: |
| 63 | + - name: config-volume |
| 64 | + emptyDir: {} |
| 65 | +``` |
| 66 | +
|
| 67 | +Using this approach is a breeze. |
| 68 | +You define your environment variables in the pod spec using the `fileKeyRef` field, |
| 69 | +which tells Kubernetes where to find the file and which key to pull. |
| 70 | +The file itself follows the de-facto standard for `.env` syntax (think `KEY=VALUE`), |
| 71 | +and (for this alpha stage at least) you must ensure that it is written into |
| 72 | +an `emptyDir` volume. Other volume types aren't supported for this feature. |
| 73 | +At least one init container must mount that `emptyDir` volume (to write the file), |
| 74 | +but the main container doesn’t need to—it just gets the variables handed to it at startup. |
| 75 | + |
| 76 | +## A word on security |
| 77 | +While this feature supports handling sensitive data such as keys or tokens, |
| 78 | +note that its implementation relies on `emptyDir` volumes mounted into pod. |
| 79 | +Operators with node filesystem access could therefore |
| 80 | +easily retrieve this sensitive data through pod directory paths. |
| 81 | + |
| 82 | +If storing sensitive data like keys or tokens using this feature, |
| 83 | +ensure your cluster security policies effectively protect nodes |
| 84 | +against unauthorized access to prevent exposure of confidential information. |
0 commit comments