Mastering Kubernetes Deployments for High‑Availability Online Services
This guide explains why Deployments are essential in Kubernetes, walks through a full production‑grade YAML, and covers replica control, rolling updates, health probes, anti‑affinity, scaling, and rollback best practices for resilient cloud‑native applications.
Kubernetes Deployments are a core controller for online services, providing high availability, self‑healing, rolling updates, quick rollback, automatic scaling (HPA), and declarative management compatible with GitOps.
Typical Use Cases
Online web services and APIs
E‑commerce, payment, IM, content platforms
Frequent deployments requiring smooth upgrades
Cloud‑native apps with elastic scaling needs
Generating a Deployment YAML
kubectl create deployment nginx \
--image=nginx:1.20 \
--replicas=3 \
--port=80 \
--dry-run=client -o yaml > nginx-deployment.yamlFull Production‑Grade Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-high-availability
namespace: default
labels:
app: nginx
environment: production
spec:
replicas: 3
selector:
matchLabels:
app: nginx
tier: web-frontend
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
minReadySeconds: 30
template:
metadata:
labels:
app: nginx
tier: web-frontend
version: v1.2.0
spec:
restartPolicy: Always
containers:
- name: nginx-container
image: nginx:1.20-alpine
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
env:
- name: PROJECT_ENV
value: "production"
- name: DEPLOYMENT_VERSION
value: "v1.2.0"
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values: ["nginx"]
topologyKey: kubernetes.io/hostnameKey Configuration Details
replicas : Set to 3 to ensure service continuity even if a node fails.
selector vs. template labels : Selector must be stable (e.g., app: nginx) and match template labels; avoid versioned labels in selector to prevent crashes during upgrades.
RollingUpdate strategy : maxSurge: 1 allows one extra pod during update; maxUnavailable: 0 ensures zero downtime.
Health checks : readinessProbe controls traffic admission; livenessProbe triggers container restarts on failure.
Pod anti‑affinity : Distributes pods across nodes to improve fault tolerance.
Operational Commands
# Apply the deployment
kubectl apply -f nginx-deployment.yaml
# View deployment status
kubectl get deploy nginx-high-availability
kubectl describe deploy nginx-high-availability
# List pods
kubectl get pods -l app=nginx
kubectl get pods -l app=nginx -o wide
# Stream logs
kubectl logs -l app=nginx --tail=10 --prefix
# Manual scaling
kubectl scale deploy nginx-high-availability --replicas=5
# Horizontal Pod Autoscaler (HPA)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-high-availability
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
# Rollout history and rollback
kubectl rollout history deploy nginx-high-availability
kubectl rollout undo deploy nginx-high-availabilityProduction Best Practices
Use stable selector labels only (e.g., app, tier).
Enable readinessProbe to prevent traffic during startup.
Define resource requests and limits to avoid OOM kills.
Apply anti‑affinity rules to spread pods across nodes.
Set maxUnavailable: 0 for zero‑downtime rolling updates.
Combine HPA, VPA, and Cluster Autoscaler for a complete elastic system.
Conclusion
By mastering Deployments, you can build self‑healing, roll‑backable, upgradable, auto‑scalable, highly available, and fully automated Kubernetes‑based online services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ray's Galactic Tech
Practice together, never alone. We cover programming languages, development tools, learning methods, and pitfall notes. We simplify complex topics, guiding you from beginner to advanced. Weekly practical content—let's grow together!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
