Skip to main content
This article walks through the Kubernetes Secrets lab step-by-step, explains the commands used, and shows how to inject a Secret into a Pod so an application can access database credentials securely. Follow the sequence below to reproduce the lab and understand the root cause and remediation.

Questions 1–4: Inspecting the default namespace Secrets

Question 1 — How many Secrets exist in the default namespace?
Run:
Example output:
There is only one Secret in the default namespace: the service-account token. Question 2 — What data keys are defined in the default token Secret?
Describe the Secret:
Example (truncated):
Data entries present:
  • ca.crt
  • namespace
  • token
Question 3 — What is the type of the default token Secret?
From the output above, the Secret type is:
Question 4 — Which of the following is NOT a Secret data key defined in the default token?
Any key other than ca.crt, namespace, or token (for example DB_Host, DB_User, or DB_Password) is not present in the default token.

Deploying and inspecting the sample application

This lab uses a simple application composed of:
  • a webapp Pod (the client-facing app)
  • a mysql Pod (the database)
First, inspect the cluster to list deployments, pods, services, and secrets:
Example output:

Observed failure in the web application

The webapp initially fails because it cannot connect to MySQL. The app reports missing DB environment variables:
Root cause: the Secret containing database credentials has not been created or injected into the webapp Pod.

Create a Secret with database credentials

We create a generic Secret named db-secret containing the required keys. Secret keys are case-sensitive and will be injected into the Pod environment exactly as named. Create the Secret:
Verify creation:
Example output:
Tip: you can also create Secrets from files with --from-file or provide base64-encoded values in a manifest.

Consume the Secret in a Pod

A common pattern is to map all keys in a Secret into container environment variables using envFrom with secretRef. The Kubernetes docs provide an example Secret manifest and a Pod manifest that consumes it: Example Secret (manifest form):
Example Pod that reads all keys as environment variables:
For this lab, use envFrom with db-secret so DB_Host, DB_User, and DB_Password become environment variables inside the webapp container. Add the following snippet into the containers: block of the webapp-pod manifest:
If you edit the live Pod using kubectl edit pod webapp-pod, kubectl will open a temporary file. After saving edits, the Pod must be replaced for the new environment variables to take effect (editing a running Pod does not update its containers in-place). Replace the Pod (example):
Example output when replacing the Pod:
Verify the Pod now references the Secret:
Relevant excerpt:
Restarting the Pod with the Secret injected should allow the application to connect to the MySQL database. After the Pod is running, check the application UI or logs to confirm the successful connection. Note: this sample app uses environment variables only for demonstration—do not log secrets or display them in production.
Avoid printing or logging secret values. Secrets injected into environment variables or mounted as files can be exposed through logs, process listings, or debugging output. Treat them like sensitive credentials.

Quick reference: common kubectl commands used in this lab


Security considerations

A screenshot of the Kubernetes documentation page titled "Secrets," explaining what a Secret is and showing a caution box about secrets being stored unencrypted by default and how to secure them. The page includes side navigation menus with related topics.
Secrets are stored in etcd. By default they may be unencrypted at rest unless you enable encryption at rest for the API server. For production clusters, enable encryption, use RBAC to restrict access, and consider external secret stores (for example: HashiCorp Vault, AWS Secrets Manager, or cloud provider secrets solutions).
Additional reading: how Pods use Secrets and alternatives (volumes, imagePullSecrets, external stores):
A screenshot of a Kubernetes documentation page titled "Uses for Secrets" and "Alternatives to Secrets," showing explanatory text and bullet points about how Pods use Secrets. The page includes a left navigation menu and a right-hand table of contents.
This page explains multiple consumption methods (environment variables, projected volumes, imagePullSecrets) and discusses alternatives for secret management.

Summary

  • The default namespace initially contained a single Secret: the service-account token (with ca.crt, namespace, and token keys).
  • The webapp failed because DB-related environment variables were not set.
  • Created an Opaque Secret db-secret with keys DB_Host, DB_User, and DB_Password.
  • Injected the Secret into webapp via envFrom → secretRef, recreated the Pod, and confirmed the app could connect to MySQL.
  • In production, ensure Secrets are encrypted at rest, access is limited via RBAC, and consider external secret stores for improved security.

Watch Video