Skip to content

Commit f83015e

Browse files
committed
Add documentation for the EnvFiles Feature Gate
1 parent 8255f3f commit f83015e

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: EnvFiles
3+
content_type: feature_gate
4+
_build:
5+
list: never
6+
render: false
7+
8+
stages:
9+
- stage: alpha
10+
defaultValue: false
11+
fromVersion: "1.34"
12+
---
13+
Support defining container's Environment Variable Values via File.
14+
See [Define Environment Variable Values Using An Init Container](/docs/tasks/inject-data-application/define-environment-variable-via-file) for more details.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
During container initialization, the kubelet retrieves environment variables
38+
from specified files in the `emptyDir` volume and exposes them to the container.
39+
40+
{{< note >}}
41+
All container types (initContainers, regular containers, sidecars containers,
42+
and ephemeral containers) support environment variable loading from files.
43+
44+
While these environment variables can store sensitive information,
45+
`emptyDir` volumes don't provide the same protection mechanisms as
46+
dedicated Secret objects. Therefore, exposing confidential environment variables
47+
to containers through this feature is not considered a security best practice.
48+
{{< /note >}}
49+
50+
51+
Create the Pod:
52+
53+
```shell
54+
kubectl apply -f https://k8s.io/examples/pods/inject/envars-file-container.yaml
55+
```
56+
57+
Verify that the container in the Pod is running:
58+
59+
```shell
60+
# If the new Pod isn't yet healthy, rerun this command a few times.
61+
kubectl get pods
62+
```
63+
64+
Check container logs for environment variables:
65+
66+
```shell
67+
kubectl logs dapi-test-pod -c use-envfile | grep DB_ADDRESS
68+
```
69+
70+
The output shows the values of selected environment variables:
71+
72+
```
73+
DB_ADDRESS=address
74+
```
75+
76+
## `.env` File Syntax
77+
78+
The following syntax rules apply to environment files:
79+
80+
* Blank Lines: Blank lines are ignored.
81+
82+
* Leading Spaces: Leading spaces on all lines are ignored.
83+
84+
* Variable Declaration: Variables must be declared as `VAR=VAL`. Spaces surrounding `=` and trailing spaces are ignored.
85+
```
86+
VAR=VAL → VAL
87+
```
88+
89+
* Comments: Lines beginning with # are treated as comments and ignored.
90+
```
91+
# comment
92+
VAR=VAL → VAL
93+
94+
VAR=VAL # not a comment → VAL # not a comment
95+
```
96+
97+
* Line Continuation: A backslash (`\`) at the end of a variable declaration line indicates the value continues on the next line. The lines are joined with a single space.
98+
```
99+
VAR=VAL \
100+
VAL2
101+
→ VAL VAL2
102+
```
103+
104+
105+
106+
## {{% heading "whatsnext" %}}
107+
108+
* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/).
109+
* Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
110+
* Read [Expose Pod Information to Containers Through Environment Variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: envfile-test-pod
5+
spec:
6+
initContainers:
7+
- name: setup-envfile
8+
image: nginx
9+
command: ['sh', '-c', 'echo "DB_ADDRESS=address\nREST_ENDPOINT=endpoint" > /data/config.env']
10+
volumeMounts:
11+
- name: config
12+
mountPath: /data
13+
containers:
14+
- name: use-envfile
15+
image: nginx
16+
command: [ "/bin/sh", "-c", "env" ]
17+
env:
18+
- name: DB_ADDRESS
19+
valueFrom:
20+
fileKeyRef:
21+
path: config.env
22+
volumeName: config
23+
key: DB_ADDRESS
24+
optional: false
25+
restartPolicy: Never
26+
volumes:
27+
- name: config
28+
emptyDir: {}

0 commit comments

Comments
 (0)