Define container environment variables using Secret data (#13800)
* Example for injecting single secret as env variable Define a container environment variable with data from a single Secret * Env variable using secret data * Update pod-single-secret-env-variable.yaml * Adding example for multiple env variable using secrets Adding example for multiple env variables using secrets * Create pod-secret-envFrom.yaml * Update pod-multiple-secret-env-variable.yaml * Update pod-multiple-secret-env-variable.yaml * Update pod-multiple-secret-env-variable.yaml * Update pod-single-secret-env-variable.yaml * Update pod-multiple-secret-env-variable.yaml * Update pod-single-secret-env-variable.yaml * Different ways to add env variable using secret Adding different ways to add env variable in pod using secret * Correcting formatting issues Correcting formatting issues * Correcting format issue
This commit is contained in:
committed by
Kubernetes Prow Robot
parent
b914148850
commit
67aa1fa26a
@@ -144,47 +144,77 @@ is exposed:
|
|||||||
39528$vdg7Jb
|
39528$vdg7Jb
|
||||||
```
|
```
|
||||||
|
|
||||||
## Create a Pod that has access to the secret data through environment variables
|
## Define container environment variables using Secret data
|
||||||
|
|
||||||
Here is a configuration file you can use to create a Pod:
|
### Define a container environment variable with data from a single Secret
|
||||||
|
|
||||||
{{< codenew file="pods/inject/secret-envars-pod.yaml" >}}
|
* Define an environment variable as a key-value pair in a Secret:
|
||||||
|
|
||||||
1. Create the Pod:
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
kubectl apply -f https://k8s.io/examples/pods/inject/secret-envars-pod.yaml
|
kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Verify that your Pod is running:
|
* Assign the `backend-username` value defined in the Secret to the `SECRET_USERNAME` environment variable in the Pod specification.
|
||||||
|
|
||||||
|
{{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}}
|
||||||
|
|
||||||
|
* Create the Pod:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
kubectl get pod secret-envars-test-pod
|
kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
Output:
|
* Now, the Pod’s output includes environment variable `SECRET_USERNAME=backend-admin`
|
||||||
|
|
||||||
|
|
||||||
|
### Define container environment variables with data from multiple Secrets
|
||||||
|
|
||||||
|
* As with the previous example, create the Secrets first.
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
NAME READY STATUS RESTARTS AGE
|
kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
|
||||||
secret-envars-test-pod 1/1 Running 0 4m
|
|
||||||
|
kubectl create secret generic db-user --from-literal=db-username='db-admin'
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Get a shell into the Container that is running in your Pod:
|
* Define the environment variables in the Pod specification.
|
||||||
|
|
||||||
|
{{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}}
|
||||||
|
|
||||||
|
* Create the Pod:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
kubectl exec -it secret-envars-test-pod -- /bin/bash
|
kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
1. In your shell, display the environment variables:
|
* Now, the Pod’s output includes `BACKEND_USERNAME=backend-admin` and `DB_USERNAME=db-admin` environment variables.
|
||||||
|
|
||||||
|
|
||||||
|
## Configure all key-value pairs in a Secret as container environment variables
|
||||||
|
|
||||||
|
{{< note >}}
|
||||||
|
This functionality is available in Kubernetes v1.6 and later.
|
||||||
|
{{< /note >}}
|
||||||
|
|
||||||
|
* Create a Secret containing multiple key-value pairs
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
root@secret-envars-test-pod:/# printenv
|
kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb'
|
||||||
```
|
```
|
||||||
|
|
||||||
The output includes your username and password:
|
* Use envFrom to define all of the Secret’s data as container environment variables. The key from the Secret becomes the environment variable name in the Pod.
|
||||||
|
|
||||||
|
{{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}}
|
||||||
|
|
||||||
|
* Create the Pod:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
...
|
kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml
|
||||||
SECRET_USERNAME=my-app
|
|
||||||
...
|
|
||||||
SECRET_PASSWORD=39528$vdg7Jb
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* Now, the Pod’s output includes `username=my-app` and `password=39528$vdg7Jb` environment variables.
|
||||||
|
|
||||||
|
|
||||||
{{% /capture %}}
|
{{% /capture %}}
|
||||||
|
|
||||||
{{% capture whatsnext %}}
|
{{% capture whatsnext %}}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: envvars-multiple-secrets
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: envars-test-container
|
||||||
|
image: nginx
|
||||||
|
env:
|
||||||
|
- name: BACKEND_USERNAME
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: backend-user
|
||||||
|
key: backend-username
|
||||||
|
- name: DB_USERNAME
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: db-user
|
||||||
|
key: db-username
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: envfrom-secret
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: envars-test-container
|
||||||
|
image: nginx
|
||||||
|
envFrom:
|
||||||
|
- secretRef:
|
||||||
|
name: test-secret
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: env-single-secret
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: envars-test-container
|
||||||
|
image: nginx
|
||||||
|
env:
|
||||||
|
- name: SECRET_USERNAME
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: backend-user
|
||||||
|
key: backend-username
|
||||||
Reference in New Issue
Block a user