Skip to main content
Welcome to this lesson on Kubernetes Secrets. In this guide, we’ll refactor a simple Python web app that connects to MySQL and demonstrate how to replace hardcoded credentials with Kubernetes Secrets.
Hardcoding sensitive data like database passwords in your application is insecure. Always store secrets in a dedicated secret store rather than plain text files or code.

1. Application Overview

Here’s the original Flask application with hardcoded credentials:
By externalizing configuration into ConfigMaps and Secrets, we improve security and flexibility.

2. Storing Non-sensitive Configuration

Use a ConfigMap for non-sensitive data such as hostnames and users:
Avoid placing DB_Password here—this belongs in a Secret.

3. Creating Kubernetes Secrets

There are two ways to create Secrets in Kubernetes:

3.1 Imperative Method

Generate a generic Secret directly:
Or load from files:

3.2 Declarative Method

  1. Base64-encode each value:
  2. Create app-secret.yaml:
  3. Apply the manifest:
The type: Opaque is the default Secret type for arbitrary user-defined data.
Learn more in the Kubernetes Secrets documentation.

4. Viewing Secrets

  • List all Secrets:
  • Describe a Secret (values are masked):
  • View encoded data in YAML:
  • Decode a specific value:

5. Injecting Secrets into Pods

5.1 As Environment Variables

Inject all keys at once:
Or inject specific keys:

5.2 As Files in a Volume

Mount the Secret as a read-only volume; each key becomes a file:
Inside the container:

Watch Video