Kubernetes_deployments
Chapter 20: Kubernetes Deployments
Section titled “Chapter 20: Kubernetes Deployments”Deployments provide declarative updates for Pods and ReplicaSets. They manage the lifecycle of applications including rolling updates and rollbacks.
What is a Deployment?
Section titled “What is a Deployment?”┌─────────────────────────────────────────────────────────────────────────────┐│ Kubernetes Deployment │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Deployment ││ ┌──────────────────────────────────────────────────────────────────┐ ││ │ │ ││ │ ┌──────────────────────────────────────────────────────────┐ │ ││ │ │ ReplicaSet │ │ ││ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ ││ │ │ │ Pod 1 │ │ Pod 2 │ │ Pod 3 │ │ │ ││ │ │ │ nginx:1.2│ │ nginx:1.2│ │ nginx:1.2│ │ │ ││ │ │ └─────────┘ └─────────┘ └─────────┘ │ │ ││ │ └──────────────────────────────────────────────────────────┘ │ ││ │ │ ││ └──────────────────────────────────────────────────────────────────┘ ││ ││ Benefits: ││ ✓ Declare desired state ││ ✓ Rolling updates ││ ✓ Rollback to previous versions ││ ✓ Scale up/down ││ ✓ Self-healing ││ │└─────────────────────────────────────────────────────────────────────────────┘Creating Deployments
Section titled “Creating Deployments”Using kubectl
Section titled “Using kubectl”# Create deploymentkubectl create deployment nginx --image=nginx:latest
# Create with replicaskubectl create deployment nginx --image=nginx:latest --replicas=3
# Create from YAMLkubectl apply -f deployment.yamlUsing YAML
Section titled “Using YAML”apiVersion: apps/v1kind: Deploymentmetadata: name: nginx-deployment labels: app: nginxspec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 resources: limits: memory: "256Mi" cpu: "500m" requests: memory: "128Mi" cpu: "250m"Deployment Strategies
Section titled “Deployment Strategies”Rolling Update
Section titled “Rolling Update”┌─────────────────────────────────────────────────────────────────────────────┐│ Rolling Update Strategy │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Initial: nginx:1.20 (3 replicas) ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │1.20 │ │1.20 │ │1.20 │ ││ └─────────┘ └─────────┘ └─────────┘ ││ ││ Step 1: Add 1 new pod with nginx:1.21 ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │1.20 │ │1.20 │ │1.20 │ │1.21 │ ← new ││ └─────────┘ └─────────┘ └─────────┘ └─────────┘ ││ ││ Step 2: Remove 1 old pod ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │1.20 │ │1.21 │ │1.21 │ ││ └─────────┘ └─────────┘ └─────────┘ ││ ││ Continue until all pods are nginx:1.21 ││ ││ Default: maxSurge=1, maxUnavailable=0 ││ │└─────────────────────────────────────────────────────────────────────────────┘Recreate
Section titled “Recreate”spec: replicas: 3 strategy: type: Recreate┌─────────────────────────────────────────────────────────────────────────────┐│ Recreate Strategy │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Initial: nginx:1.20 (3 replicas) ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │1.20 │ │1.20 │ │1.20 │ ││ └─────────┘ └─────────┘ └─────────┘ ││ ││ Step 1: Kill all pods ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │ X │ │ X │ │ X │ (downtime) ││ └─────────┘ └─────────┘ └─────────┘ ││ ││ Step 2: Create new pods ││ ┌─────────┐ ┌─────────┐ ┌─────────┐ ││ │1.21 │ │1.21 │ │1.21 │ ││ └─────────┘ └─────────┘ └─────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Managing Deployments
Section titled “Managing Deployments”# Get deploymentskubectl get deployments
# Get deployment detailskubectl describe deployment nginx-deployment
# Scale deploymentkubectl scale deployment nginx-deployment --replicas=5
# Scale based on conditionskubectl autoscale deployment nginx-deployment --min=2 --max=10 --cpu-percent=80
# Update imagekubectl set image deployment/nginx-deployment nginx=nginx:1.21
# View rollout statuskubectl rollout status deployment/nginx-deployment
# View rollout historykubectl rollout history deployment/nginx-deploymentRollback
Section titled “Rollback”# Rollback to previous revisionkubectl rollout undo deployment/nginx-deployment
# Rollback to specific revisionkubectl rollout undo deployment/nginx-deployment --to-revision=2
# Check rollout statuskubectl rollout status deployment/nginx-deploymentDeployment Commands
Section titled “Deployment Commands”# Apply deploymentkubectl apply -f deployment.yaml
# Delete deploymentkubectl delete deployment nginx-deployment
# Get deployment YAMLkubectl get deployment nginx -o yaml
# Watch rollout statuskubectl rollout status -w deployment/nginx-deployment
# Pause deploymentkubectl rollout pause deployment/nginx-deployment
# Resume deploymentkubectl rollout resume deployment/nginx-deployment
# Restart deploymentkubectl rollout restart deployment/nginx-deploymentSummary
Section titled “Summary”In this chapter, you learned:
- What Deployments are
- Creating Deployments
- Deployment strategies (Rolling Update, Recreate)
- Managing Deployments (scale, update)
- Rollback procedures