How to Store Sensitive Data Using Kubernetes Secrets

In this article, we will explore the world of Kubernetes Secrets and discover how they can help you store sensitive data securely. From login credentials to tokens and keys, Kubernetes Secrets are designed to keep your valuable information safe. These secrets can be created outside of Pods and stored in the Kubernetes data store, ensuring their confidentiality. By encoding values in base64 and marking them as immutable, you can prevent any unwanted updates. Furthermore, Kubernetes Secrets can be injected into Pods in various ways, such as data volumes, environment variables, or imagePullSecrets. With different types of Kubernetes Secrets available, including Opaque Secrets and TLS Secrets, you’ll have the flexibility to choose the best option for your specific needs. So let’s dive in, and learn how to utilize Kubernetes Secrets effectively.

How to Store Sensitive Data Using Kubernetes Secrets

Types of Kubernetes Secrets

Opaque Secrets

Opaque Secrets are the most basic type of Kubernetes Secret. They store arbitrary key-value pairs of sensitive data. These key-value pairs can be used to store any kind of data, such as login credentials, tokens, or keys. Opaque Secrets are useful when you have data that does not require any specific interpretation or processing by Kubernetes.

Service account token Secrets

Service account token Secrets are used to securely access the Kubernetes API from within a Pod. By default, each Pod in a Kubernetes cluster is automatically assigned a service account token, which allows it to authenticate with the Kubernetes API. Service account token Secrets store this token, and can be mounted as a volume in a Pod to securely access the Kubernetes API.

Docker config Secrets

Docker config Secrets are used to securely authenticate with private Docker registries. When you create a Docker config Secret, you can specify the username and password for the Docker registry. This Secret can then be used as an imagePullSecret to authenticate when pulling images from the private registry.

Basic authentication Secrets

Basic authentication Secrets are used to store username and password credentials for authentication purposes. These Secrets can be used to authenticate with external services, such as a database or an API, that require basic authentication. The username and password are stored in the Secret and can be accessed by the Pod when needed.

SSH authentication Secrets

SSH authentication Secrets are used to store SSH private keys and other related information. These Secrets can be used to authenticate with remote servers using SSH. The private key is stored in the Secret and can be mounted as a volume in the Pod to securely access the remote server.

TLS Secrets

TLS Secrets are used to store TLS certificates and private keys for secure communication between Pods or between a Pod and an external service. These Secrets can be used for HTTPS communication, mutual TLS authentication, or any other scenario that requires TLS encryption. The certificate and private key are stored in the Secret and can be mounted as a volume in the Pod.

Bootstrap token Secrets

Bootstrap token Secrets are used during the bootstrap process of a new Kubernetes cluster. These Secrets are automatically created and used for initial cluster setup. They are mainly used for authentication and authorization purposes during the cluster bootstrapping process.

Creating Kubernetes Secrets

Using kubectl

You can create Kubernetes Secrets using the kubectl command-line tool. To create a Secret, you need to provide the data and/or stringData fields with the sensitive information. The data field is used to store binary data, such as TLS certificates, encoded in base64. The stringData field is used to store plain text data, such as login credentials, which doesn’t need to be encoded.

To create a Secret using kubectl, you can run the following command:

kubectl create secret –from-literal==

Using manifest files

Another way to create Kubernetes Secrets is by using manifest files. Manifest files are YAML or JSON files that describe the desired state of Kubernetes resources. You can create a Secret manifest file and provide the sensitive information in the appropriate fields. Then, you can apply the manifest file using kubectl to create the Secret.

Here is an example of a Secret manifest file:

apiVersion: v1 kind: Secret metadata: name: my-secret type: data: username: password:

You can apply the manifest file using the following command:

kubectl apply -f secret.yaml

Using generators like Kustomize

Kustomize is a tool that allows you to customize Kubernetes resources, including Kubernetes Secrets, using simple declarative configuration files. You can use Kustomize to generate Secrets based on a set of parameters defined in a kustomization.yaml file. This allows you to create Secrets with different values for different environments or configurations.

To create a Secret using Kustomize, you need to define the parameters and values in a kustomization.yaml file, like this:

apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources:

  • secret.yaml vars:
  • name: username objref: kind: Secret name: my-secret apiVersion: v1 fieldref: fieldpath: data.username
  • name: password objref: kind: Secret name: my-secret apiVersion: v1 fieldref: fieldpath: data.password

