DEV Community

Cover image for Part-86: 🚀Deploy ReplicaSets and Practice High Availability & Scalability in Google Kubernetes Engine (GCP)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-86: 🚀Deploy ReplicaSets and Practice High Availability & Scalability in Google Kubernetes Engine (GCP)

Kubernetes ReplicaSets are the backbone of ensuring your application runs reliably, stays available, and scales seamlessly in production. In this guide, we’ll deploy a ReplicaSet on Google Kubernetes Engine (GKE) and test its High Availability (HA) and Scalability features.

r1


🔹 Step-01: Introduction to ReplicaSets

A ReplicaSet is a Kubernetes object that ensures a specified number of pod replicas are running at any given time.

  • ✅ If a pod crashes, ReplicaSet automatically recreates it.
  • ✅ Helps maintain High Availability.
  • ✅ Supports Load Balancing when combined with a Service.
  • ✅ Allows Scaling up or down easily when traffic changes.

🔹 Step-02: Create ReplicaSet

Step-02-01: Create ReplicaSet

Create a file named replicaset-demo.yml:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-helloworld-rs
  labels:
    app: my-helloworld
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-helloworld
  template:
    metadata:
      labels:
        app: my-helloworld
    spec:
      containers:
      - name: my-helloworld-app
        image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
        ports: 
          - containerPort: 8080
Enter fullscreen mode Exit fullscreen mode

Deploy the ReplicaSet:

kubectl create -f replicaset-demo.yml
Enter fullscreen mode Exit fullscreen mode

Step-02-02: List ReplicaSets

kubectl get replicaset
kubectl get rs
Enter fullscreen mode Exit fullscreen mode

r2


Step-02-03: Describe ReplicaSet

kubectl describe rs my-helloworld-rs
Enter fullscreen mode Exit fullscreen mode

r3


Step-02-04: List Pods

# List pods
kubectl get pods

# Get pod details
kubectl describe pod <pod-name>

# List pods with Pod IP & Node
kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

r4


🔹 Step-03: Expose ReplicaSet as a Service

Expose the ReplicaSet to the internet with a LoadBalancer Service:

kubectl expose rs my-helloworld-rs \
  --type=LoadBalancer \
  --port=80 \
  --target-port=8080 \
  --name=my-helloworld-rs-service
Enter fullscreen mode Exit fullscreen mode

List the service:

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

r5

Access the application using the External IP:

http://<External-IP>
curl http://<External-IP>
Enter fullscreen mode Exit fullscreen mode

r6

👉 Each request will be routed to different pods. Test this by running:

while true; do curl http://<EXTERNAL-IP>; sleep 1; done
Enter fullscreen mode Exit fullscreen mode

r7


🔹 Step-04: Test ReplicaSet Reliability (High Availability)

ReplicaSet ensures pods are always available. Let’s test it:

# List pods
kubectl get pods

# Delete one pod
kubectl delete pod <pod-name>

# Verify a new pod is created automatically
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

r8

👉 Notice that the ReplicaSet auto-created a new pod to maintain the replica count.


🔹 Step-05: Test ReplicaSet Scalability

ReplicaSets make scaling super easy.

Option 1: Update YAML

Change replicas from 3 → 5 in replicaset-demo.yml:

spec:
  replicas: 5
Enter fullscreen mode Exit fullscreen mode

Apply changes:

kubectl apply -f replicaset-demo.yml
Enter fullscreen mode Exit fullscreen mode

Verify new pods:

kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

r9


Option 2: Imperative Scaling

# Scale out
kubectl scale --replicas=7 replicaset my-helloworld-rs

# Scale in
kubectl scale --replicas=2 replicaset my-helloworld-rs
Enter fullscreen mode Exit fullscreen mode

r10


Option 3: Edit ReplicaSet

kubectl edit replicaset my-helloworld-rs
Enter fullscreen mode Exit fullscreen mode

Change:

replicas: 6
Enter fullscreen mode Exit fullscreen mode

Save & exit, then check pods:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

r11


🔹 Step-06: Clean-Up

Step-06-01: Delete ReplicaSet

kubectl delete rs my-helloworld-rs
kubectl get rs
Enter fullscreen mode Exit fullscreen mode

Step-06-02: Delete Service

kubectl delete svc my-helloworld-rs-service
kubectl get svc
Enter fullscreen mode Exit fullscreen mode

r12


🎯 Summary

In this guide, we:

  • Deployed a ReplicaSet with 3 pods.
  • Exposed it to the internet using a LoadBalancer Service.
  • Tested High Availability by deleting pods.
  • Practiced Scalability by scaling pods up and down.

👉 ReplicaSets are the foundation of reliable, scalable Kubernetes apps. In production, they are usually managed by Deployments, but practicing ReplicaSets directly gives a strong foundation. 🚀


🌟 Thanks for reading! If this post added value, a like ❤️, follow, or share would encourage me to keep creating more content.


— Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | 🔐 Security | ⚡ Automation
📌 Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)