📖 Scenario: You are managing a Kubernetes application that needs to securely access sensitive data like passwords. Instead of hardcoding secrets in your app, you will use Kubernetes Secrets and mount them as volumes inside your pod.
🎯 Goal: Create a Kubernetes Secret with exact key-value pairs, configure a pod to mount this Secret as a volume, and verify the secret data is accessible inside the pod.
📋 What You'll Learn
Create a Secret named db-secret with keys username and password and exact values admin and p@ssw0rd
Create a pod named secret-demo-pod that mounts the db-secret as a volume at /etc/secret-volume
Verify the secret files username and password exist inside the pod and contain the correct values
💡 Why This Matters
🌍 Real World
Kubernetes Secrets are used to store sensitive information like passwords, tokens, and keys securely. Mounting them as volumes allows applications to read secrets as files without exposing them in environment variables or code.
💼 Career
Understanding how to manage secrets securely in Kubernetes is essential for DevOps engineers and cloud-native developers to protect sensitive data and comply with security best practices.
Progress0 / 4 steps
1
Create the Kubernetes Secret
Create a Kubernetes Secret named db-secret with the exact key-value pairs: username: admin and password: p@ssw0rd using a YAML manifest.
Kubernetes
Hint
Use base64 encoding for the values: 'admin' -> 'YWRtaW4=', 'p@ssw0rd' -> 'cEBzc3cwcmQ='
2
Add volume configuration to the pod
Create a pod named secret-demo-pod that mounts the Secret db-secret as a volume named secret-volume at the path /etc/secret-volume inside the container. Use the image busybox and command sleep 3600.
Kubernetes
Hint
Mount the secret as a volume inside the container under /etc/secret-volume.
3
Verify secret files inside the pod
Use kubectl exec to run cat /etc/secret-volume/username and cat /etc/secret-volume/password inside the pod secret-demo-pod to verify the secret values are accessible.
Kubernetes
Hint
Use kubectl exec with the pod name and cat command to read secret files.
4
Display the secret values
Print the output of the commands kubectl exec secret-demo-pod -- cat /etc/secret-volume/username and kubectl exec secret-demo-pod -- cat /etc/secret-volume/password to show the secret values admin and p@ssw0rd respectively.
Kubernetes
Hint
Print the exact secret values as output.
Practice
(1/5)
1. What is the main benefit of mounting Kubernetes Secrets as volumes inside a pod instead of using environment variables?
easy
A. Environment variables are more secure than mounted volumes.
B. Mounted volumes require manual pod restart to update secrets.
C. Secrets mounted as volumes can be updated automatically without restarting the pod.
D. Secrets cannot be mounted as volumes in Kubernetes.
Solution
Step 1: Understand how secrets are exposed
Secrets can be exposed as environment variables or mounted as files in volumes inside pods.
Step 2: Compare update behavior
Mounted secrets update automatically when changed in Kubernetes, environment variables do not update without pod restart.
Final Answer:
Secrets mounted as volumes can be updated automatically without restarting the pod. -> Option C
Quick Check:
Mounted secrets auto-update = B [OK]
Hint: Mounted secrets auto-update; env vars need pod restart [OK]
If the secret mysecret contains a key password with value abc123, what will be the content of the file /etc/secret/password inside the container?
medium
A. password
B. abc123
C. mysecret
D. The file will not exist
Solution
Step 1: Understand secret volume mounting
Each key in the secret becomes a file inside the mounted path with the key as filename and value as file content.
Step 2: Identify file content for key 'password'
The file /etc/secret/password will contain the secret value abc123.
Final Answer:
abc123 -> Option B
Quick Check:
Secret key value = file content [OK]
Hint: Secret keys become filenames; values become file contents [OK]
Common Mistakes:
Confusing filename with secret name
Expecting filename to contain key name instead of value
Thinking file won't be created
4. You mounted a secret as a volume in your pod, but inside the container the secret files are empty. What is the most likely cause?
medium
A. The secret was created but has no data keys.
B. The pod spec used configMap instead of secret in volumes.
C. The volumeMount path is incorrect.
D. Secrets cannot be mounted as volumes.
Solution
Step 1: Check secret data content
If the secret exists but has no keys or empty values, mounted files will be empty.
Step 2: Verify volume type and mount path
Using secret in volumes and correct mountPath is required; wrong type or path causes missing files, not empty files.
Final Answer:
The secret was created but has no data keys. -> Option A
Quick Check:
Empty secret data = empty files [OK]
Hint: Empty secret data means empty files inside container [OK]
Common Mistakes:
Confusing empty files with wrong mount path
Assuming secrets can't be mounted
Mixing configMap with secret volumes
5. You want to mount a secret as a volume in a pod, but only expose the key db-password as a file named password.txt inside the container at /etc/db. Which volume configuration achieves this?