nerdexam
Linux_Foundation

CKAD · Question #2

You are tasked to create a secret and consume the secret in a pod using environment variables as follow: Create a secret named another-secret with a key/value pair; key1/value4 Start an nginx pod…

The task requires creating a Kubernetes secret and then deploying an Nginx pod that consumes a specific secret key's value as an environment variable using direct kubectl commands.

Submitted by andreas_gr· May 4, 2026Application Environment, Configuration and Security

Question

You are tasked to create a secret and consume the secret in a pod using environment variables as follow:
  • Create a secret named another-secret with a key/value pair; key1/value4
  • Start an nginx pod named nginx-secret using container image nginx, and add an environment variable exposing the value of the secret key key 1, using COOL_VARIABLE as the name for the environment variable inside the pod

Exhibit

CKAD question #2 exhibit

Explanation

The task requires creating a Kubernetes secret and then deploying an Nginx pod that consumes a specific secret key's value as an environment variable using direct kubectl commands.

Approach. To correctly address the requirements, two commands need to be executed in the provided web terminal:

  1. Create the Secret: kubectl create secret generic another-secret --from-literal=key1=value4 This command creates a Kubernetes Secret named 'another-secret'. The --from-literal flag is used to specify the key-value pair 'key1' and 'value4' to be stored within the secret. This fulfills the first requirement of creating the secret.

  2. Create the Pod and Inject Environment Variable: kubectl run nginx-secret --image=nginx --env="COOL_VARIABLE=$(kubectl get secret another-secret -o jsonpath='{.data.key1}' | base64 --decode)" This command performs several actions to meet the second requirement:

    • kubectl run nginx-secret --image=nginx: This part creates a Pod named 'nginx-secret' using the 'nginx' container image.
    • --env="COOL_VARIABLE=...": This flag is used to define an environment variable named 'COOL_VARIABLE' inside the 'nginx' container.
    • $(kubectl get secret another-secret -o jsonpath='{.data.key1}' | base64 --decode): This is a shell command substitution that dynamically retrieves the secret's value:
      • kubectl get secret another-secret -o jsonpath='{.data.key1}': This retrieves the base64-encoded value of 'key1' from the 'another-secret' Secret. Kubernetes stores data field values in Secrets as base64.
      • | base64 --decode: This pipes the retrieved base64-encoded value to the base64 --decode command, which decodes it back to its original plaintext ('value4').
      • The resulting plaintext 'value4' is then assigned to the 'COOL_VARIABLE' environment variable inside the 'nginx-secret' pod, thus fulfilling the requirement to expose the secret key's value as 'COOL_VARIABLE'.

Common mistakes.

  • common_mistake. Common mistakes include:
  • Not decoding the secret value: Retrieving the secret value using kubectl get secret ... -o jsonpath='{.data.key1}' without piping it to | base64 --decode will result in the pod receiving the base64-encoded string ('dmFsdWU0') instead of the actual plaintext value ('value4'). Secrets' data field always contains base64-encoded values.
  • Using --env-from incorrectly: Attempting to use kubectl run ... --env-from=secretRef:another-secret would inject all key-value pairs from 'another-secret' into the pod's environment, with the environment variable names matching the secret keys (e.g., 'key1=value4'). This fails to meet the specific requirement of mapping 'key1' to a different environment variable named 'COOL_VARIABLE'.
  • Incorrect command-line syntax: Errors in shell substitution (e.g., missing quotes, incorrect jsonpath query, misplaced parentheses) can cause the command to fail or incorrectly inject the value.
  • Incorrect resource names or image: Using secret, pod, or container image names other than 'another-secret', 'nginx-secret', or 'nginx' (respectively) would not satisfy the exact requirements specified in the question.
  • Attempting to use valueFrom.secretKeyRef directly with kubectl run: The kubectl run command-line utility does not support the valueFrom.secretKeyRef syntax, which is exclusively used within Kubernetes YAML manifest definitions for Pods.

Concept tested. Kubernetes Secrets management, including creating secrets from literal values and securely injecting specific secret key values into Pods as environment variables using kubectl commands.

Topics

#Kubernetes Secrets#Pod Configuration#Environment Variables

Community Discussion

No community discussion yet for this question.

Full CKAD Practice