Skip to main content
In this lesson, we’ll refactor a simple Python web application that connects to a MySQL database. Currently, the database hostname, username, and password are hardcoded in the source—an insecure practice. We’ll move sensitive values into Kubernetes Secrets to keep credentials safe.
Never store plaintext passwords in your code or in a ConfigMap. Use Kubernetes Secrets for all sensitive data.

1. Creating Secrets

Secrets can be created imperatively with kubectl or declaratively with a YAML manifest.

1.1 Imperative Creation

Specify key-value pairs on the command line:
Or load from a file (key=value per line):

1.2 Declarative Creation

Define a Secret manifest in secret-data.yaml:
Apply the manifest:
Generate base64-encoded values:
You can decode any value with echo '<base64>' | base64 --decode. Keep your raw files out of version control.

2. Viewing Secrets

List all existing secrets:
Inspect a specific secret:
Output the raw YAML (with encoded data):
Decode an encoded secret value:

3. Injecting Secrets into Pods

You can consume Secrets as environment variables or as mounted volumes.

3.1 All Keys as Environment Variables

Add an envFrom directive to your Pod spec:
Apply the Pod definition:

3.2 Single Key as an Environment Variable

Use valueFrom.secretKeyRef for a specific key:

3.3 Mounting Secrets as Files

Mount the secret into a volume:
Inside the running Pod:

Try the exercises to practice creating, viewing, and injecting Secrets in your Kubernetes clusters!

Watch Video

Practice Lab