📖 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
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
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
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
Hint
Use kubectl exec with printenv to check environment variables inside the Pod.
Practice
(1/5)
1. What is the main purpose of using Kubernetes Secrets as environment variables in a Pod?
easy
A. To keep sensitive data like passwords out of the container image and code
B. To speed up container startup time
C. To increase the size of the container image
D. To make the container logs more detailed
Solution
Step 1: Understand what Secrets store
Kubernetes Secrets store sensitive data such as passwords or tokens securely.
Step 2: Understand environment variables usage
Using Secrets as environment variables keeps sensitive data out of code and container images, improving security.
Final Answer:
To keep sensitive data like passwords out of the container image and code -> Option A
Quick Check:
Secrets protect sensitive data = A [OK]
Hint: Secrets keep sensitive data safe outside code [OK]
Common Mistakes:
Thinking Secrets speed up container startup
Believing Secrets increase container size
Assuming Secrets add logging details
2. Which of the following is the correct syntax to use a Kubernetes Secret named mysecret as an environment variable API_KEY in a Pod spec?
easy
A. "env": [{"secretKeyRef": {"name": "mysecret", "key": "api_key"}}]
B. "env": [{"name": "API_KEY", "value": "mysecret"}]
C. "env": [{"name": "API_KEY", "valueFrom": {"secretKeyRef": {"name": "mysecret", "key": "api_key"}}}]
D. "env": [{"name": "API_KEY", "secretRef": {"name": "mysecret"}}]
Solution
Step 1: Identify correct env var syntax for Secrets
The correct syntax uses valueFrom.secretKeyRef with name and key fields.
Step 2: Match syntax to options
"env": [{"name": "API_KEY", "valueFrom": {"secretKeyRef": {"name": "mysecret", "key": "api_key"}}}] matches the correct structure exactly, specifying the secret name and key.
Assuming the Secret db-secret has the key password with value supersecret123.
medium
A. db-secret
B. DB_PASSWORD
C. password
D. supersecret123
Solution
Step 1: Understand valueFrom.secretKeyRef usage
The environment variable gets its value from the Secret named db-secret and key password.
Step 2: Check Secret key value
The Secret key password holds the value supersecret123, so that is assigned to DB_PASSWORD.
Final Answer:
supersecret123 -> Option D
Quick Check:
Env var gets Secret key's value = B [OK]
Hint: Env var equals Secret's key value, not name or key name [OK]
Common Mistakes:
Using Secret name as value
Using key name as value
Confusing env var name with Secret key
4. You created a Secret named app-secret with key token. You try to use it in a Pod environment variable but get an error: secret "app-secret" not found. What is the most likely cause?
medium
A. The Secret was created in a different namespace than the Pod
B. The Secret key name is misspelled in the Pod spec
C. The Pod spec uses value instead of valueFrom
D. The container image does not support environment variables
Solution
Step 1: Understand Secret scope in Kubernetes
Secrets are namespace-scoped. If the Pod is in a different namespace, it cannot access the Secret.
Step 2: Analyze error message
The error secret "app-secret" not found usually means the Secret does not exist in the Pod's namespace.
Final Answer:
The Secret was created in a different namespace than the Pod -> Option A
Quick Check:
Secret must be in same namespace as Pod = C [OK]
Hint: Check Secret and Pod namespaces match [OK]
Common Mistakes:
Ignoring namespace differences
Assuming key name typo causes 'not found' error
Blaming container image for env var errors
5. You want to mount multiple keys from a Secret named config-secret as environment variables in a Pod. Which is the best way to do this efficiently?
hard
A. Mount the Secret as a volume and read files inside the container
B. List each key separately under env with individual valueFrom.secretKeyRef entries
C. Create multiple Secrets, one per key, and reference each in env
D. Encode all keys into one string and store as a single Secret key
Solution
Step 1: Understand environment variable mapping from Secrets
You can map each Secret key to an environment variable by listing them separately under env with valueFrom.secretKeyRef.
Step 2: Compare alternatives
Mounting as volume is valid but not environment variables. Multiple Secrets or encoding keys complicate management.
Final Answer:
List each key separately under env with individual valueFrom.secretKeyRef entries -> Option B
Quick Check:
Map each key to env var individually = A [OK]
Hint: Map each Secret key individually in env list [OK]
Common Mistakes:
Mounting Secret as volume when env vars needed
Creating many Secrets instead of one
Combining keys into one encoded string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.