This page show how to configure environment variables for containers in a Pod via file.
You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:
Your Kubernetes server must be at or later than version v1.34.
To check the version, enter kubectl version.
In this exercise, you will create a Pod that sources environment variables from files, projecting these values into the running container.
apiVersion: v1
kind: Pod
metadata:
name: envfile-test-pod
spec:
initContainers:
- name: setup-envfile
image: nginx
command: ['sh', '-c', "echo \"DB_ADDRESS=\'address\'\nREST_ENDPOINT=\'endpoint\'\" > /data/config.env"]
volumeMounts:
- name: config
mountPath: /data
containers:
- name: use-envfile
image: nginx
command: [ "/bin/sh", "-c", "env" ]
env:
- name: DB_ADDRESS
valueFrom:
fileKeyRef:
path: config.env
volumeName: config
key: DB_ADDRESS
optional: false
restartPolicy: Never
volumes:
- name: config
emptyDir: {}In this manifest, you can see the initContainer mounts an emptyDir volume and writes environment variables to a file within it,
and the regular containers reference both the file and the environment variable key
through the fileKeyRef field without needing to mount the volume.
When the optional field is set to false (the default), the file and the specified key must exist, or the container fails to start with CreateContainerConfigError. When optional is set to true and the file or the key is missing, the kubelet does not set the environment variable.
The volume will only be mounted to the container that writes to the file
(initContainer), while the consumer container that consumes the environment variable will not have the volume mounted.
The env file format adheres to the kubernetes env file standard.
During container initialization, the kubelet retrieves environment variables
from specified files in the emptyDir volume and exposes them to the container.
All container types (initContainers, regular containers, sidecars containers, and ephemeral containers) support environment variable loading from files.
While these environment variables can store sensitive information,
emptyDir volumes don't provide the same protection mechanisms as
dedicated Secret objects. Therefore, exposing confidential environment variables
to containers through this feature is not considered a security best practice.
Create the Pod:
kubectl apply -f https://k8s.io/examples/pods/inject/envars-file-container.yaml
Verify that the container in the Pod is running:
# If the new Pod isn't yet healthy, rerun this command a few times.
kubectl get pods
Check container logs for environment variables:
kubectl logs envfile-test-pod -c use-envfile | grep DB_ADDRESS
The output shows the values of selected environment variables:
DB_ADDRESS=address
The env file format used by Kubernetes is a well-defined subset of the environment variable semantics for POSIX-compliant bash. Any env file supported by Kubernetes will produce the same environment variables as when interpreted by a POSIX-compliant bash. However, POSIX-compliant bash supports some additional formats that Kubernetes does not accept.
Example:
MY_VAR='my-literal-value'
VAR='value', with no whitespace between the variable name, =, and the opening quote. Leading spaces on a line are ignored; blank lines are ignored.VAR='' to set the variable to an empty string. A key with an empty value exists, so it satisfies optional: false.').# are treated as comments and ignored. A comment can also follow the closing quote after a space or tab, as in VAR='value' # comment. A # character inside a single-quoted value is not a comment.Examples:
# comment
DB_ADDRESS='address'
MULTI='line1
line2'
VAR=value — not supported.VAR="value" — not supported.= is prohibited:VAR ='value' and VAR= 'value' are not supported. In bash, VAR= 'value' runs value as a command and does not set VAR.VAR='value'#comment is not supported. In bash, the # here is part of the value.VAR='val1''val2' — not supported.VAR='a'$OTHER or VAR=${OTHER} — not supported.The strict single-quote requirement ensures the value is taken literally by the kubelet when loading environment variables from files.