Then, you can run the kubectl apply command with the -k flag and the path to the directory containing the kustomization.yaml file to create the Secret:

kubectl apply -k path/to/kustomization-directory

Managing Kubernetes Secrets

Describing Kubernetes Secrets

You can use the kubectl describe secret command to get detailed information about a Kubernetes Secret. This command displays metadata about the Secret, such as its name, type, and creation timestamp. It also shows the key-value pairs or data fields stored in the Secret.

To describe a Secret, you can run the following command:

kubectl describe secret

Decoding Kubernetes Secrets

Kubernetes Secrets store data in base64-encoded strings for security purposes. If you need to view the actual values stored in a Secret, you can use the kubectl get secret command with the --output=jsonpath flag to decode the values.

To decode the values of a Secret, you can run the following command:

kubectl get secret -o jsonpath='{.data.}’ | base64 –decode

Editing Kubernetes Secrets

You can edit the values stored in a Kubernetes Secret using the kubectl edit secret command. This command opens the Secret in your default text editor, allowing you to modify the values.

To edit a Secret, you can run the following command:

kubectl edit secret

How to Store Sensitive Data Using Kubernetes Secrets

Storing Sensitive Data in Kubernetes Secrets

Login credentials

Kubernetes Secrets are a secure way to store login credentials, such as usernames and passwords. You can create a Secret with the username and password as key-value pairs, and then use this Secret within your Pods to authenticate with external services. By storing login credentials in Secrets, you can avoid exposing sensitive information in your application code or configuration files.

Tokens

Tokens are another type of sensitive data that can be stored in Kubernetes Secrets. Tokens can be used for authentication and authorization purposes, allowing Pods to access specific resources or perform certain actions within the Kubernetes cluster. By storing tokens in Secrets, you can ensure that they are securely stored and easily accessible by your Pods.

Keys

Kubernetes Secrets are also useful for storing keys, such as API keys or encryption keys. Keys are essential for securing communication between services or encrypting sensitive data. By storing keys in Secrets, you can centralize their management and ensure that they are securely stored and easily accessible by your Pods.

Secure Storage of Kubernetes Secrets

Creating Secrets outside of Pods

Kubernetes Secrets are created outside of Pods to ensure secure storage and management of sensitive data. Secrets are stored in the Kubernetes data store and can be accessed by Pods when needed. By storing Secrets centrally, you can ensure that sensitive data is not exposed within your application code or configuration files, reducing the risk of security breaches.

Storing Secrets in Kubernetes data store

Kubernetes provides a built-in data store for storing Secrets. This data store is secure and encrypted, ensuring that the sensitive data stored in Secrets is protected. By using the Kubernetes data store, you can ensure that your Secrets are stored in a secure and reliable manner.

Marking Secrets as immutable to prevent unwanted updates

To prevent unwanted updates and ensure the integrity of sensitive data, you can mark Kubernetes Secrets as immutable. An immutable Secret cannot be modified once it is created, ensuring that the data stored in the Secret remains unchanged. This is particularly useful for Secrets that contain keys or certificates, as any unauthorized modifications can compromise the security of your application.

Encoding Values in Kubernetes Secrets

Encoding values in base64

Kubernetes Secrets store values in base64-encoded strings to ensure security and prevent accidental exposure. When creating a Secret, you can encode the values in base64 before storing them. Similarly, when retrieving the values from a Secret, you need to decode the base64-encoded strings to get the original values.

To encode a value in base64, you can use the echo command followed by the base64 command:

echo -n “my-secret-value” | base64

To decode a base64-encoded value, you can use the base64 command with the -d flag:

echo -n “bXktc2VjcmV0LXZhbHVl” | base64 -d

Injecting Secrets into Pods

Using Secrets as data volumes

One way to inject Secrets into Pods is by using them as data volumes. You can mount a Secret as a volume in a Pod, allowing the Pod to access the data stored in the Secret. This is useful when you need to make sensitive data, such as configuration files or certificates, available to your Pods.

To mount a Secret as a volume in a Pod, you need to define a volume and a volume mount in the Pod’s configuration. The volume will reference the Secret, and the volume mount will specify the location within the Pod’s filesystem where the Secret should be mounted.

