How to Create and Use Kubernetes Secrets
Learn how to securely store your sensitive information in Kubernetes secrets and access those secrets in your manifests.
Secrets are any sensitive information used by applications and services. Nearly all applications use sensitive configuration information, from database credentials to TLS\SSL certificates. For obvious security reasons, it is a good proactive to keep your code separate from your secrets.
Kubernetes Secrets allow us to store our sensitive information separately from our resources that use it. In this tutorial, you will learn how to create secrets in Kubernetes, and also how to use those secrets in your other resources, such as pods and deployments.
Creating Secrets
To begin we will create secrets from the shell using kubectl
. The basic syntax of the kubectl create secret
command is the following.
kubectl create secret <type> <name> <flags>
There are three types of secrets that can be created from the command-line:
- docker-registry
- generic
- tls
Generic Literal Strings
To set a secret using a string from the command-line, use the --from-literal
flag. There is no limit to the number of --from-literal
flags that can be used.
kubectl create secret generic example-secrets --from-literal=db.password=super-secret-password
Generic From File
Alternatively, secrets can be imported from a properties file. The syntax for creating secrets from a file
kubectl create secret generic example-secrets --from-file=<filepath>
For example, a properties file named DB-credentials.txt
has bas the following content.
db.password=super-secret-password
db.user=app1-prod
To create secrets for db.password
and db.user
in a secret named example-secrets
, the following kubectl
command would be executed.
kubectl create secret generic example-secrets --from-file=db-credentials.txt
Secrets Manifests
data
key.
The example fellow adds data keys for a web application: db.password
, db.user
, and certificate
. Look carefully at the values for each key. While the db.password
and db.user
could pass clear text values, the certificate
value is obviously not a clear text certificate. All values under the data
key of a Secrets manifest must be Base64 encoded.
apiVersion: v1
kind: Secrets
metadata:
name: my-secrets
data:
db.password: c3VwZXItc2VjcmV0LXBhc3N3b3Jk
db.user: ZGVtby1hcHAtcHJvZA==
certificate: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUZRakNDQXlvQ0NRQ3BrQ1ZUNm9UdzhqQU5CZ2txaGtpRzl3MEJBUXNGQURCak1Rc3dDUVlEVlFRR0V3SkQKUVRFUU1BNEdBMVVFQ0F3SFQyNTBZWEpwYnpFUU1BNEdBMVVFQnd3SFZHOXliMjUwYnpFVE1CRUdBMVVFQ2d3SwpRMnh2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0U25XOVVJc2lLbGpOTWJCZU1ZYjdiajM1dG8raStqOGp6CkdyeVhIQlM5L2NJeEJqaUtCNWNVbXRkcGhmU2JNWXdNby9FUWJOeWlrdm84Q2M0S3pnZjMySTkza0EvRmd3VG8KZ055YWpiOUkKLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
If you are running OSX or Linux you likely have a base64
command available out-of-the-box. To encode a string value as base64, you can run the following command:
echo -n 'my-secret-text' | base64
| output
Xktc2VjcmV0LXRleHQ=
Follow Us