Practical Configuration
Configuration Management in Kubernetes
Configuration Methods
graph TD
A[Kubernetes Configuration] --> B[ConfigMaps]
A --> C[Secrets]
A --> D[Environment Variables]
A --> E[Volume Mounts]
Configuration Types
Type |
Use Case |
Security Level |
ConfigMaps |
Non-sensitive configuration |
Low |
Secrets |
Sensitive data |
High |
Environment Variables |
Simple key-value pairs |
Medium |
Volume Mounts |
File-based configurations |
High |
ConfigMap Configuration
Creating a ConfigMap
## Create ConfigMap from literal values
kubectl create configmap app-config \
--from-literal=DATABASE_URL=mysql://localhost \
--from-literal=LOG_LEVEL=debug
YAML Configuration
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DATABASE_URL: mysql://localhost
LOG_LEVEL: debug
Secret Management
Creating Secrets
## Create secret from literal values
kubectl create secret generic db-credentials \
--from-literal=username=admin \
--from-literal=password=secure-password
Secret YAML Configuration
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
stringData:
username: admin
password: secure-password
Deployment with Configurations
Sample Deployment Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
template:
spec:
containers:
- name: web-container
image: myapp:latest
env:
- name: DATABASE_URL
valueFrom:
configMapKeyRef:
name: app-config
key: DATABASE_URL
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: db-credentials
key: username
Advanced Configuration Techniques
Environment Variable Injection
graph LR
A[ConfigMap/Secret] --> B[Environment Variables]
B --> C[Container Runtime]
Volume-Based Configuration
volumes:
- name: config-volume
configMap:
name: app-config
containers:
- name: app
volumeMounts:
- name: config-volume
mountPath: /etc/config
Best Practices
- Never commit secrets to version control
- Use least privilege principle
- Rotate credentials regularly
- Use external secret management systems
- Encrypt sensitive data
Validation and Debugging
## Verify ConfigMap
kubectl get configmaps
## Describe ConfigMap
kubectl describe configmap app-config
## Check secret
kubectl get secrets
## Verify deployment configuration
kubectl describe deployment web-app
LabEx Learning Path
LabEx provides interactive labs to master Kubernetes configuration techniques, offering hands-on experience with real-world scenarios and best practices.