Add envFrom details in docs (#47709)
* Add envFrom details in docs * Add envFrom details in docs * deleted command prompt * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Andrii Holovin <Andygol@users.noreply.github.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Andrii Holovin <Andygol@users.noreply.github.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Andrii Holovin <Andygol@users.noreply.github.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Andrii Holovin <Andygol@users.noreply.github.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Andrii Holovin <Andygol@users.noreply.github.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Andrii Holovin <Andygol@users.noreply.github.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Andrii Holovin <Andygol@users.noreply.github.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Tim Bannister <tim@scalefactory.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Tim Bannister <tim@scalefactory.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Tim Bannister <tim@scalefactory.com> * added shortcode * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Qiming Teng <tengqm@outlook.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Qiming Teng <tengqm@outlook.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Qiming Teng <tengqm@outlook.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Qiming Teng <tengqm@outlook.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Qiming Teng <tengqm@outlook.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Qiming Teng <tengqm@outlook.com> * Update content/en/docs/concepts/configuration/configmap.md Co-authored-by: Qiming Teng <tengqm@outlook.com> * Accept review comments --------- Co-authored-by: Andrii Holovin <Andygol@users.noreply.github.com> Co-authored-by: Tim Bannister <tim@scalefactory.com> Co-authored-by: Qiming Teng <tengqm@outlook.com>
This commit is contained in:
parent
7ba952a4e0
commit
2ccaf064f8
|
@ -221,6 +221,54 @@ in a Pod:
|
|||
in the specified environment variables.
|
||||
|
||||
This is an example of defining a ConfigMap as a pod environment variable:
|
||||
|
||||
The following ConfigMap (myconfigmap.yaml) stores two properties: username and access_level:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: myconfigmap
|
||||
data:
|
||||
username: k8s-admin
|
||||
access_level: "1"
|
||||
```
|
||||
|
||||
The following command will create the ConfigMap object:
|
||||
|
||||
```shell
|
||||
kubectl apply -f myconfigmap.yaml
|
||||
```
|
||||
|
||||
The following Pod consumes the content of the ConfigMap as environment variables:
|
||||
|
||||
{{% code_sample file="configmap/env-configmap.yaml" %}}
|
||||
|
||||
The `envFrom` field instructs Kubernetes to create environment variables from the sources nested within it.
|
||||
The inner `configMapRef` refers to a ConfigMap by its name and selects all its key-value pairs.
|
||||
Add the Pod to your cluster, then retrieve its logs to see the output from the printenv command.
|
||||
This should confirm that the two key-value pairs from the ConfigMap have been set as environment variables:
|
||||
|
||||
```shell
|
||||
kubectl apply -f env-configmap.yaml
|
||||
```
|
||||
```shell
|
||||
kubectl logs pod/ env-configmap
|
||||
```
|
||||
The output is similar to this:
|
||||
```console
|
||||
...
|
||||
username: "k8s-admin"
|
||||
access_level: "1"
|
||||
...
|
||||
```
|
||||
|
||||
Sometimes a Pod won't require access to all the values in a ConfigMap.
|
||||
For example, you could have another Pod which only uses the username value from the ConfigMap.
|
||||
For this use case, you can use the `env.valueFrom` syntax instead, which lets you select individual keys in
|
||||
a ConfigMap. The name of the environment variable can also be different from the key within the ConfigMap.
|
||||
For example:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
|
@ -236,9 +284,13 @@ spec:
|
|||
configMapKeyRef:
|
||||
name: myconfigmap
|
||||
key: username
|
||||
|
||||
```
|
||||
|
||||
In the Pod created from this manifest, you will see that the environment variable
|
||||
`CONFIGMAP_USERNAME` is set to the value of the `username` value from the ConfigMap.
|
||||
Other keys from the ConfigMap data are not copied into the environment.
|
||||
|
||||
|
||||
It's important to note that the range of characters allowed for environment
|
||||
variable names in pods is [restricted](/docs/tasks/inject-data-application/define-environment-variable-container/#using-environment-variables-inside-of-your-config).
|
||||
If any keys do not meet the rules, those keys are not made available to your container, though
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: env-configmap
|
||||
spec:
|
||||
containers:
|
||||
- name: app
|
||||
command: ["/bin/sh", "-c", "printenv"]
|
||||
image: busybox:latest
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: myconfigmap
|
Loading…
Reference in New Issue