Secret

Kubernetes provides an object called 'Secret' that deals with the challenge of configuring sensitive data like passwords, tokens, keys, etc. without exposing this valuable data within images or Pod Specs. Secrets can be utilized either as volumes or as environment variables.

The Various Types of Secrets

Secrets in Kubernetes come in three different types:

  • Opaque: This is a Secret that is formatted in base64 encoding and used to store sensitive elements like passwords, keys, etc. However, it only offers weak encryption security as the data can be decoded back to the original form using base64 --decode.

  • kubernetes.io/dockerconfigjson: This type of Secret is used to maintain authentication information of a private Docker registry.

  • kubernetes.io/service-account-token: This variety is referred to by service accounts. When a service account is created, Kubernetes will automatically generate a paired secret. If a Pod utilizes a service account, the matching secret will be automatically mounted to the directory: /run/secrets/kubernetes.io/serviceaccount within the Pod.

Note: A service account enables a Pod to access the Kubernetes API.

API Version Corresponding Chart

Kubernetes Version
Core API Version

v1.5+

core/v1

Opaque Secret

The data for this type is a map requiring the value to be in base64 encoding format:

$ echo -n "admin" | base64
YWRtaW4=
$ echo -n "1f2d1e2e67df" | base64
MWYyZDFlMmU2N2Rm

secrets.yml

Create a secret: kubectl create -f secrets.yml.

Deceased: The default-token-cty7p is the default secret created when creating a cluster, which is referenced by serviceaccount/default.

If you are creating a secret from a file, you can use a simpler kubectl command, such as creating a TLS secret:

Using Opaque Secrets

Once a secret is created, there are two ways to use it:

  • As a Volume

  • As an Environment Variable

Mounting Secrets into Volumes

Here's the information within the Pod:

Exporting Secrets to Environment Variables

Mounting A Specific Key of the Secret

After creating the Pod successfully, you can see the following in the corresponding directory:

To be continued...

最后更新于