How to Configure a Kubernetes Secret in a Pod
`The DevOpscube team is deploying an application that needs database credentials at runtime. The credentials must be stored in Kubernetes as a Secret rather than being defined directly in the Pod configuration.
Your task is to create a Kubernetes Secret named db-credentials containing the database username and password required by the application.
Create a Pod named secret-app using the nginx image and configure it to consume the credentials stored in db-credentials as environment variables.
Verify that:
- The
db-credentialsSecret exists. - The
secret-appPod is in the Running state. - The Pod is configured to consume the database credentials from the Secret.
- The expected environment variables are available inside the running container.
Create the Secret
Create a generic Secret containing the database username and password:
$ kubectl create secret generic db-credentials \
--from-literal=<username=app-user> \
--from-literal=<password=app-password>This creates a generic Secret containing the database username and password.
Verify the Secret
Check that the Secret was created successfully:
$ kubectl get secret
NAME TYPE DATA AGE
db-credentials Opaque 2 2m51sThe DATA column shows that the Secret contains 2 data entries.
Create the Pod Manifest
Generate a basic Pod manifest without creating the Pod:
$ kubectl run secret-app \
--image=nginx \
--dry-run=client -o yaml > pod.yamlThis creates pod.yaml, which you can edit to configure the Secret as environment variables.
Why Use secretKeyRef?
secretKeyRef allows a specific key from a Kubernetes Secret to be exposed as an environment variable inside a container.
For example, referencing name: db-credentials with key: username retrieves the username value from that Secret. The same pattern can be used to expose the password.
Get theusernamekey from thedb-credentialsSecret and make it available throughDB_USERNAME.
Apply the Pod
Apply the manifest to create the Pod:
$ kubectl apply -f pod.yaml
pod/secret-app createdVerify that the Pod is running:
$ kubectl get pod secret-app
NAME READY STATUS RESTARTS AGE
secret-app 1/1 Running 0 27sThe Pod should reach the Running state.
Verify the Secret Environment Variables
Check the environment variables inside the container:
$ kubectl exec secret-app -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TERM=xterm
HOSTNAME=secret-app
DB_USERNAME=app-user
DB_PASSWORD=app-password
You should see this, confirming the Secret values were successfully injected into the Pod as environment variables.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
1. Remember the Secret Creation Command
You can quickly create a generic Secret using kubectl create secret with --from-literal:
kubectl create secret generic <name> \
--from-literal=key=value
2. Don't Confuse Secret and ConfigMap
A ConfigMap is used for non-sensitive configuration, while a Secret is intended for sensitive data such as passwords, tokens, and credentials.
3. Verify Inside the Container
When a Secret is injected as an environment variable, you can verify it from inside the container using:
kubectl exec secret-app -- env
Or check a specific variable:
kubectl exec secret-app -- printenv DB_USERNAME