Question 17
Domain 1: Application Design and BuildWhich kubectl command should be used to create an nginx pod with an environment variable var1=val1 and verify that the variable exists inside the pod?
Correct answer: A
Explanation
`kubectl run nginx --image=nginx --env="var1=val1"` creates an nginx pod and sets the container environment variable using the `--env` flag. `kubectl exec -it nginx -- printenv var1` then runs `printenv` inside the pod to verify that `var1` exists and has the value `val1`.
Why each option is right or wrong
A. kubectl run nginx --image=nginx --env="var1=val1" && kubectl exec -it nginx -- printenv var1
`kubectl run` supports setting container environment variables at creation time with the `--env` flag, so `--env="var1=val1"` injects that key/value into the nginx container when the pod is started. To verify it inside the running pod, `kubectl exec -it nginx -- printenv var1` executes `printenv` in the container and returns the value of that specific variable; the `-it` flags allocate an interactive TTY, and `--` separates kubectl options from the command run in the pod.
B. kubectl create pod nginx --image=nginx --env="var1=val1" && kubectl describe pod nginx | grep var1
C. kubectl run nginx --image=nginx --set-env="var1=val1" && kubectl exec nginx -- env | grep var1
D. kubectl apply -f nginx.yaml with env var1=val1 in metadata.labels && kubectl logs nginx | grep var1