Full lesson
Explore the full explanation, examples, and visuals at your own pace.
What the developer ran
Your password is right. Kubernetes says it's wrong. Here, the developer runs echo into base64, which just turns bytes into plain text. They paste czNjcmV0Cg== into the Secret under password. Then the app tries to log in, and the database says authentication failed. Nothing looks wrong.
$ echo "s3cret" | base64
czNjcmV0Cg==
# secret.yaml
data:
password: czNjcmV0Cg==
# app log
password authentication failed for user "app"The cause: echo adds a newline
The cause is echo. By default, it ends its output with a newline, the same as pressing Enter. So s3cret, six letters, becomes seven bytes. Base64 doesn't judge what it receives. It encodes all seven, and that hidden newline ends up inside czNjcmV0Cg==.
Same letters, different bytes
Compare the two Secrets. With plain echo, the encoded value ends in Cg==, and that tail is the newline. With echo -n, the value stops at czNjcmV0, which decodes to exactly s3cret. Print either one and you'll see the same letters, but the stored bytes are different.
The database compares every byte
Follow the value through. Kubernetes decodes the Secret and hands the App Pod s3cret plus a newline. The app sends that whole value to Postgres as the password. Postgres compares bytes exactly, and a newline isn't part of s3cret. So the login fails with auth failed, even though the visible password looks right.
Fix 1: echo -n, then verify
The first fix is echo with the dash n flag, which tells it not to add a trailing newline. Now base64 gives czNjcmV0. To verify, decode the stored value and pipe it to od -c, which prints hidden characters. You should see just s3cret. A broken Secret would end with backslash n.
$ echo -n "s3cret" | base64
czNjcmV0
# check what the Secret really holds
$ kubectl get secret db -o jsonpath='{.data.password}' | base64 -d | od -c
0000000 s 3 c r e t
0000006Fix 2: let kubectl encode it
The second fix skips the shell pipeline entirely. With kubectl create secret generic, the flag --from-literal means the value is typed directly, exactly as s3cret. kubectl does the base64 step itself, so no echo is involved and no newline sneaks in. The Secret named db holds exactly six bytes.
$ kubectl create secret generic db \
--from-literal=password=s3cret
secret/db createdCatch it next time
Base64 preserves bytes, it never cleans them. So when a correct credential fails, decode the stored value and inspect it with od -c to see hidden characters. Also remember that --from-file keeps a file's trailing newline, too.
- Decode, then inspect with od -c
- Prefer echo -n or --from-literal
- Files can hide trailing newlines too
The password was right. It just had an extra Enter key.
The password was right. It just had an extra Enter key. Base64 encodes every byte, even invisible ones, so use echo -n or --from-literal.
