Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Configuration Management  ›  Capstone Project

Using Secrets for Sensitive Data

Using Secrets for Sensitive Data 12 minSecrets hold sensitive values that pods use like ConfigMaps, but their base64 encoding is not encryption, so real protection comes from RBAC, encryption at rest and keeping them out of Git.

Using Secrets for Sensitive Data

notes-web needs the database password. Put it in a ConfigMap and anyone who can read ConfigMaps can read it; put it in the image and anyone who can pull the image can. Secrets are the Kubernetes object for values that must be guarded.

The rule

A Secret is encoded, not encrypted — protect it with access, not with base64.

A Secret looks like a ConfigMap and is used the same ways — as env variables or as mounted files. The differences are in handling:

  • Values under data are base64-encoded. That is an encoding, not encryption: anyone can decode it in one command.
  • Kubernetes treats Secrets more carefully: they can be encrypted at rest in etcd (when the cluster is configured for it), are kept in memory rather than on disk on nodes, and can be locked down separately with RBAC.

Writing a Secret is easiest with stringData, which takes plain text and encodes it for you:

apiVersion: v1
kind: Secret
metadata:
  name: notes-db-credentials
  namespace: notes
type: Opaque
stringData:
  POSTGRES_USER: notes
  POSTGRES_PASSWORD: change-me-please

Using one key in a pod:

env:
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: notes-db-credentials
        key: POSTGRES_PASSWORD

Or from the command line, which avoids writing the value to any file:

kubectl create secret generic notes-db-credentials --from-literal=POSTGRES_PASSWORD='change-me-please' -n notes

Other built-in types include kubernetes.io/tls for certificates and kubernetes.io/dockerconfigjson for private registry logins.

The shortcut

For every value ask: "Would I be upset if this appeared in a screenshot?" Yes → Secret. No → ConfigMap. Then check the three guards: who can read it (RBAC), is it encrypted at rest, and is it kept out of Git?

Sorting notes-web's settingsStep 1 of 4
  1. LOG_LEVEL and DB_HOST

    Harmless in a screenshot: ConfigMap.

The encoding trap: echo "pass" | base64 includes a newline, so the stored password becomes pass plus a newline. Use echo -n, or better, stringData.

Drills

Answer fast — trust the rule.

quick drill

The data field of a Secret holds Y2hhbmdlLW1l. Is the password protected by this?

quick drill

Which field lets you write Secret values as plain text in the manifest?

quick drill

Which value belongs in a Secret rather than a ConfigMap?

quick drill

Which practice actually keeps notes-db-credentials safe?

quick drill

A Secret was built with echo "hunter2" | base64 and the app's login now fails. Why?

Remember

  • A Secret is encoded, not encrypted — protect it with RBAC, encryption at rest, and keeping it out of Git.
  • Upsetting in a screenshot → Secret; harmless → ConfigMap.
  • Trap: echo without -n sneaks a newline into the value.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSecure the password

A teammate says: 'The database password is safe because it is base64 in a Secret YAML file in our Git repo.' Explain what is wrong with this, and describe a safer setup.