A Kubernetes Secret stores application data such as credentials and configuration properties. It can be referenced by a workload manifest, for example to supply database connection settings to a container rather than placing them directly in the application image. section 3.3.3
A Secret is not inherently secret because its data is usually Base64-encoded, and Base64 can be decoded directly with base64 -d. Base64 is encoding, not encryption. The protection comes from the process used to manage the Secret; by itself, a Secret is like a ConfigMap. section 3.3.3
Use Secrets to keep sensitive values out of container image layers:
- Do not put passwords or keys in a Dockerfile or image, even temporarily. Removing them in a later layer leaves the original layer accessible. section 3.2.1
- Reference Secret keys from a Deployment to populate container environment variables, such as PostgreSQL host and port settings. section 3.3.3
Database settings in a Deployment
The appointments container reads POSTGRES_HOST and POSTGRES_PORT through secretKeyRef entries pointing at db-appointments-app. This keeps those values outside the Deployment's literal environment-value fields while making them available to the running container. section 3.3.3
Listing 3.1 Deployment configuration for a cloud native application.
apiVersion: apps/v1
kind: Deployment
metadata:
name: appointments #A
labels:
app: appointments
spec:
selector:
matchLabels:
app: appointments
template:
metadata:
labels:
app: appointments
spec:
containers:
- name: appointments #B
image: ko.local/appointments:0.0.1-SNAPSHOT #C
env: #D
- name: POSTGRES_HOST
valueFrom:
secretKeyRef:
name: db-appointments-app
key: host
- name: POSTGRES_PORT
valueFrom:
secretKeyRef:
name: db-appointments-app
key: port
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: db-appointments-app
key: dbname
- name: POSTGRES_USERNAME
valueFrom:
secretKeyRef:
name: db-appointments-app
key: username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: db-appointments-app
key: password
...
Inspect the Deployment manifest to see Secret keys mapped into container environment variables with secretKeyRef. section 3.3.3






