Sensitive file projection
Each Secret key becomes a file in a mounted directory, making file-based credentials easy to consume.
Mount Secrets as read-only files inside Pods so apps can consume credentials more safely.
k8s/labs/storage/secret-volume.yaml — Pod mounting a Secret as a read-only volumeEach Secret key becomes a file in a mounted directory, making file-based credentials easy to consume.
Secret volumes are commonly mounted read-only so apps can read but not accidentally alter credential files.
Secrets are for confidential values, while ConfigMaps are for ordinary non-sensitive configuration.
Store sensitive values such as passwords, usernames, tokens, or certificates.
The Pod references the Secret in the volumes section.
Kubernetes projects each Secret key into the mount path as a file.
Use RBAC, narrow mount paths, and read-only access to keep credential exposure controlled.
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
data:
username: c3VwZXJ1c2Vy
password: cGFzc3dvcmQ=
apiVersion: apps/v1
kind: Deployment
metadata:
name: secret-deployment
spec:
template:
spec:
containers:
- name: secret-container
image: nginx
volumeMounts:
- name: secret-volume
mountPath: /etc/secrets
readOnly: true
volumes:
- name: secret-volume
secret:
secretName: db-credentials
| Topic | ConfigMap Volume | Secret Volume |
|---|---|---|
| Data type | Non-sensitive config | Sensitive credentials or keys |
| Main concern | Configuration management | Confidentiality and controlled exposure |
| Typical fit | Settings and templates | Passwords, certs, tokens |
Mount usernames and passwords into apps that expect file-based runtime configuration.
Provide TLS keys and certs to services or reverse proxies through mounted files.
Keep sensitive values out of container images and out of environment-specific image builds.