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.
🔹 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
Deploy the ReplicaSet:
kubectl create -f replicaset-demo.yml
Step-02-02: List ReplicaSets
kubectl get replicaset
kubectl get rs
Step-02-03: Describe ReplicaSet
kubectl describe rs my-helloworld-rs
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
🔹 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
List the service:
kubectl get svc
Access the application using the External IP:
http://<External-IP>
curl http://<External-IP>
👉 Each request will be routed to different pods. Test this by running:
while true; do curl http://<EXTERNAL-IP>; sleep 1; done
🔹 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
👉 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
Apply changes:
kubectl apply -f replicaset-demo.yml
Verify new pods:
kubectl get pods -o wide
Option 2: Imperative Scaling
# Scale out
kubectl scale --replicas=7 replicaset my-helloworld-rs
# Scale in
kubectl scale --replicas=2 replicaset my-helloworld-rs
Option 3: Edit ReplicaSet
kubectl edit replicaset my-helloworld-rs
Change:
replicas: 6
Save & exit, then check pods:
kubectl get pods
🔹 Step-06: Clean-Up
Step-06-01: Delete ReplicaSet
kubectl delete rs my-helloworld-rs
kubectl get rs
Step-06-02: Delete Service
kubectl delete svc my-helloworld-rs-service
kubectl get svc
🎯 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)