Rotating a Kubernetes encryption at rest key is a staged change: every API server must learn to read with the new key before any of them writes with it, and only then can you re-encrypt existing data and retire the old key. Getting the order wrong does not usually fail loudly at first. It fails later, when one control plane node writes Secrets that another node cannot read, or when someone removes an old key while objects in etcd still depend on it.
This guide is the procedure for changing the key. If you have not yet confirmed that encryption at rest is switched on and working, start with How to check Kubernetes secrets encryption at rest, which covers enabling and checking the configuration. Everything here assumes an EncryptionConfiguration file is already in use on your control plane, and the commands and behaviour described come from the Kubernetes documentation on encrypting confidential data at rest and the Kubernetes documentation on using a KMS provider.
Key takeaways
- The order of keys in the
keysarray matters: the first key encrypts new writes, and every listed key can decrypt. - A safe local key rotation has two restart cycles: add the new key as the second entry everywhere, then promote it to first everywhere.
- Rotation only changes how new writes are encrypted. Existing objects stay under the old key until you rewrite them with
kubectl get secrets --all-namespaces -o json | kubectl replace -f -or an equivalent per namespace loop. - Back up the new key before you depend on it. Losing every copy of a key means deleting the resources encrypted under it.
- With KMS v2, rotating the key encryption key (KEK) in the external KMS does not need an API server restart, and the Kubernetes project recommends rotating the KEK at least every 90 days.
- Remove the old key only after you have verified, in etcd, that no object still carries its key name.
When to rotate, and what rotation does not fix
There are three common triggers for a rotation. The first is a schedule: your key management policy sets a cryptoperiod, and the key in the encryption configuration has reached it. The second is a provider constraint. The Kubernetes documentation states that the aesgcm provider must be rotated every 200,000 writes and is not recommended except when an automated key rotation scheme is implemented. The third is suspected exposure: a control plane host, a backup of the configuration file, or a configuration management repository that held the raw key may have been read by someone who should not have it.
Be clear about the limits. With local providers (aescbc, aesgcm, secretbox), the documentation notes that a locally managed key protects against an etcd compromise but not a host compromise, because the key sits on the host in the configuration file. If an attacker had the old key and a copy of etcd, re-encrypting today does not protect the copy they already took. In that case rotation of the encryption key must be paired with rotation of the Secrets themselves: the database passwords, API tokens and cloud credentials stored in them. Treat that as an incident, and follow the Kubernetes Cluster Compromise Playbook alongside this procedure.
Rotation also does not fix a weak provider choice. The documentation describes aescbc as not recommended because of CBC's vulnerability to padding oracle attacks, and describes KMS v2 as a good choice if you use a third party tool for key management. If you are rotating anyway, consider whether this is the moment to move to KMS, which is covered later in this guide.
How the encryption configuration chooses a key
An EncryptionConfiguration lists resources (for example secrets and configmaps) and, for each group, an ordered list of providers. Inside a local provider there is an ordered list of named keys. Two rules drive the whole rotation procedure:
- The first provider, and within it the first key, is used to encrypt new writes.
- Every configured provider and key is tried when reading. Stored data carries a prefix that names the provider and key. The documentation's example shows a Secret in etcd beginning with
k8s:enc:aescbc:v1:key1:, which tells you both the provider and the key name.
That prefix is what makes a zero downtime rotation possible. You can list key2 and key1 together, write with key2, and still read anything written under key1. It is also what lets you prove the rotation finished: when no object in etcd carries :key1:, the old key can go.
Two other rules from the documentation matter here. The identity provider stores data as plain text and is the default if nothing else is specified. And for clusters with two or more control plane nodes, the encryption configuration should be identical across each node, because a difference may mean that a kube-apiserver cannot decrypt data. During a rotation you will deliberately have short windows where nodes differ, so the procedure is designed so that every intermediate state can still decrypt everything.
Before you start
- Confirm who runs the control plane. These steps apply to self-managed clusters where you edit the kube-apiserver static pod manifest and the configuration file. Managed Kubernetes services normally handle envelope encryption through the cloud provider's KMS integration; follow your provider's own documentation for key rotation there rather than editing control plane files.
- Find the configuration file. Look for the
--encryption-provider-configflag in the kube-apiserver manifest, commonly under/etc/kubernetes/manifests/kube-apiserver.yamlon kubeadm clusters. Note whether--encryption-provider-config-automatic-reload=trueis set. - Take an etcd snapshot and record which key decrypts it. A snapshot taken before rotation can only be restored with the old key in the configuration, so keep the old key in your secure backup until the last such snapshot ages out.
- Check file permissions. The documentation requires that only the user who runs kube-apiserver can read a configuration file that contains key material. Check this on every node before you put a new key in it.
- Plan a secure copy path. The documentation says to copy keys to other control plane hosts using a secure mechanism, at a minimum with encryption in transit such as SSH.
- List every resource type in scope. If your configuration encrypts
configmapsor custom resources as well as Secrets, the re-encryption step must cover each of them. - Agree a change window. No downtime is expected, but each kube-apiserver restart briefly removes that node from the API pool.
Step-by-step: rotating a local provider key
This sequence follows the documented "rotate a decryption key" procedure. The example assumes an aescbc provider with a current key named key1; substitute your provider and names.
1. Generate the new key
The documentation's method on Linux and macOS is to generate a 32 byte random key and base64 encode it:
head -c 32 /dev/urandom | base64
Run it on a trusted host, avoid leaving the output in shell history or tickets, and keep the key confidential from the moment it is generated.
2. Add the new key as the second entry on every node
Edit the configuration on each control plane node so that the current key stays first and the new key is added after it:
providers: - aescbc: keys: - name: key1 secret: <old key> - name: key2 secret: <new key>
Use a new, unique key name. Reusing a name for different key material makes the etcd prefix meaningless for verification.
3. Restart all kube-apiserver processes
Restart each API server in turn so that every one of them can decrypt data written under key2. Nothing is yet encrypted with the new key, so this stage is safe even if one node lags behind. Do not continue until every API server is running with the new file. If automatic reload is enabled, see the section on reload below instead of restarting.
4. Back up the new key
The documentation is explicit: make a secure backup of the new key now. If you lose all copies, you would need to delete all resources encrypted under it, and workloads may not operate as expected while encryption at rest is broken. Store it where your other root key material lives, with access logging.
5. Promote the new key to first on every node
Swap the order so key2 is first and key1 is second. Keep key1: existing data still needs it.
6. Restart all kube-apiserver processes again
After this restart cycle, every API server encrypts new writes with key2. Because step 3 already taught every node to read key2, a node that has not yet restarted can still read what the others write.
7. Re-encrypt existing data
As a privileged user, rewrite every Secret so it is stored under the new key:
kubectl get secrets --all-namespaces -o json | kubectl replace -f -
The next section covers doing this safely on large clusters and for other resource types.
8. Remove the old key
Once all existing objects are re-encrypted and you hold a secure backup of the new key, remove key1 from the configuration on every node and restart (or wait for reload) once more. Verify first, as described below.
Re-encrypting existing data at scale
The kubectl replace pipeline reads each object and writes it back unchanged, which forces the API server to encrypt it with the current first key. The documentation notes three practical points: if a conflicting write causes an error, retry; it is safe to run the command more than once; and for larger clusters you may wish to subdivide by namespace or script the update.
An illustrative per namespace loop, which limits the size of each request and makes failures easier to pin down:
for ns in $(kubectl get ns -o jsonpath='{.items[*].metadata.name}'); do kubectl get secrets -n "$ns" -o json | kubectl replace -f - || echo "retry $ns"; done
Points to plan for:
- Permissions. The identity running the rewrite needs to read and write every Secret. Use a short lived, audited administrator session, not a long lived service account token created for the job. Our guide How to rotate a Kubernetes service account token and detect old token use covers cleaning up any token you do create.
- Other resource types. If the configuration encrypts
configmapsor custom resources, run the same read and replace pattern for each of those types. The documentation says to adjust the steps to match the API kinds you encrypt. - Controllers and watchers. A replace produces update events. Operators and controllers that react to Secret changes may reconcile or restart workloads. Test in a staging cluster and warn application owners.
- Audit volume. Every rewrite is an audit event. Tag the change ticket so the detection team can recognise the burst; the Kubernetes Audit Log Detection Guide explains how bulk Secret access normally shows up.
- Objects created during the run. Anything written after step 6 is already under the new key, so the run does not need to be perfectly atomic.
Rotating the key encryption key with KMS v2
With a KMS provider, Kubernetes uses envelope encryption: data is encrypted by data encryption keys (DEKs) using AES-GCM, and DEKs are encrypted by a KEK held in the external KMS. The documentation lists KMS v2 as stable from Kubernetes v1.29, with KMS v1 deprecated since v1.28. For KMS v2, Kubernetes generates a new DEK per encryption from a secret seed, and the seed is rotated whenever the KEK is rotated. See the Envelope Encryption and KMS Integration Patterns Guide for the general pattern.
Rotation here is mostly a KMS operation, not a Kubernetes file edit:
- Rotate the KEK in the external KMS using its own rotation feature, and confirm the plugin starts reporting a new
key_id. Thekey_idis the public, non secret name of the KEK in use; the API server treats the value returned by the plugin'sStatuscall as authoritative, and a change signals that the KEK has changed. - Wait for the API servers to notice. The API server polls
Statusabout every minute and coasts on the last valid state for about three minutes, so rotation is not immediate. If you rely on waiting, the documentation says to schedule migration at3 + N + Mminutes after the KEK rotation, where N is how long the plugin takes to observe the change and M is a buffer of at least five minutes. - Re-encrypt with the same
kubectl replacepattern so stored objects are rewritten under the new KEK. - Keep the old KEK enabled in the KMS until verification is complete and old etcd snapshots have aged out. Disabling it early makes old data unreadable.
The documentation states that no API server restart is required for KEK rotation, and it recommends rotating the KEK at least every 90 days, because you do not control how many writes are performed with a DEK. It also requires plugins never to reuse a key_id, even if an old KEK is reinstated, so that stale data can always be identified.
If you are moving from a local key to KMS as part of this change, the documentation's approach is the same ordering logic: add the kms provider as the first entry, keep the old local provider after it for reading, restart, run the replace command, then remove the local provider once nothing depends on it. Plugin health is exposed at /healthz/kms-providers (or per plugin endpoints such as /healthz/kms-provider-0 for KMS v1 without automatic reload); check it before and after each step.
Using automatic reload instead of restarts
When kube-apiserver runs with --encryption-provider-config-automatic-reload=true, it watches the configuration file instead of reading it once at start up. The documentation states that file changes are polled every minute, and that the apiserver_encryption_config_controller_automatic_reload_last_timestamp_seconds metric identifies when a new configuration becomes effective. This allows keys to be rotated without restarting the API server.
Reload does not remove the need for ordering. Each "restart all API servers" step in the local procedure becomes "update the file on every node, then confirm on every node that the reload metric has moved past the time of your edit". Only then move on to promoting the key or running the re-encryption. With reload enabled, all KMS health checks also collapse into the single /healthz/kms-providers endpoint.
Verify the rotation
Verification happens in etcd, not through the API, because the API always returns decrypted data. The documentation's method uses etcdctl with the etcd client certificates to read a Secret's raw value and inspect the prefix, for example:
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key get /registry/secrets/default/secret1 | hexdump -C
- New writes: create a test Secret after step 6 and confirm its prefix names the new key, for example
k8s:enc:aescbc:v1:key2:. For KMS the prefix isk8s:enc:kms:v2:. - Existing data: after re-encryption, sample Secrets across namespaces, or script a scan of the
/registry/secrets/key space, and confirm none still carry the old key name. - Read path: confirm
kubectl get secret <name> -o yamlstill returns the expected value, proving the API server decrypts correctly. - Every node: repeat a read through each API server endpoint, not just through the load balancer, to catch a node with a stale file.
- Other types: repeat the prefix check for
configmapsand any custom resources in scope.
Only when every check passes should you remove the old key.
Rollback and recovery
- Before the new key is first (after step 3): nothing is encrypted with it yet. Remove it from every node and restart; there is no data impact.
- After promotion but before removing the old key: put the old key back in first position on every node and restart. Data written under the new key stays readable because the new key is still listed second. Do not remove the new key until you have re-encrypted back.
- A node cannot decrypt: compare the configuration file on every node. Mismatched files are the most common cause, and the documentation warns that a difference may mean kube-apiserver cannot decrypt data.
- An old key was removed too early: restore it from your secure backup into the configuration as a non first entry and restart. If no copy exists, the documentation says resources encrypted under the lost key would need to be deleted and recreated, which for Secrets means re-issuing the credentials they held.
- Restoring an old etcd snapshot: the configuration must include the key that encrypted that snapshot. Keep a record linking snapshots to key names.
Common failure patterns
- One restart cycle instead of two. Adding the new key as first and restarting nodes one by one means early nodes write data that late nodes cannot read.
- Skipping re-encryption. The configuration lists a new key, but most Secrets are still under the old one, so removing it later breaks reads.
- Removing the old key on a schedule rather than on evidence. No etcd prefix scan was run before deletion.
- Leaving
identityfirst or as a quiet fallback. The documentation describes removing theidentityentry once all data is encrypted, so the API server cannot read plain text Secrets even by accident. - Key material in the wrong places. The new key ends up in a ticket, a chat message, a Git repository or a world readable file, which undoes the rotation.
- Treating key rotation as credential rotation. After a suspected exposure, the stored Secrets remain valid credentials for whoever copied them.
- Disabling the old KMS KEK straight away. Data and snapshots still wrapped by it become unreadable.
- Forgetting other encrypted types. Secrets are re-encrypted but ConfigMaps or custom resources in the same configuration are not.
Practitioner checklist
- Record the current provider, key names, file location and reload setting on every control plane node.
- Take and label an etcd snapshot, and keep a backup of the old key for as long as that snapshot is retained.
- Generate the new key on a trusted host and copy it to each node over an encrypted channel.
- Add the new key second on every node, then restart or confirm reload on every node.
- Back up the new key before promoting it.
- Promote the new key to first on every node, then restart or confirm reload again.
- Re-encrypt Secrets and every other encrypted resource type, namespace by namespace if the cluster is large.
- Scan etcd for the old key name; confirm reads through every API server.
- Remove the old key, restart or reload, and record the change against your key inventory.
- For KMS v2, rotate the KEK at least every 90 days and keep old KEKs enabled until stale data and snapshots are gone.
- If exposure is suspected, rotate the credentials inside the Secrets as well as the encryption key.
Standards and references
- Kubernetes: Encrypting Confidential Data at Rest
- Kubernetes: Using a KMS provider for data encryption
- NIST: SP 800-57 Part 1 Rev. 5, Recommendation for Key Management
- NIST: SP 800-53 Rev. 5 (SC-12 Cryptographic Key Establishment and Management; SC-28 Protection of Information at Rest)
- MITRE ATT&CK: T1552 Unsecured Credentials
- CIS: Critical Security Controls v8.1
Related NHI Mgmt Group resources: How to check Kubernetes secrets encryption at rest · Kubernetes Secrets Security Guide · How to migrate Kubernetes secrets to an external Secrets Manager · Secrets Rotation Runbook Template