0
0
Kubernetesdevops~30 mins

Using Secrets as environment variables in Kubernetes - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Secrets as environment variables
📖 Scenario: You are deploying a simple web application on Kubernetes. The application needs a secret password to connect to a database. You will create a Kubernetes Secret and then use it as an environment variable inside a Pod.
🎯 Goal: Create a Kubernetes Secret with a password, configure a Pod to use this Secret as an environment variable, and verify the environment variable is set inside the Pod.
📋 What You'll Learn
Create a Secret named db-password with the key password and value myS3cretPass
Create a Pod named secret-env-pod that uses the Secret db-password as an environment variable named DB_PASSWORD
Verify the environment variable DB_PASSWORD inside the Pod by printing it
💡 Why This Matters
🌍 Real World
Storing sensitive information like passwords, API keys, or tokens securely in Kubernetes and injecting them into applications without hardcoding.
💼 Career
Kubernetes Secrets are essential for DevOps engineers and developers to manage sensitive data securely in cloud-native applications.
Progress0 / 4 steps
1
Create the Secret manifest
Create a YAML file named secret.yaml that defines a Secret called db-password with the key password and the value myS3cretPass encoded in base64 as bXlTM3NyZXRQYXNz.
Kubernetes
Need a hint?

Use type: Opaque and put the base64 encoded password under data.

2
Create the Pod manifest using the Secret as environment variable
Create a YAML file named pod.yaml that defines a Pod called secret-env-pod with one container named app using the image busybox. Add an environment variable named DB_PASSWORD that gets its value from the Secret db-password key password. The container should run the command sleep 3600 to keep it alive.
Kubernetes
Need a hint?

Use env with valueFrom.secretKeyRef to reference the Secret key.

3
Deploy the Secret and Pod to Kubernetes
Run the commands to create the Secret and Pod in your Kubernetes cluster using the YAML files secret.yaml and pod.yaml. Use kubectl apply -f secret.yaml and kubectl apply -f pod.yaml.
Kubernetes
Need a hint?

Use kubectl apply -f to create or update resources from YAML files.

4
Verify the environment variable inside the Pod
Run the command kubectl exec secret-env-pod -- printenv DB_PASSWORD to print the value of the environment variable DB_PASSWORD inside the Pod. This should output myS3cretPass.
Kubernetes
Need a hint?

Use kubectl exec with printenv to check environment variables inside the Pod.