Course contentsShow
← Cloud and Infrastructure
Lesson 1263 of 2,98925. Kubernetes Configuration and SecretsFree lesson

Declarative Secret Manifests

Write Secret manifests in YAML with base64-encoded values for version-controlled definitions.

Declarative Secret Manifests

You'll learn how to define Kubernetes Secrets as YAML manifests with base64-encoded values for consistent, version-controlled deployments.

Why Use Declarative Secret Manifests?

While creating Secrets imperatively with kubectl is quick, declarative YAML manifests let you treat Secrets like any other Kubernetes resource. You can store them in Git (with proper access controls), review changes, and apply them consistently across environments. Think of it like writing down a recipe instead of explaining it verbally each time—it's repeatable and auditable.

The Base64 Encoding Requirement

Kubernetes requires Secret data values to be base64-encoded in YAML manifests. This isn't encryption—it's just encoding to handle binary data safely. You encode your values before putting them in the manifest:

apiVersion: v1
kind: Secret
metadata:
  name: db-credentials
type: Opaque
data:
  username: YWRtaW4=        # "admin" base64-encoded
  password: cGFzc3dvcmQxMjM= # "password123" base64-encoded

To encode a value, use the command line:

echo -n 'admin' | base64

The -n flag prevents adding a newline character, which would corrupt your secret value.

Structure Breakdown

  • type: Opaque: The default Secret type for arbitrary key-value pairs
  • data: Contains base64-encoded values
  • Keys: Can be any valid identifier (like username, password)

Applying the Manifest

Once defined, apply it like any resource:

kubectl apply -f secret.yaml

Plain Text Alternative

You can use stringData for plain text values (Kubernetes encodes them automatically), but data with base64 is the standard for manifest files.

Key Takeaway: Declarative Secret manifests use base64-encoded values under the data field, enabling version-controlled, reproducible Secret definitions across your infrastructure.