Cloud Native 7 min read

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.

Ray's Galactic Tech
Ray's Galactic Tech
Ray's Galactic Tech
Mastering Kubernetes Deployments for High‑Availability Online Services

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.yaml

Full 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/hostname

Key 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-availability

Production 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DeploymentKubernetesHPARolling Update
Ray's Galactic Tech
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.