This guide explains how to secure secret data at rest in Kubernetes by encrypting it within the etcd datastore.
In this guide, you’ll learn how to secure secret data at rest in Kubernetes by encrypting it inside the etcd datastore. We cover creating secret objects, inspecting their base64-encoded storage, and finally enabling encryption at rest through an encryption configuration. This step-by-step process helps ensure that confidential information remains protected even if someone gains access to your etcd datastore.
Begin by launching your single-node Kubernetes playground built with Kubernetes and ContainerD. Open your terminal to create a secret object using various methods. Here are several examples:
Copy
Ask AI
# Create a new secret named my-secret by loading files from a directorykubectl create secret generic my-secret --from-file=path/to/bar# Create a secret with specified keys taken from disk fileskubectl create secret generic my-secret --from-file=ssh-privatekey=path/to/id_rsa --from-file=ssh-publickey=path/to/id_rsa.pub# Create a secret with literal values for keyskubectl create secret generic my-secret --from-literal=key1=supersecret --from-literal=key2=topsecret# Create a secret using a combination of a file and a literal valuekubectl create secret generic my-secret --from-file=ssh-privatekey=path/to/id_rsa --from-literal=passphrase=topsecret# Create a secret from environment fileskubectl create secret generic my-secret --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env
Additional customization options include:
--allow-missing-template-keys=true
--append-hash=false
--dry-run='none'
After creating the secret, verify its existence with:
Copy
Ask AI
controlplane ~ ➜ kubectl create secret generic my-secret --from-literal=key1=supersecretsecret/my-secret createdcontrolplane ~ ➜ kubectl get secretNAME TYPE DATA AGEmy-secret Opaque 1 5s
This demonstrates that anyone with access to the secret manifest can decode the data.
Storing secrets in base64 does not provide true security. Without encryption at rest, confidential data can be exposed by anyone with direct access to the etcd datastore.
Running the command with the secret key from our object yields similar unencrypted output, which clearly demonstrates that secret data (i.e., the password “supersecret”) is stored without encryption. Anybody with etcd access and the appropriate certificates can retrieve and decode this information.
After installation, running etcdctl displays usage information along with a relevant warning regarding the API version:
Copy
Ask AI
controlplane ~ ➜ etcdctlNAME: etcdctl - A simple command line client for etcd.WARNING: Environment variable ETCDCTL_API is not set; defaults to etcdctl v2. Set environment variable ETCDCTL_API=3 to use v3 API or ETCDCTL_API=2 to use v2 API.
Ensure your Kubernetes cluster contains the necessary certificate files, such as /etc/kubernetes/pki/etcd/ca.crt. Then inspect the raw secrets stored in etcd using:
Before proceeding further, confirm that the Kube API server is configured with an encryption provider. Check for the --encryption-provider-config flag in the process arguments or in the API server manifest file. If the flag is absent, you must enable encryption at rest for your secrets.
To secure your secret data at rest, create an encryption configuration file that specifies which resources to encrypt and which encryption providers to use. Create a file named “enc.yaml” with the following content:
Modify the Kube API server manifest (typically found at /etc/kubernetes/manifests/kube-apiserver.yaml) to include a new volume mount and add the --encryption-provider-config flag. An example snippet is as follows:
You should no longer see the plain text value “topsecret” in the output, confirming that the data is now encrypted.To update and re-encrypt pre-existing secrets, run:
In this guide, we demonstrated how Kubernetes stores secret data as base64‑encoded strings in etcd, highlighting the vulnerabilities of unencrypted data. We then enabled encryption at rest by creating an encryption configuration file, updating the Kube API server manifest, and subsequently verifying that both new and updated secrets are securely encrypted. Following these steps is essential to protect critical data from unauthorized access.Thank you for reading this guide on encrypting secret data at rest in Kubernetes. For further details, refer to the Kubernetes Documentation.