Kubernetes
ConfigMap & Secret
Inject configuration and sensitive data into containers.
By EZ4Code Team
configmapsecretconfiguration
Code
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: api-config
data:
LOG_LEVEL: "info"
config.yaml: |
server:
port: 8080
timeout: 30s
---
# secret.yaml (values must be base64-encoded)
apiVersion: v1
kind: Secret
metadata:
name: api-secret
type: Opaque
data:
DB_PASSWORD: c2VjcmV0 # echo -n secret | base64
---
# Pod consuming them
spec:
containers:
- name: api
envFrom:
- configMapRef: { name: api-config }
- secretRef: { name: api-secret }
volumeMounts:
- name: cfg
mountPath: /etc/appExplanation
ConfigMaps store non-sensitive configuration as key-value pairs or files, while Secrets hold base64-encoded sensitive data. envFrom injects all keys as environment variables, and volumes can mount ConfigMap entries as files. Secrets are not encrypted by default; enable encryption at rest in etcd for production.
More Kubernetes Snippets
Pod Manifest
Define a standalone Pod with a container, env, and probe.
Deployment & Rollout
Manage a replicated, rolling-updated workload.
Service Types
Expose Pods via ClusterIP, NodePort, and LoadBalancer services.
Ingress Routing
Route external HTTP traffic to services by host and path.
Labels & Selectors
Tag resources and query them with selectors.
kubectl Basics
Common kubectl commands for everyday operations.