Create Deployments for zero-downtime rollouts, scale replicas, roll back versions, and use ConfigMaps for configuration.
A Deployment manages a ReplicaSet, which manages Pods. You tell the Deployment what you want (image, replicas) and Kubernetes makes it happen.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web
image: nginx:1.24
ports:
- containerPort: 80
resources:
requests:
memory: '64Mi'
cpu: '100m'
limits:
memory: '128Mi'
cpu: '200m'kubectl apply -f deployment.yaml
kubectl get deployments
kubectl get pods # should show 3 pods
# Scale
kubectl scale deployment web-app --replicas=5
# Update image (triggers rolling update)
kubectl set image deployment/web-app web=nginx:1.25
# Watch the rollout
kubectl rollout status deployment/web-app
# Roll back
kubectl rollout undo deployment/web-app
kubectl rollout history deployment/web-app# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_ENV: production
LOG_LEVEL: info
# Use in a Deployment
env:
- name: APP_ENV
valueFrom:
configMapKeyRef:
name: app-config
key: APP_ENVkubectl rollout undo reverts to the previous version instantly.