Use Secret to Supply Environment Variables¶
Let's see how we can use Secret
to supply environment variables to containers in a pod:
Step 1: Create a Secret¶
Let's create a Secret
with data that contains the required environment variables:
Apply the manifest to create the Secret:
Step 2: Verify Secret¶
Step 3: Create Pods That Uses Environment Variables¶
Let's create pods that uses Secret
to set environment variables for the container. We'll use a deployment to create pods:
Observe that we are using the keyword envFrom
to supply a list of environment variables from the Secret my-secret
.
Apply the manifest to create deployment:
Step 4: Verify Deployment and Pods¶
Step 5: Verify Environment Variables¶
Start a shell session inside the container:
List environment variables available to the container:
You'll see a list of environment variables available to the container. This includes both system-provided
as well as user-provided
environment variables.
Print values of the environment variables we set:
# Print value of the environment variable username
echo $username
# Print value of the environment variable password
echo $password
Note
Kubernetes automatically does base64
decoding for secrets used in the pod.
Clean Up¶
Assuming your folder structure looks like the one below:
Let's delete all the resources we created:
Tip
Since Secret
is similar to ConfigMap
you can repeat all the examples that we discussed in ConfigMap
section for Secret
as well. Just replace configMapRef
by secretRef
and configMapKeyRef
by secretKeyRef
.