Secrets allows creating data objects that can be consumed by Pods at runtime.
Here's an example of a Secret (YAML):
The manifest file of the Secret is pretty simple.
It contains the fields
apiVersion,
kind, and
metadata.
It also contain the
data field that contains the keys-values of the Secret.
The data in Secrets is stored as key-value entries:
-
Key: is a string value formed by alphanumeric, dot (.), dash (-), and underscore (_) characters.
-
Value: is a base64-encoded string value.
The key and the value, in the Secrets, are separated with a colon (
key:value).
The name of the Secrets must be a valid DNS subdomain name.
The data stored in a Secrets can be consumed in a container in one the following ways:
-
Environment variables.
-
Command-line arguments of the container command.
-
Files in volumes.
-
Custom code (read the Secrets directly from the Kubernetes API).
To encode a text into a base64 string:
The -n flag ensures that the generated output doesn't have an extra newline character at the end of the text.
This is to avoid that the extra newline character gets encoded along with the text.
To decode a base64 data:
Note:
If you update a Secret that was already posted to the Kubernetes API server,
then Pods that were already created will be able to leverage the new data only if it was injected as files in a volume.
Secrets data injected as environment variables won't be updated in a running Pod unless you recreate it.