> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kubernetes Secret Management

> How to securely manage Kubernetes secrets, comparing hardening cluster storage versus external secrets managers and recommending best practices for encryption, RBAC, and avoiding environment variable leaks.

How should you manage secrets on Kubernetes?

Consider a food-delivery application running on Kubernetes. When a customer pays, the payment service (running as a pod inside the cluster) calls an external payment provider such as Stripe. Each payment request includes an API key that proves the request originates from your application.

A natural place to store that API key is a Kubernetes Secret — but is it actually secret?

## Where Kubernetes stores Secrets

Kubernetes stores Secret objects in etcd. By default, values are written to etcd unencrypted on disk. For example, an API key like:

`sk_live_51Hx9Qp`

can end up sitting in etcd in plaintext (base64-encoded), meaning anyone who can read etcd or access etcd backups can obtain the key.

Anyone with permission to read Secrets can run:

```bash theme={null}
kubectl get secret stripe-key -o yaml
```

The output contains base64-encoded data:

```yaml theme={null}
apiVersion: v1
data:
  apiKey: c2tfbGl2ZV81MUh4OVFw
kind: Secret
metadata:
  name: stripe-key
```

Decoding that base64 value reveals the API key:

```bash theme={null}
echo c2tfbGl2ZV81MUh4OVFw | base64 --decode
# sk_live_51Hx9Qp
```

Base64 is an encoding, not encryption. Encoding makes data safe for transport/storage formats, but it does not protect confidentiality.

## Pod access patterns and environment-variable leaks

Any pod in the same namespace can mount or reference a Secret if its Pod spec includes that Secret. Without namespace isolation and strict RBAC, other workloads in the namespace may be able to access Secrets.

A common, but risky, pattern is exposing secrets to applications via environment variables. If a developer prints environment variables for debugging, that output goes to the pod logs. Many teams aggregate logs to centralized systems ([Datadog](https://www.datadoghq.com/), [Splunk](https://www.splunk.com/)), so sensitive values can leak there.

Example JavaScript snippet that prints environment variables:

```javascript theme={null}
console.log(process.env);
```

Example log output that includes the API key:

```text theme={null}
STRIPE_API_KEY=sk_live_51Hx9Qp
PORT=8080
```

Avoid printing or persisting secrets in logs. Centralized logging systems can expose secrets to anyone with log access.

## Two common approaches to mitigate these risks

You have two primary approaches to secure secrets used by workloads on Kubernetes. Choose based on your risk tolerance, compliance needs, and operational maturity.

### 1) Keep Secrets in Kubernetes, but harden the cluster

* Enable encryption at rest for etcd so the API server encrypts Secret values before writing them to disk.
* Manage the encryption key with a cloud Key Management Service (KMS) to separate key material from the cluster.
* With encryption at rest enabled, etcd backups contain encrypted blobs that are useless without KMS access.
* Apply namespace isolation and strict RBAC so only the payment service’s namespace and specific service accounts can create pods or read Secrets.
* Limit use of environment variables for secrets and prefer mounted files with limited file permissions when necessary.

Example conceptual representation of an encrypted Secret stored in etcd:

```text theme={null}
stripe-key
k8s:enc:5AmG5zt
```

### 2) Move secrets out of Kubernetes into a dedicated secrets store

A stronger pattern is to keep secrets outside Kubernetes in a purpose-built secrets manager (HashiCorp Vault, AWS Secrets Manager, GCP Secret Manager, etc.). A typical flow when using Vault looks like this:

* The payment pod authenticates with Vault using its Kubernetes service account token.
* Vault verifies the token against the Kubernetes API server and enforces policies to decide which secrets the pod can access.
* Rather than pushing secrets into containers directly, deploy a Vault agent as a sidecar in the same pod.
* The Vault agent writes secrets to a shared emptyDir or serves them in-memory so the application reads them from a local file or memory, avoiding creation of a Kubernetes Secret object that would persist into the API server.

This pattern reduces blast radius and centralizes secret lifecycle management (rotation, audit logging, fine-grained access control).

![A dark-themed architecture diagram titled "MOVE IT OUT" showing a payments namespace with a Payment app and a Vault/Vault Agent box containing a highlighted secret key. The graphic also shows cluster and service account token labels and dotted boundaries connecting the components.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/Kubernetes/Kubernetes-Secret-Management/move-it-out-payments-vault-secret.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=c05c8c613f9d4168c95196ff6e757a0e)

## Comparison: Keep in Kubernetes vs. Dedicated secrets store

| Approach | Pros | Cons | When to use |
| - | -: | - | - |
| Hardened Kubernetes Secrets | Simpler (uses built-in primitives); lower operational overhead; works well with small teams | Risk of accidental persistence in etcd/backups; requires careful RBAC, etcd encryption and key management | Small clusters or teams that prefer native primitives and can enforce strict controls |
| Dedicated Secrets Store (Vault, AWS Secrets Manager, etc.) | Centralized rotation, audit logging, fine-grained access; reduces secrets in etcd; supports dynamic secrets | Additional infrastructure and integration work (sidecars/agents, auth plumbing) | Environments with high compliance needs, many teams, or frequent secret rotation requirements |

## Practical best practices

* Use KMS-backed etcd encryption if you keep secrets in Kubernetes.
* Use least privilege RBAC and isolate namespaces by workload.
* Avoid exposing secrets via environment variables; prefer mounted files or in-memory injection.
* Rotate credentials regularly and use short-lived/dynamic secrets when possible.
* Audit access to secrets and monitor for anomalous reads.
* Avoid storing secrets in source control or in plain text in logs.

For production-grade security, combine multiple measures: encrypt etcd at rest, enforce RBAC and network isolation, and consider an external secrets manager for centralized rotation and auditing.

## Links and References

* [Kubernetes Secrets - official docs](https://kubernetes.io/docs/concepts/configuration/secret/)
* [etcd encryption at rest - Kubernetes documentation](https://kubernetes.io/docs/tasks/administer-cluster/encrypt-data/)
* [HashiCorp Vault](https://www.vaultproject.io/)
* [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/)
* [GCP Secret Manager](https://cloud.google.com/secret-manager)
* [Datadog](https://www.datadoghq.com/) — centralized logging and monitoring
* [Splunk](https://www.splunk.com/) — centralized logging and analysis

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/60905e58-3a8e-4423-9743-081b4959f0a0/lesson/bdf5dc18-932d-4534-92dd-eab582c1bccb)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.