4.03 Configure Env Variables,ConfigMaps & Secrets in Applications
Kubernetes lets you pass configuration into containers using:
- Environment variables
- ConfigMaps
- Secrets
This keeps configuration separate from container images and makes apps easier to manage and update.
π― Why Use Environment Configuration
Applications often need runtime configuration such as:
- App mode (dev / prod)
- Feature flags
- Colors / themes
- DB hostnames
- Ports
- External service URLs
Hard-coding these inside images is bad practice.
Success
Kubernetes supports external configuration using env, ConfigMaps, and Secrets.
π§ͺ Basic Environment Variable in a Pod
Equivalent Docker command:
Kubernetes Pod YAML:
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: app
image: simple-webapp-color
ports:
- containerPort: 8080
env:
- name: APP_COLOR
value: pink
Note
env is a list β each variable is a separate item.
π§© Environment Value Sources (3 Ways)
Kubernetes supports three env value types:
- Plain key/value
- ConfigMap
- Secret
ποΈ What Is a ConfigMap
A ConfigMap stores configuration as keyβvalue pairs in Kubernetes.
Benefits:
- Central config storage
- Reusable across Pods
- No image rebuild required
- Easy updates
Abstract
ConfigMap = configuration outside the container image.
βοΈ Create ConfigMap β Imperative Method
From Literal Values
From File
π Create ConfigMap β Declarative Method
ConfigMap YAML:
Create:
π View ConfigMaps
π Inject ConfigMap into Pod β All Keys
Load all keys as environment variables:
apiVersion: v1
kind: Pod
metadata:
name: webapp
spec:
containers:
- name: app
image: simple-webapp-color
envFrom:
- configMapRef:
name: app-config
Result inside container:
Tip
envFrom loads all keys from the ConfigMap.
π― Inject Single ConfigMap Key
Note
Use this when you only need specific keys.
πΎ Inject ConfigMap as Volume
ConfigMap can also be mounted as files.
volumes:
- name: config-vol
configMap:
name: app-config
containers:
- name: app
image: simple-webapp-color
volumeMounts:
- name: config-vol
mountPath: /etc/config
Result inside container:
β οΈ When to Use Secrets Instead
Use Secrets instead of ConfigMaps for:
- Passwords
- Tokens
- Keys
- Certificates
Warning
ConfigMaps are NOT encrypted β Secrets are.
β Exam-Style Checks
Question
Where is ConfigMap data stored?
In Kubernetes API (etcd).
Question
Does ConfigMap require Pod restart after change?
Usually yes (unless app reloads dynamically).
Question
Can ConfigMap be used as files?
Yes β via volume mount.
β Quick Summary
Summary
- Use env for simple variables
- Use ConfigMaps for shared configuration
- Create via kubectl or YAML
- Inject using env, envFrom, or volumes
- envFrom loads all keys
- configMapKeyRef loads one key
- Use Secrets for sensitive data
Kubernetes Secrets
Kubernetes Secrets are used to store and manage sensitive data such as:
- Passwords
- API keys
- Tokens
- Database credentials
Secrets are similar to ConfigMaps β but meant for confidential values.
π― Why Secrets Are Needed
Hard-coding credentials inside application code or Pod YAML is unsafe.
Example (β bad practice):
Instead β move sensitive values into Kubernetes Secrets and inject them securely.
Warning
Do NOT store passwords in Pod specs or container images.
π§ Secrets vs ConfigMaps
| Feature | ConfigMap | Secret |
|---|---|---|
| Purpose | Non-sensitive config | Sensitive data |
| Storage | Plain text | Base64 encoded |
| Use case | App settings | Credentials, keys |
Note
Secrets are base64-encoded β not encrypted by default.
π Secret Workflow (2 Steps)
Secrets can be injected as:
- Environment variables
- Single env values
- Mounted volume files
βοΈ Create Secrets β Imperative Method
From Literal Values
kubectl create secret generic app-secret \
--from-literal=DB_Host=mysql \
--from-literal=DB_User=root \
--from-literal=DB_Password=passwd
From File
Tip
Good when many secret keys exist.
π¦ Create Secrets β Declarative Method
Secrets YAML requires base64 encoded values.
Encode Values
Secret YAML
apiVersion: v1
kind: Secret
metadata:
name: app-secret
type: Opaque
data:
DB_Host: bXlzcWw=
DB_User: cm9vdA==
DB_Password: cGFzc3dk
Apply:
Success
Declarative method is preferred for GitOps.
π View Secrets
List
Describe (values hidden)
View YAML (encoded values visible)
π Decode Secret Value
Note
Base64 encoding is reversible β enable etcd encryption for real security.
π Inject Secrets into Pods β All as ENV
apiVersion: v1
kind: Pod
metadata:
name: web-pod
spec:
containers:
- name: app
image: myapp
envFrom:
- secretRef:
name: app-secret
Result inside container:
π― Inject Single Secret Key
Tip
Use this when you only need one value.
π Mount Secret as Volume
Each secret key becomes a file.
spec:
containers:
- name: app
image: myapp
volumeMounts:
- name: secret-vol
mountPath: /opt/secrets
volumes:
- name: secret-vol
secret:
secretName: app-secret
Inside container:
π§ Secret Injection Diagram
Secret Object
β
βββ envFrom β all keys β env vars
β
βββ secretKeyRef β single key β env var
β
βββ volume mount β keys β files
β οΈ Important Security Notes
Warning
Base64 is encoding β not encryption.
Warning
Enable etcd encryption for production clusters.
Tip
Use RBAC to restrict secret access.
Tip
Avoid committing Secret YAML with real values to Git.
π§ͺ Exam-Style Checks
Question
Are Secrets encrypted by default?
No β only base64 encoded.
Question
How many steps to use Secrets?
Create β Inject.
Question
Can Secrets be used as files?
Yes β via volume mount.
Question
Which field injects all keys as env vars?
envFrom + secretRef
β Quick Summary
Summary
- Secrets store sensitive data
- Similar to ConfigMaps but for credentials
- Values stored base64 encoded
- Create via imperative or YAML
- Inject using envFrom, secretKeyRef, or volumes
- Describe hides values
- YAML shows encoded values
- Use RBAC + etcd encryption in production