Bird
Raised Fist0
Kubernetesdevops~5 mins

Using Secrets as mounted volumes in Kubernetes - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes applications need sensitive information like passwords or API keys. Storing these secrets safely and making them available to apps without exposing them in code is important. Kubernetes lets you store secrets and mount them as files inside your app's containers.
When your app needs a database password without hardcoding it in the app code.
When you want to provide TLS certificates to your app securely.
When you need to share API keys with your app without exposing them in environment variables.
When you want to update secrets without rebuilding your app image.
When you want to keep sensitive data separate from your app configuration files.
Config File - secret-volume-pod.yaml
secret-volume-pod.yaml
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: bXl1c2Vy
  password: bXlwYXNzd29yZA==
---
apiVersion: v1
kind: Pod
metadata:
  name: secret-volume-pod
spec:
  containers:
  - name: my-container
    image: busybox
    command: ["sleep", "3600"]
    volumeMounts:
    - name: secret-volume
      mountPath: "/etc/secret-data"
      readOnly: true
  volumes:
  - name: secret-volume
    secret:
      secretName: my-secret

This file has two parts:

  • Secret: Stores sensitive data encoded in base64. Here, 'username' and 'password' are stored.
  • Pod: Defines a pod that mounts the secret as a volume at /etc/secret-data. The container runs a simple sleep command to keep running.

The secret data will appear as files inside the container at the mount path.

Commands
This command creates the secret and the pod that mounts the secret as a volume. It sets up the environment for the app to access secret data as files.
Terminal
kubectl apply -f secret-volume-pod.yaml
Expected OutputExpected
secret/my-secret created pod/secret-volume-pod created
Check that the pod is running and ready to use the secret volume.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE secret-volume-pod 1/1 Running 0 10s
List the files inside the mounted secret volume to verify the secret keys are available as files.
Terminal
kubectl exec secret-volume-pod -- ls /etc/secret-data
Expected OutputExpected
password username
Read the content of the username file inside the container to see the secret value.
Terminal
kubectl exec secret-volume-pod -- cat /etc/secret-data/username
Expected OutputExpected
myuser
Key Concept

If you remember nothing else from this pattern, remember: Kubernetes secrets can be securely mounted as files inside containers, letting your app read sensitive data without exposing it in code or environment variables.

Common Mistakes
Not encoding secret data in base64 before creating the secret manifest.
Kubernetes requires secret data to be base64 encoded; otherwise, the secret creation will fail or data will be corrupted.
Encode your secret values using base64 before adding them to the secret manifest.
Mounting the secret volume without setting readOnly to true.
Without readOnly, the container might accidentally modify or delete secret files, risking security and stability.
Always set readOnly: true when mounting secrets as volumes to prevent accidental changes.
Trying to access secret files before the pod is fully running.
If the pod is not ready, the secret volume might not be mounted yet, causing file not found errors.
Wait until the pod status is Running and Ready before accessing secret files.
Summary
Create a Kubernetes secret with base64 encoded data.
Define a pod that mounts the secret as a volume at a specific path.
Verify the pod is running and list the secret files inside the container.
Read secret values from the mounted files securely inside the container.

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

  1. Step 1: Understand how secrets are exposed

    Secrets can be exposed as environment variables or mounted as files in volumes inside pods.
  2. Step 2: Compare update behavior

    Mounted secrets update automatically when changed in Kubernetes, environment variables do not update without pod restart.
  3. Final Answer:

    Secrets mounted as volumes can be updated automatically without restarting the pod. -> Option C
  4. Quick Check:

    Mounted secrets auto-update = B [OK]
Hint: Mounted secrets auto-update; env vars need pod restart [OK]
Common Mistakes:
  • Thinking environment variables update automatically
  • Believing secrets cannot be mounted as volumes
  • Assuming mounted volumes require pod restart
2. Which of the following is the correct YAML snippet to mount a secret named mysecret as a volume at path /etc/secret-volume inside a pod?
easy
A. volumes:\n - name: secret-volume\n secret:\n secretName: mysecret\ncontainers:\n - name: app\n volumeMounts:\n - name: secret-volume\n path: /etc/secret-volume
B. volumes:\n - name: secret-volume\n configMap:\n name: mysecret\ncontainers:\n - name: app\n volumeMounts:\n - name: secret-volume\n mountPath: /etc/secret-volume
C. volumes:\n - name: secret-volume\n secret:\n name: mysecret\ncontainers:\n - name: app\n volumeMounts:\n - name: secret-volume\n mountPath: /etc/secret-volume
D. volumes:\n - name: secret-volume\n secret:\n secretName: mysecret\ncontainers:\n - name: app\n volumeMounts:\n - name: secret-volume\n mountPath: /etc/secret-volume

Solution

  1. Step 1: Check volume secret syntax

    The correct key to specify the secret is secretName, not name.
  2. Step 2: Verify volumeMount path key

    The correct key to specify mount path is mountPath, not path.
  3. Final Answer:

    Uses correct keys: secretName and mountPath. -> Option D
  4. Quick Check:

    secretName + mountPath = A [OK]
Hint: Use secretName for secret, mountPath for volume mount [OK]
Common Mistakes:
  • Using configMap instead of secret
  • Using 'name' instead of 'secretName'
  • Using 'path' instead of 'mountPath'
3. Given this pod spec snippet mounting a secret as a volume:
volumes:
  - name: secret-vol
    secret:
      secretName: mysecret
containers:
  - name: app
    volumeMounts:
      - name: secret-vol
        mountPath: /etc/secret
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

  1. 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.
  2. Step 2: Identify file content for key 'password'

    The file /etc/secret/password will contain the secret value abc123.
  3. Final Answer:

    abc123 -> Option B
  4. 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

  1. Step 1: Check secret data content

    If the secret exists but has no keys or empty values, mounted files will be empty.
  2. 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.
  3. Final Answer:

    The secret was created but has no data keys. -> Option A
  4. 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?
hard
A. volumes: - name: db-secret secret: secretName: mysecret items: - key: db-password path: password.txt containers: - name: app volumeMounts: - name: db-secret mountPath: /etc/db
B. volumes: - name: db-secret secret: secretName: mysecret items: - key: password.txt path: db-password containers: - name: app volumeMounts: - name: db-secret mountPath: /etc/db
C. volumes: - name: db-secret secret: secretName: mysecret containers: - name: app volumeMounts: - name: db-secret mountPath: /etc/db/password.txt
D. volumes: - name: db-secret configMap: name: mysecret items: - key: db-password path: password.txt containers: - name: app volumeMounts: - name: db-secret mountPath: /etc/db

Solution

  1. Step 1: Use secret volume with items to map keys to filenames

    The items field allows selecting specific keys and renaming files.
  2. Step 2: Verify correct key and path mapping

    Key db-password mapped to password.txt with mountPath /etc/db matches volumes: - name: db-secret secret: secretName: mysecret items: - key: db-password path: password.txt containers: - name: app volumeMounts: - name: db-secret mountPath: /etc/db.
  3. Final Answer:

    Correctly maps the secret key to the desired filename and mount path. -> Option A
  4. Quick Check:

    Use items to rename secret keys in volume [OK]
Hint: Use items to rename secret keys as files [OK]
Common Mistakes:
  • Swapping key and path in items
  • Using configMap instead of secret
  • Mounting secret directly without items for renaming
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard