Skip to content

Commit 6a02723

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

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
title: Defining Environment Variable Values via File
3+
content_type: task
4+
weight: 30
5+
---
6+
7+
<!-- overview -->
8+
9+
{{< feature-state feature_gate_name="EnvFiles" >}}
10+
11+
This page show how to configure environment variables for containers in a Pod via file.
12+
To use this feature, ensure your Kubernetes version v1.34 or later
13+
with the EnvFiles feature gate enabled.
14+
15+
## {{% heading "prerequisites" %}}
16+
17+
{{< include "task-tutorial-prereqs.md" >}}
18+
19+
<!-- steps -->
20+
21+
## Using File to Define Environment Variables for Containers in a Pod
22+
23+
In this exercise, you'll create a Pod that sources environment variables from files,
24+
projecting these values into the running container.
25+
26+
{{% code_sample file="pods/inject/dapi-envars-file-container.yaml" %}}
27+
28+
In this manifest, you can see the `initContainer` mounts an `emptyDir` volume and writes environment variables to a file within it,
29+
and the regular containers reference both the file and the environment variable key
30+
through the `fileKeyRef` field without needing to mount the volume.
31+
When `optional` field is set to false, the specified `key` in `fileKeyRef` must exist in the environment variables file.
32+
33+
Please note that the volume will only be mounted to the environment variable producer container
34+
(initContainer), while the consumer container that consumes the environment variable will not have the volume mounted.
35+
36+
During container initialization, the kubelet retrieves environment variables
37+
from specified files in the `emptyDir` volume and exposes them to the container.
38+
39+
{{< note >}}
40+
All container types (`initContainers`, `regular containers`, `sidecars containers`,
41+
and `ephemeral containers`) support environment variable loading from files.
42+
43+
While these environment variables can store sensitive information,
44+
please note that `emptyDir` volumes don't provide the same protection mechanisms as
45+
dedicated Secret objects. Therefore, exposing confidential environment variables
46+
to containers through this feature is not considered a security best practice.
47+
{{< /note >}}
48+
49+
50+
Create the Pod:
51+
52+
```shell
53+
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-file-container.yaml
54+
```
55+
56+
Verify that the container in the Pod is running:
57+
58+
```shell
59+
# If the new Pod isn't yet healthy, rerun this command a few times.
60+
kubectl get pods
61+
```
62+
63+
Check container logs for environment variables:
64+
65+
```shell
66+
kubectl logs dapi-test-pod -c use-envfile | grep CONFIG_MAIN
67+
```
68+
69+
The output shows the values of selected environment variables:
70+
71+
```
72+
CONFIG_MAIN=CONFIG_INIT
73+
```
74+
75+
## {{% heading "whatsnext" %}}
76+
77+
78+
* Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
79+
* Read the [`spec`](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec)
80+
API definition for Pod. This includes the definition of Container (part of Pod).
81+
* Read the list of [available fields](/docs/concepts/workloads/pods/downward-api/#available-fields) that you
82+
can expose using the downward API.
83+
84+
Read about Pods, containers and environment variables in the legacy API reference:
85+
86+
* [PodSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
87+
* [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
88+
* [EnvVar](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core)
89+
* [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core)
90+
* [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core)
91+
* [ResourceFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: dapi-test-pod
5+
spec:
6+
initContainers:
7+
- name: setup-envfile
8+
image: nginx
9+
command: ['sh', '-c', 'echo "CONFIG_INIT" > /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: CONFIG_MAIN
19+
valueFrom:
20+
fileKeyRef:
21+
path: config.env
22+
volumeName: config
23+
key: CONFIG_INIT
24+
optional: false
25+
restartPolicy: Never
26+
volumes:
27+
- name: config

0 commit comments

Comments
 (0)