Sub topic 9
Creating Secrets
This chapter assumes you can use kubectl, read YAML manifests, and know what a namespace is. You should also know the Secret structure from the earlier chapter: values live under data as base64 text, and base64 is an encoding, not encryption.
Three ways to create a Secret
You can create a Secret from literal values, from files, or from a manifest. All three produce the same API object.
| Method | Command or field | Best for |
|---|---|---|
| Literals | `kubectl create secret generic --from-literal` | Quick tests, small values |
| Files | `kubectl create secret generic --from-file` | Certificates, key files, multi-line content |
| Manifest | `stringData` or `data` in YAML | Declarative workflows |
Imperative creation
With kubectl create secret, kubectl does the base64 encoding for you.
kubectl create secret generic db-credentials \
--from-literal=username=app \
--from-literal=password='S3cr3t!'
kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 --decodeQuote values that contain shell characters such as !. With --from-file, the file name becomes the key. Use --from-file=key=path to choose a different key name.
The second command shows the point of the previous chapter. Anyone who can read the Secret can decode it.
Declarative creation with stringData
In a manifest, stringData lets you write plain text. The API server encodes it and stores it under data.
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
stringData:
username: app
password: S3cr3t!Apply it with kubectl apply -f secret.yaml.
stringData is write-only. When you read the Secret back, you see only data. If a key appears in both fields, stringData wins.
If you use data instead, you must encode each value yourself. Use echo -n, because a trailing newline becomes part of the secret.
echo -n 'S3cr3t!' | base64Choosing a type
The type field defaults to Opaque, which accepts any keys. Other built-in types make the API server check that required keys exist. For example, kubernetes.io/tls requires tls.crt and tls.key.
kubectl create secret tls web-tls --cert=tls.crt --key=tls.keyThe type is set at creation and cannot be changed later.
The creation flow
Common mistakes
- Trailing newline. Encoding with
echoinstead ofecho -nadds a newline. Logins then fail in ways that are hard to see. - Wrong namespace. A Secret is namespaced. A Pod can only reference Secrets in its own namespace. Pass
-nwhen creating. - Shell history.
--from-literalstores the value in your shell history. Prefer--from-fileor--from-env-filefor real credentials. - Size. A Secret is limited to 1 MiB.
Verifying what you created
kubectl describe secret db-credentials lists keys and byte sizes but not values. This is a safe way to confirm that a key exists and has a plausible length.
kubectl describe secret db-credentialsExercise
You apply a Secret manifest that has stringData: {token: abc}. You then run kubectl get secret -o yaml. Which field holds the token, and what does it contain?
Show answer
The token appears under data, not stringData, as the base64 encoding of abc, which is YWJj. stringData is write-only and is not returned when you read the Secret.
Exercise
A login fails after you create a password Secret with echo 'pw' | base64 in a data field. What is the likely cause and the fix?
Show answer
echo added a trailing newline, so the stored password is pw plus a newline. Re-encode with echo -n 'pw' | base64, or use stringData and let the API server do the encoding.