Kubernetes v1.34: Use An Init Container To Define App Environment Variables
Link⚡ TL;DR
📝 Summary
Kubernetes v1.34: Use An Init Container To Define App Environment Variables What’s this all about? How It Works A word on security Summary Kubernetes typically uses ConfigMaps and Secrets to set environment variables, which introduces additional API calls and complexity, For example, you need to separately manage the Pods of your workloads and their configurations, while ensuring orderly updates for both the configurations and the workload Pods. Alternatively, you might be using a vendor-supplied container that requires environment variables (such as a license key or a one-time token), but you don’t want to hard-code them or mount volumes just to get the job done. If that's the situation you are in, you now have a new (alpha) way to achieve that. Provided you have the EnvFiles feature gate enabled across your cluster, you can tell the kubelet to load a container's environment variables from a volume (the volume must be part of the Pod that the container belongs to). this feature gate allows you to load environment variables directly from a file in an emptyDir volume without actually mounting that file into the container. It’s a simple yet elegant solution to some surprisingly common problems. EnvFiles At its core, this feature allows you to point your container to a file, one generated by an initContainer , and have Kubernetes parse that file to set your environment variables. The file lives in an emptyDir volume (a temporary storage space that lasts as long as the pod does), Your main container doesn’t need to mount the volume. The kubelet will read the file and inject these variables when the container starts. initContainer emptyDir Here's a simple example: apiVersion : v1 kind : Pod spec : initContainers : - name : generate-config image : busybox command : [ 'sh' , '-c' , 'echo "CONFIG_VAR=HELLO" > /config/config. env' ] volumeMounts : - name : config-volume mountPath : /config containers : - name : app-container image : gcr. io/distroless/static env : - name : CONFIG_VAR valueFrom : fileKeyRef : path : config.
Open the original post ↗ https://kubernetes.io/blog/2025/09/10/kubernetes-v1-34-env-files/