Kubernetes v1.34: Use An Init ... Note

Kubernetes v1.34: Use An Init Container To Define App Environment Variables

Kubernetes traditionally uses ConfigMaps and Secrets for environment variables, which adds complexity and extra API calls. Managing Pods and their configurations separately, especially during updates, can be challenging. This is particularly true when dealing with vendor containers requiring environment variables without wanting to hard-code them or use volume mounts.A new alpha feature called EnvFiles offers an alternative solution for these scenarios. If the EnvFiles feature gate is enabled, the kubelet can load environment variables for a container directly from a file within an emptyDir volume. This eliminates the need to mount the file into the container itself. The core functionality allows Kubernetes to parse a file, often generated by an initContainer, and set environment variables for the main container upon startup.The file resides in an emptyDir volume, which is temporary storage lasting for the pod's lifecycle. The main container does not need to mount this volume; the kubelet handles reading the file and injecting the variables. Defining these variables involves using the fileKeyRef field in the pod spec, specifying the file path and the key to extract.The file format supports a KEY=VALUE syntax, and for this alpha stage, it must be written to an emptyDir volume. An init container is required to write the file into the emptyDir volume, but the main application container does not need access to the volume. While this feature can handle sensitive data, it's important to note that node operators with filesystem access could potentially retrieve this data from emptyDir volumes. Therefore, robust cluster security policies are essential to protect nodes against unauthorized access when storing sensitive information. This EnvFiles feature simplifies application authoring and enables new use cases by reducing complex workarounds.