Skip to content

Commit 3607f29

Browse files
committed
Add documentation for the EnvFiles Feature Gate
1 parent e20b18f commit 3607f29

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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"
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Using Files to Define Environment Variables for Containers in a Pod
3+
content_type: task
4+
weight: 30
5+
---
6+
7+
<!-- overview -->
8+
9+
{{< feature-state feature_gate_name="EnvFiles" >}}
10+
11+
Defining Environment Variable Values via Files.
12+
To use this feature, ensure your Kubernetes version v1.34 or later
13+
with the EnvFiles feature gate enabled.
14+
15+
<!-- steps -->
16+
17+
## Using Files to Define Environment Variables for Containers in a Pod
18+
19+
In this exercise, you'll create a Pod that sources environment variables from files,
20+
projecting these values into the running container.
21+
22+
{{% code_sample file="pods/inject/ddapi-envars-file-container.yaml" %}}
23+
24+
In this manifest, you can see:
25+
26+
An initContainer mounts an emptyDir volume and writes environment variables to a file within it.
27+
28+
The main container references this file through valueFrom.fileKeyRef without mounting the volume.
29+
30+
The kubelet automatically projects the values from the file into the container's environment.
31+
32+
{{< note >}}
33+
Note: All container types (initContainers, main containers, sidecars,
34+
and ephemeral containers) support environment variable loading from files.
35+
{{< /note >}}
36+
37+
Create the Pod:
38+
39+
```shell
40+
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-file-container.yaml
41+
```
42+
43+
Verify that the container in the Pod is running:
44+
45+
```shell
46+
# If the new Pod isn't yet healthy, rerun this command a few times.
47+
kubectl get pods
48+
```
49+
50+
Check container logs for environment variables:
51+
52+
```shell
53+
kubectl logs dapi-test-static-pod -c use-envfile | grep CONFIG_MAIN
54+
```
55+
56+
The output shows the values of selected environment variables:
57+
58+
```
59+
CONFIG_MAIN=CONFIG_INIT
60+
```
61+
62+
## {{% heading "whatsnext" %}}
63+
64+
65+
* Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
66+
* Read the [`spec`](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec)
67+
API definition for Pod. This includes the definition of Container (part of Pod).
68+
* Read the list of [available fields](/docs/concepts/workloads/pods/downward-api/#available-fields) that you
69+
can expose using the downward API.
70+
71+
Read about Pods, containers and environment variables in the legacy API reference:
72+
73+
* [PodSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
74+
* [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
75+
* [EnvVar](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core)
76+
* [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core)
77+
* [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core)
78+
* [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)