A Kubernetes Secret is not a secret. It is a base64-encoded object stored in etcd, readable by anyone with the right RBAC verb, and copied into at least three places the moment a pod starts using it. Teams that argue for weeks about env vars versus mounted files usually lose the credential through a door they never looked at: a last-applied-configuration annotation, a Helm release object, a CI log, or a service account that was allowed to list secrets in 2022 and still can.

So the useful question is broader than "how do I get the value into the container". It is how many copies of the value exist, who can read each copy, and how long each copy stays valid. The pod spec is the last and smallest part of that answer. It still comes first here, because the two native injection methods fail in different ways, and those failures decide how many of the other copies you create.

Env vars and mounted volumes behave differently under pressure

The two native options look interchangeable in a manifest. They are not, and the differences show up in incidents.

An environment variable sourced from secretKeyRef is resolved once, when the container starts. After that the value lives in the process environment. On Linux that means /proc/<pid>/environ, readable by the same user and by root on the node.

Every child process inherits it: the shell a developer opens with kubectl exec, the curl a health script spawns, the crash reporter that dumps the environment into a ticket. Many frameworks also print their configuration on startup at debug level, and a single log line at 09:14 can ship a database password into a log store that keeps data for 30 or 90 days.

A Secret mounted as a volume lands on a tmpfs filesystem inside the pod, so it never touches the node's disk. You control the file mode (defaultMode: 0400 is a sensible start), and the application reads the file only when it needs it. Volumes also update in place when the Secret changes, after a delay of up to the kubelet sync period (1 minute by default) plus cache propagation.

volumes:
  - name: db-creds
    secret:
      secretName: orders-db
      defaultMode: 0400
containers:
  - name: api
    volumeMounts:
      - name: db-creds
        mountPath: /var/run/secrets/db
        readOnly: true

The rotation advantage has a trap. If you mount a single key with subPath, that file never updates. The pod keeps the old value until it restarts, which is fine until the old credential is revoked at 02:00 and 40 replicas start failing authentication at once. Env vars have the same problem by design: changing the Secret does nothing to running containers.

A sensible default: files for anything that rotates, env vars only for values the application cannot read from disk, and no subPath on secret mounts at all.

Who can read a Secret is wider than RBAC suggests

People audit get on secrets and stop there. The list and watch verbs return full secret contents too, so a role with list on secrets in a namespace can read every value in it. A controller granted watch on secrets cluster-wide in 2023 because a Helm chart asked for it is a standing read path to every credential you run.

Pod creation is the bigger gap. Anyone who can create a pod in a namespace can mount any Secret in that namespace into it, then read it from inside the container. RBAC on the Secret object does not stop this, because the kubelet does the reading.

In practice a namespace is the security boundary for secrets, and "can create Deployments" in a namespace means "can read every secret in it". Teams that put 12 microservices from 4 different owners into one namespace have quietly given each owner the others' credentials.

Service account tokens deserve their own line. Since Kubernetes 1.22 pods get short-lived, audience-bound projected tokens by default, and since 1.24 the control plane stops creating long-lived token Secrets automatically. Older clusters upgraded in place often still carry legacy token Secrets that never expire. Set automountServiceAccountToken: false on workloads that never call the API server; most application pods do not.

kubectl auth can-i list secrets -n orders --as=system:serviceaccount:orders:api
kubectl get secrets -A --field-selector type=kubernetes.io/service-account-token

Two commands, five minutes, and you will usually find at least one surprise.

Humans are readers too. Every engineer who can run kubectl exec into a pod can read its mounted files and its environment, and audit logs record the exec but not what was typed afterwards. If 9 people hold exec rights in production for a service that 2 of them maintain, the other 7 are part of the secret's audience whether anyone intended it or not. Break-glass access with an expiry, granted per incident, keeps that number honest.

Copies at rest: etcd, apply annotations, Helm and Git

Where does a secret value live when nobody is looking at it? Count the copies.

The first is etcd. Without an encryption configuration on the API server, values sit in etcd in plain base64, and every etcd backup carries them. Encryption at rest uses an EncryptionConfiguration with providers such as aescbc, aesgcm, secretbox or kms. The KMS v2 provider, stable since Kubernetes 1.29, keeps the key-encryption key in an external service such as AWS KMS, Google Cloud KMS or Azure Key Vault. Managed offerings differ: some encrypt etcd by default, some need envelope encryption switched on per cluster. Check rather than assume.

