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.
Question
- 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
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:
-
Create the Secret:
kubectl create secret generic another-secret --from-literal=key1=value4This command creates a Kubernetes Secret named 'another-secret'. The--from-literalflag 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. -
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 thebase64 --decodecommand, 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 --decodewill result in the pod receiving the base64-encoded string ('dmFsdWU0') instead of the actual plaintext value ('value4'). Secrets'datafield always contains base64-encoded values. - Using
--env-fromincorrectly: Attempting to usekubectl run ... --env-from=secretRef:another-secretwould 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
jsonpathquery, 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.secretKeyRefdirectly withkubectl run: Thekubectl runcommand-line utility does not support thevalueFrom.secretKeyRefsyntax, 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
Community Discussion
No community discussion yet for this question.
