πŸ”Kubernetes Secrets Basics Labs

🎯 Objective

Learn how to manage secrets securely in Kubernetes using best practices, sealed secrets, and volume-based injection.


🧰 Prerequisites

  • Kubernetes cluster

  • kubectl configured

  • Optional tools: kubeseal, helm, vault


πŸ”Ή Lab 1: Create a Secret and Mount It as a Volume

Step 1: Create the secret

kubectl create secret generic db-creds \
  --from-literal=username=admin \
  --from-literal=password='S3cureP@ss'

Step 2: Mount secret as volume

Step 3: Run the pod

Step 4: Check the secrets

βœ… Secrets are mounted as read-only files.


πŸ”Ή Lab 2: Avoid Using Environment Variables (But Here's How)

Step 1: Mount secret as environment variables

Step 2: Run the pod

Step 3: Expose secrets

This command does not expose secrets any more:

But the log does reveal the secrets:

❗️ Secrets exposed in env vars can be viewed via kubectl logs.


πŸ”Ή Lab 3: Use Sealed Secrets (Bitnami)

Problem: "I can manage all my K8s config in git, except Secrets."

Solution: Encrypt your Secret into a SealedSecret, which is safe to store - even inside a public repository. The SealedSecret can be decrypted only by the controller running in the target cluster and nobody else (not even the original author) is able to obtain the original Secret from the SealedSecret.

Requires kubesealarrow-up-right and corresponding Kubernetes controller setup

Step 1: Install the controller

Step 2: Install kubeseal client

Install via brew:

Step 3: Use kubeseal

Create secrets:

If you do a cat mysecret.json:

You may decode the secret using echo c2VjcmV0 | base64 -d.

No let us seal the secrets:

βœ… Safe for GitOps workflows.

Read secrets again:


πŸ”Ή Lab 4: Use External Secret Managers

  • HashiCorp Vault

  • AWS Secrets Manager

  • Azure Key Vault

Use tools like:

  • External Secrets Operator

  • CSI Driver Secrets Store

🧠 Fetch secrets at runtime, not stored in etcd.


βœ… Wrap-Up

  • βœ… Use volume mounts over env vars

  • βœ… Don’t log or echo secrets

  • βœ… Rotate secrets regularly

  • βœ… Use SealedSecrets for Git

  • βœ… Use external secret managers for runtime safety


Last updated