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 

# 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://
curl http://
Enter fullscreen mode

Exit fullscreen mode

r6

šŸ‘‰ Each request will be routed to different pods. Test this by running:

while true; do curl http://; 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 

# 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



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *