Prompt
How do I store API keys securely in Kubernetes? Give me the simplest workable setup.
Latest observation
The simplest workable setup is:
- Put the key in a Kubernetes Secret
- Mount it into the pod as an environment variable or file
- Restrict access to the Secret with RBAC
- Avoid committing the Secret manifest to git in plain text
That’s the basic minimum. Here’s a practical setup.
1) Create a Secret
Option A: from literal value
kubectl create secret generic api-key-secret \
--from-literal=API_KEY='your-real-api-key'
Option B: from a file
kubectl create secret generic api-key-secret \
--from-file=api-key.txt=./api-key.txt
Kubernetes stores Secrets base64-encoded, not encrypted by default. That’s okay for a simple setup, but not truly “secure” unless you also enable encryption at rest in the cluster.
2) Use it in a Pod
As an environment variable
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
containers:
- name: app
image: nginx
env:
- name: API_KEY
valueFrom:
secretKeyRef:
name: api-key-secret
key: API_KEY
Or as a mounted file
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: secret-volume
mountPath: /etc/secrets
readOnly: true
volumes:
- name: secret-volume
secret:
secretName: api-key-secret
If you mounted from a file-based secret, the app can read /etc/secrets/api-key.txt.
3) Keep the Secret out of git
If you manage manifests in Git, don’t store the raw secret YAML in the repo unless you encrypt it first.
Simplest safe-ish workflow
- Keep the
kubectl create secret ...command in your deployment docs or script - Apply it separately from the app manifest
If you must store it in git, use one of:
- SOPS
- Sealed Secrets
- External Secrets Operator
But those are extra tooling; for the simplest workable setup, just don’t commit the secret value.
4) Use RBAC to limit access
Make sure only the app’s service account and admins can read the Secret.
Example Role allowing read of just that secret:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: read-api-key-secret
rules:
- apiGroups: [""]
resources: ["secrets"]
resourceNames: ["api-key-secret"]
verbs: ["get"]
Bind it to a service account:
apiVersion: v1
kind: ServiceAccount
metadata:
name: app-sa
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: app-read-secret
subjects:
- kind: ServiceAccount
name: app-sa
roleRef:
kind: Role
name: read-api-key-secret
apiGroup: rbac.authorization.k8s.io
Then use the service account in your pod:
spec:
serviceAccountName: app-sa
5) The minimum “secure enough” cluster setting: encryption at rest
By default, Secret data is only base64-encoded in etcd. Enable encryption at rest for Kubernetes Secrets if possible. That’s a cluster-admin setting.
If you don’t control the cluster, at least confirm your provider supports it and it’s enabled.
Practical recommendation
If you want the simplest workable setup with decent security:
- Create a Kubernetes Secret
- Mount it as an env var or file
- Don’t commit secret values to git
- Use RBAC to limit who can read it
- Enable secret encryption at rest if your cluster supports it
Example end-to-end
kubectl create secret generic api-key-secret \
--from-literal=API_KEY='your-real-api-key'
apiVersion: v1
kind: ServiceAccount
metadata:
name: app-sa
---
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
serviceAccountName: app-sa
containers:
- name: app
image: nginx
env:
- name: API_KEY
valueFrom:
secretKeyRef:
name: api-key-secret
key: API_KEY
If you want, I can also give you the simplest “production-ish” version using SOPS or External Secrets.