> ## Content Index
> Fetch the complete content index at: https://skills.devopscube.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Configure a Kubernetes Secret in a Pod
- URL: https://skills.devopscube.com/configure-a-secret-in-a-pod/
- Published: 2026-09-17T09:35:41.000Z
- Updated: 2026-10-14T14:24:46.000Z
- Author: Sulaiman
- Tags: #blog, #CKA-Exam-question, #docs, Cluster Architecture, Installation & Configuration

\`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-credentials` Secret exists.
- The `secret-app` Pod 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.

🙅‍♀️

Do not hard-code the database credentials directly in the Pod manifest.

📚

For more details about [Secret](https://kubernetes.io/docs/concepts/configuration/secret/?ref=skills.devopscube.com),[pods](https://kubernetes.io/docs/concepts/workloads/pods/?ref=skills.devopscube.com) refer to the official Kubernetes documentation.

---

### Create the Secret

Create a generic Secret containing the database username and password:

```sh
$ 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:

```sh
$ kubectl get secret

NAME             TYPE     DATA   AGE
db-credentials   Opaque   2      2m51s
```

The `DATA` column shows that the Secret contains 2 data entries.

⚠️

Important: Kubernetes stores Secret data as Base64-encoded values in the API representation. Base64 is encoding, not encryption by itself.

### Create the Pod Manifest

Generate a basic Pod manifest without creating the Pod:

```sh
$ kubectl run secret-app \
  --image=nginx \
  --dry-run=client -o yaml > pod.yaml
```

This creates `pod.yaml`, which you can edit to configure the Secret as environment variables.

pod.yaml Copy 

apiVersion: v1 kind: Pod metadata:  name: secret-app spec:  containers:  \- name: secret-app  image: nginx  env:  \- name: DB\_USERNAME  valueFrom:  secretKeyRef:  name: db-credentials  key: username  \- name: DB\_PASSWORD  valueFrom:  secretKeyRef:  name: db-credentials  key: password 

## 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 the `username` key from the `db-credentials` Secret and make it available through `DB_USERNAME`.

### Apply the Pod

Apply the manifest to create the Pod:

```sh
$ kubectl apply -f pod.yaml

pod/secret-app created
```

Verify that the Pod is running:

```sh
$ kubectl get pod secret-app

NAME         READY   STATUS    RESTARTS   AGE
secret-app   1/1     Running   0          27s
```

The Pod should reach the **`Running`** state.

### Verify the Secret Environment Variables

Check the environment variables inside the container:

```sh
$ 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.

💡

****Security Note:** Avoid printing real secrets in logs or terminal output in production. Here, displaying the values is only for verifying this CKA exercise.

## 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
```