Here is an example of mounting a Secret as a volume in a Pod:

apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: – name: my-container image: my-image volumeMounts: – name: secret-volume mountPath: /etc/my-secret volumes: – name: secret-volume secret: secretName: my-secret

Using Secrets as environment variables

Another way to inject Secrets into Pods is by using them as environment variables. You can set environment variables in a Pod’s configuration and reference the values stored in a Secret. This allows your application code to access the sensitive data stored in the Secret using environment variables.

To use a Secret as an environment variable in a Pod, you need to define an environment variable in the Pod’s configuration. The value of the environment variable will reference the specific value stored in the Secret.

Here is an example of using a Secret as an environment variable in a Pod:

apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: – name: my-container image: my-image env: – name: MY_SECRET valueFrom: secretKeyRef: name: my-secret key: my-key

Using Secrets as imagePullSecrets

You can also use Secrets as imagePullSecrets to authenticate with private Docker registries. When pulling images from a private registry, you need to provide authentication credentials. By creating a Docker config Secret that contains the username and password for the registry, you can use it as an imagePullSecret.

To use a Secret as an imagePullSecret, you need to specify the Secret’s name in the imagePullSecrets field of your Pod’s configuration. Kubernetes will then use the Secret to authenticate when pulling images from the private registry.

Here is an example of using a Secret as an imagePullSecret in a Pod:

apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: – name: my-container image: private-registry/my-image imagePullSecrets: – name: my-secret

Using Secrets as Environment Variables

Injecting Secrets as environment variables in Pods

One of the ways to use Secrets in Kubernetes is by injecting them as environment variables in Pods. This allows you to access sensitive data stored in Secrets directly from your application code.

To inject a Secret as an environment variable in a Pod, you need to define the environment variable in the Pod’s configuration and reference the value stored in the Secret. You can use the env field in the container specification to define the environment variable and the valueFrom field to reference the Secret.

Here is an example of injecting a Secret as an environment variable in a Pod:

apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: – name: my-container image: my-image env: – name: MY_SECRET valueFrom: secretKeyRef: name: my-secret key: my-key

In this example, the environment variable MY_SECRET will be set to the value stored in the my-key field of the my-secret Secret.

Using Secrets as Mounted Files

Mounting Secrets as files in Pods

Another way to use Secrets in Kubernetes is by mounting them as files in Pods. This allows you to access sensitive data stored in Secrets as files within your Pods’ filesystem.

To mount a Secret as a file in a Pod, you need to define a volume and a volume mount in the Pod’s configuration. The volume will reference the Secret, and the volume mount will specify the location within the Pod’s filesystem where the Secret should be mounted.

Here is an example of mounting a Secret as a file in a Pod:

apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: – name: my-container image: my-image volumeMounts: – name: secret-volume mountPath: /etc/my-secret volumes: – name: secret-volume secret: secretName: my-secret

In this example, the my-secret Secret will be mounted as a volume at the path /etc/my-secret within the Pod’s filesystem. The contents of the Secret will be accessible as files within that path.

Using Secrets as ImagePullSecrets

Using Secrets as imagePullSecrets for private Docker registry access

When pulling images from a private Docker registry, you need to provide authentication credentials. Kubernetes allows you to use Secrets as imagePullSecrets to authenticate with private Docker registries.

To use a Secret as an imagePullSecret, you need to create a Docker config Secret that contains the username and password for the private registry. Then, you can reference this Secret in the imagePullSecrets field of your Pod’s configuration.

Here is an example of using a Secret as an imagePullSecret for private Docker registry access:

apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: – name: my-container image: private-registry/my-image imagePullSecrets: – name: my-secret

In this example, the my-secret Secret is used as an imagePullSecret for authenticating with the private registry private-registry. Kubernetes will use the credentials stored in the Secret to authenticate when pulling the my-image image from the registry.

By using Secrets as imagePullSecrets, you can securely access private Docker registries without exposing your credentials in plain text.

Turnkey Solutions

About SlickFinch

Here at SlickFinch, our solutions set your business up for the future. With the right DevOps Architecture and Cloud Automation and Deployment, you’ll be ready for all the good things that are coming your way. Whatever your big vision is, we’re here to help you achieve your goals. 

Let's Connect

Reach out to learn more about how SlickFinch can help your business with DevOps solutions you’ll love.