Creating secrets in Kubernetes
In Kubernetes, there are three different ways to create secrets: from files, from YAML or JSON definitions, or directly from the command line. Let's start the exploration of how to create secrets by creating them from files.
Creating Secrets from files
The first way to create secrets in Kubernetes is to create them from a file. In this way, the contents of the file will become the value of the secret, and the filename will be the identifier of each value within the secret.
Let's say that you need to store a URL and a secure token for accessing an API. To achieve this, follow these steps:
- Store the URL inÂ
secreturl.txt
, as follows:echo https://my-url-location.topsecret.com \ Â Â > secreturl.txt
- Store the token in another file, as follows:
echo 'superSecretToken' > secrettoken.txt
- Let Kubernetes create the secret from the files, as follows:
kubectl create secret generic myapi-url-token ...