Introduction
Kubernetes Secrets are simple conceptually, but the data field requires properly base64-encoded values. Many failures come from tiny mistakes: using echo without suppressing the newline, double-encoding an already encoded value, or mixing data and stringData assumptions. The result is either an apply-time base64 error or a Secret that loads successfully but contains the wrong decoded content.
Symptoms
kubectl applyfails withillegal base64 data- The Secret exists, but the application reads malformed values
- Operators see the encoded text itself where the decoded secret should appear
- The same Secret works in one shell or OS and breaks in another
Common Causes
- A value in
datais not valid base64 echoadded a trailing newline before encoding- The value was encoded twice
dataandstringDatausage was mixed without understanding how Kubernetes handles them
Step-by-Step Fix
- 1.**Use
stringDatawhen you want Kubernetes to do the encoding** - 2.This is the safest path for many hand-authored manifests.
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: my-secret
stringData:
password: my-password- 1.**If using
data, encode without a newline** - 2.Trailing newlines are a common source of confusing mismatches.
echo -n "my-password" | base64- 1.Decode and verify the stored value
- 2.Do not assume the encoded string is correct just because the Secret applied successfully.
kubectl get secret my-secret -o jsonpath='{.data.password}' | base64 -d- 1.**Avoid mixing
dataandstringDatafor the same key casually** - 2.Keep the representation simple and explicit so future edits do not create hidden overrides.
Prevention
- Prefer
stringDatafor human-authored manifests - Use
echo -nor an equivalent newline-safe encoder when generating base64 manually - Decode and inspect critical secret values before rollout
- Keep secret generation automated and consistent across platforms