The second copy is easy to miss. When you create a Secret with client-side kubectl apply, kubectl stores the full manifest in the kubectl.kubernetes.io/last-applied-configuration annotation. If that manifest used stringData, the annotation holds your password in clear text, and anyone who can read the object's metadata or a backup sees it. Server-side apply (kubectl apply --server-side) does not write this annotation.

The third copy is Helm. Helm 3 stores each release as a Secret in the release namespace, containing the rendered manifests. A chart that templates a database password into a Secret keeps that password in every stored revision, ten by default with --history-max, so rotating the value still leaves old ones readable in previous revisions.

The fourth copy is Git. Plain Secrets in a GitOps repository are permanent: deleting the file does not remove it from history or from every clone. Tools that keep only ciphertext in Git, such as Sealed Secrets or SOPS with a KMS key, close that copy. External Secrets Operator closes it differently, by keeping only a reference in Git and pulling the value from AWS Secrets Manager, HashiCorp Vault or Google Secret Manager at sync time.

Common mistakes that create extra copies:

  • Creating Secrets with client-side kubectl apply and stringData, leaving clear text in an annotation.
  • Echoing values in CI jobs while generating manifests, so the pipeline log holds them for its whole retention period.
  • Enabling the Secrets Store CSI Driver's sync-to-Secret option "just in case", which puts the value back into etcd and cancels the main reason for using the driver.
  • Leaving Helm release history at the default when charts render credentials.
  • Mounting a whole Secret with 15 keys when the container needs 2 of them.
  • Treating base64 as obfuscation and pasting decoded values into incident channels during debugging.

A formula for comparing injection methods

Debates between "env var", "volume", "CSI driver" and "Vault sidecar" go in circles because each side optimises a different variable. A rough exposure score makes the trade-off visible. It is a heuristic for comparing designs, not a standard metric.

The score multiplies three counts. Readers are the identities that can obtain the value: RBAC readers, pod creators in the namespace and humans with exec rights. Copies are the places the value exists at rest or in logs, such as etcd, backups, apply annotations, Helm revisions, Git and log stores. Validity is the number of days the value stays usable before rotation or revocation. Exposure is readers times copies times validity days.

An illustrative example. A payments namespace has 3 human operators with exec, 2 CI service accounts that create pods and 1 controller with list on secrets: 6 readers. The database password exists in etcd, nightly etcd backups, one Helm revision history and a log store that once captured it: 4 copies. It is rotated every 180 days. Exposure: 6 x 4 x 180 = 4,320.

Now change three things. Move the password to an external manager and mount it with the CSI driver without sync, which removes etcd, backups and Helm from the count: copies drop to 1 (the tmpfs inside running pods, ignoring the manager itself). Remove list from the controller and exec from 2 operators: readers drop to 3.

Switch to database credentials issued by Vault's database secrets engine with a 24-hour lease: validity drops to 1. New exposure: 3 x 1 x 1 = 3. That is a reduction of more than 1,000 times, and the largest single factor was validity, not the injection mechanism.

The arithmetic generalises. Short-lived, dynamically issued credentials beat any clever way of mounting a long-lived one. The mounting method matters most when you cannot shorten validity, for example a third-party API key that the vendor rotates only by hand.

When not to add an external secrets manager

The strongest case against everything above is operational. Every layer you add is a new way for a pod to fail at startup. The CSI driver needs its provider plugin healthy on every node; if it is down, pods sit in ContainerCreating and the incident looks like a scheduling problem for the first 20 minutes.

The Vault Agent Injector depends on a mutating webhook; a webhook outage can block every pod creation in the namespaces it watches. External Secrets Operator adds a sync interval, so a rotated value may take a full refresh period, one hour by default in many examples, to appear.

Do not adopt an external manager when the cluster serves one team, runs fewer than a handful of credentials, has etcd encryption on and has tight namespace RBAC. Native Secrets mounted as files, created with server-side apply and kept out of Git through SOPS, are enough there, and the team will actually understand them at 3 a.m.

Do not add Vault sidecars to batch jobs that run for 40 seconds; the agent's startup and lease renewal can cost more than the job. Use an init container that renders the file once, or the CSI driver.

Do not switch from env vars to files in an application that copies the file into an env var at startup anyway. You get the complexity of both and the safety of neither.

And do not introduce dynamic credentials until you can rotate a static one end to end without downtime. If a manual rotation today takes a change window and two engineers, a 24-hour lease will page someone every night.