0
0
Kubernetesdevops~5 mins

High availability cluster setup in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
High availability cluster setup ensures your application keeps running even if some parts fail. It spreads the workload across multiple nodes to avoid downtime and data loss.
When you want your web app to stay online even if one server crashes
When you run a database that must never lose data or stop responding
When you deploy a critical service that users rely on 24/7
When you want to balance traffic evenly across several servers
When you need automatic recovery from hardware or software failures
Config File - deployment-ha.yaml
deployment-ha.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: nginx:1.23.3
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: LoadBalancer

This file creates a Deployment with 3 replicas of the nginx container to ensure multiple copies run simultaneously.

The Service exposes these replicas on port 80 and balances traffic between them.

This setup helps keep the app available even if one pod or node fails.

Commands
This command creates the Deployment and Service defined in the YAML file. It starts 3 pods running the app and exposes them via a load balancer.
Terminal
kubectl apply -f deployment-ha.yaml
Expected OutputExpected
deployment.apps/my-app created service/my-app-service created
This command lists all pods with the label app=my-app to verify that 3 pods are running as expected.
Terminal
kubectl get pods -l app=my-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-6d4cfb7f7f-abcde 1/1 Running 0 15s my-app-6d4cfb7f7f-fghij 1/1 Running 0 15s my-app-6d4cfb7f7f-klmno 1/1 Running 0 15s
-l - Filter pods by label
This command shows the Service details including the external IP and port to access the app.
Terminal
kubectl get svc my-app-service
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-app-service LoadBalancer 10.96.123.45 34.68.123.45 80:31234/TCP 1m
This command provides detailed information about the Deployment, including the number of replicas, pod status, and events.
Terminal
kubectl describe deployment my-app
Expected OutputExpected
Name: my-app Namespace: default CreationTimestamp: Thu, 01 Jun 2024 12:00:00 +0000 Labels: app=my-app Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set my-app-6d4cfb7f7f to 3
Key Concept

If you remember nothing else from this pattern, remember: running multiple replicas of your app across nodes keeps it available even if some parts fail.

Common Mistakes
Setting replicas to 1 in the Deployment
Only one pod means no backup if it crashes, so the app becomes unavailable.
Set replicas to at least 2 or 3 to ensure high availability.
Not exposing the Deployment with a Service
Without a Service, you cannot access the pods easily or balance traffic between them.
Create a Service of type LoadBalancer or ClusterIP to expose the pods.
Using the same node for all pods without spreading
If that node fails, all pods go down together, losing availability.
Kubernetes schedules pods across nodes automatically; avoid node selectors that force all pods on one node.
Summary
Create a Deployment with multiple replicas to run several copies of your app.
Expose the Deployment with a Service to balance traffic and provide a stable access point.
Verify pods and service status with kubectl commands to ensure high availability.