Version note: examples use the Kubernetes v1.35 documentation snapshot, not necessarily the newest release. Check current support and component compatibility. These commands were not tested on a live cluster.
What this guide covers
A ConfigMap stores non-secret application settings. A Secret stores sensitive values, but its base64 representation is not encryption. Access permissions and storage encryption still matter.
Before you start
Create config-lab in a test cluster. Do not place real passwords in Git. The example uses an immutable ConfigMap, meaning a new configuration needs a new resource name.
Step 1: Create and read a ConfigMap
Save this as config.yaml and apply it with kubectl apply -f config.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config-v1
namespace: config-lab
immutable: true
data:
LOG_LEVEL: info
app.conf: |
feature_banner=enabled
request_timeout_seconds=5
---
apiVersion: v1
kind: Pod
metadata:
name: config-reader
namespace: config-lab
spec:
automountServiceAccountToken: false
containers:
- name: reader
image: busybox:1.37.0
command: ["sh", "-c", "sleep 3600"]
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config-v1
key: LOG_LEVEL
volumeMounts:
- name: configuration
mountPath: /etc/demo
readOnly: true
resources:
requests:
cpu: 10m
memory: 16Mi
limits:
memory: 32Mi
volumes:
- name: configuration
configMap:
name: app-config-v1
items:
- key: app.conf
path: app.conf
The example reads settings through both environment variables and mounted files. Inspect them:
kubectl -n config-lab exec config-reader -- printenv LOG_LEVEL
kubectl -n config-lab exec config-reader -- cat /etc/demo/app.conf
kubectl -n config-lab describe pod config-reader
Step 2: Create a Secret from a protected file
Create private/password.txt using a secure editor or secret manager, restrict its permissions, and keep the directory out of Git. Then run:
kubectl -n config-lab create secret generic app-credentials \
--from-file=password=./private/password.txt
This creates the Secret; it does not mount it into the existing Pod. To use it, configure the application Pod with a secretKeyRef or a read-only Secret volume. Do not print the Secret while checking it.
Step 3: Apply configuration changes deliberately
For this immutable ConfigMap, create a new versioned ConfigMap and update the Pod template to reference it. Environment values are loaded when the container starts. Mounted-file updates and application reload behavior are separate; check both rather than assuming an edit changed a running process.
Check the result
Confirm the process reads the expected non-secret setting. Check that only authorized accounts can read the Secret. Delete test secrets securely when finished.
Conversation
0 commentsNo comments yet. Start the conversation below.