Skip to content

Commit a301bc4

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

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
The env file format adheres to the [RFC .env standard](https://smartmob-rfc.readthedocs.io/en/latest/2-dotenv.html#formal-specification).
38+
39+
During container initialization, the kubelet retrieves environment variables
40+
from specified files in the `emptyDir` volume and exposes them to the container.
41+
42+
{{< note >}}
43+
All container types (initContainers, regular containers, sidecars containers,
44+
and ephemeral containers) support environment variable loading from files.
45+
46+
While these environment variables can store sensitive information,
47+
`emptyDir` volumes don't provide the same protection mechanisms as
48+
dedicated Secret objects. Therefore, exposing confidential environment variables
49+
to containers through this feature is not considered a security best practice.
50+
{{< /note >}}
51+
52+
53+
Create the Pod:
54+
55+
```shell
56+
kubectl apply -f https://k8s.io/examples/pods/inject/envars-file-container.yaml
57+
```
58+
59+
Verify that the container in the Pod is running:
60+
61+
```shell
62+
# If the new Pod isn't yet healthy, rerun this command a few times.
63+
kubectl get pods
64+
```
65+
66+
Check container logs for environment variables:
67+
68+
```shell
69+
kubectl logs dapi-test-pod -c use-envfile | grep DB_ADDRESS
70+
```
71+
72+
The output shows the values of selected environment variables:
73+
74+
```
75+
DB_ADDRESS=address
76+
```
77+
78+
## {{% heading "whatsnext" %}}
79+
80+
* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/).
81+
* Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
82+
* 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)