The Nautilus DevOps team is gearing up to deploy applications on a Kubernetes cluster for migration purposes. A team member has been tasked with creating a ReplicaSet outlined below:
- Create a ReplicaSet using
nginximage withlatesttag (ensure to specify asnginx:latest) and name itnginx-replicaset. - Apply labels:
appasnginx_app,typeasfront-end. - Name the container
nginx-container. Ensure the replica count is4.
Solution
Step 1: Generate the ReplicaSet YAML
First, let's generate a base ReplicaSet manifest:
kubectl create replicaset nginx-replicaset \
--image=nginx:latest \
--dry-run=client -o yaml > nginx-replicaset.yaml
Step 2: Edit the YAML to Add Requirements
Open the file and modify it according to the requirements:
nano nginx-replicaset.yaml
Update the file with:
- Replica count: 4
- Labels:
app: nginx_app,type: front-end - Container name:
nginx-container
Here's the complete YAML:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
labels:
app: nginx_app
type: front-end
spec:
replicas: 4
selector:
matchLabels:
app: nginx_app
type: front-end
template:
metadata:
labels:
app: nginx_app
type: front-end
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
Step 3: Apply the ReplicaSet
Create the ReplicaSet in your cluster:
kubectl apply -f nginx-replicaset.yaml
Expected output:
replicaset.apps/nginx-replicaset created
Step 4: Verify the ReplicaSet
Check that the ReplicaSet was created successfully:
kubectl get replicasets
Expected output:
NAME DESIRED CURRENT READY AGE
nginx-replicaset 4 4 4 10s
Step 5: Verify Pods
Check that 4 pods were created:
kubectl get pods
Expected output:
NAME READY STATUS RESTARTS AGE
nginx-replicaset-xxxxx 1/1 Running 0 15s
nginx-replicaset-yyyyy 1/1 Running 0 15s
nginx-replicaset-zzzzz 1/1 Running 0 15s
nginx-replicaset-wwwww 1/1 Running 0 15s
Step 6: Verify Labels
Check that the labels are correctly applied:
kubectl get replicaset nginx-replicaset --show-labels
Expected output:
NAME DESIRED CURRENT READY AGE LABELS
nginx-replicaset 4 4 4 30s app=nginx_app,type=front-end
Step 7: Verify Pod Labels
Check that pods have the correct labels:
kubectl get pods --show-labels
Expected output:
NAME READY STATUS RESTARTS AGE LABELS
nginx-replicaset-xxxxx 1/1 Running 0 20s app=nginx_app,type=front-end
nginx-replicaset-yyyyy 1/1 Running 0 20s app=nginx_app,type=front-end
nginx-replicaset-zzzzz 1/1 Running 0 20s app=nginx_app,type=front-end
nginx-replicaset-wwwww 1/1 Running 0 20s app=nginx_app,type=front-end
Step 8: Detailed Verification
Get detailed information about the ReplicaSet:
kubectl describe replicaset nginx-replicaset
Alternative Methods
Method 1: Using kubectl create Directly
Create the ReplicaSet with a single command:
kubectl create replicaset nginx-replicaset \
--image=nginx:latest \
--replicas=4 \
--labels="app=nginx_app,type=front-end" \
--dry-run=client -o yaml > nginx-replicaset.yaml
Then apply it:
kubectl apply -f nginx-replicaset.yaml
Method 2: Complete YAML from Scratch
Create a YAML file named nginx-replicaset.yaml with the complete specification:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
labels:
app: nginx_app
type: front-end
spec:
replicas: 4
selector:
matchLabels:
app: nginx_app
type: front-end
template:
metadata:
labels:
app: nginx_app
type: front-end
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
Apply it:
kubectl apply -f nginx-replicaset.yaml
Method 3: Using kubectl run (Not Recommended for ReplicaSets)
kubectl run nginx-replicaset \
--image=nginx:latest \
--labels="app=nginx_app,type=front-end" \
--port=80 \
--replicas=4 \
--restart=Always \
--dry-run=client -o yaml > nginx-replicaset.yaml
Complete Verification Commands
1. Check ReplicaSet Status
# Basic status
kubectl get replicaset nginx-replicaset
# Detailed with labels
kubectl get replicaset nginx-replicaset --show-labels
# Wide output
kubectl get replicaset nginx-replicaset -o wide
# Custom columns
kubectl get replicaset nginx-replicaset -o custom-columns=NAME:.metadata.name,DESIRED:.spec.replicas,READY:.status.readyReplicas,AGE:.metadata.creationTimestamp
2. Check Pods
# List all pods
kubectl get pods
# Filter pods by label
kubectl get pods -l app=nginx_app
# Show pod labels
kubectl get pods --show-labels
# Get pod details
kubectl describe pods -l app=nginx_app
3. Check ReplicaSet Details
# Describe ReplicaSet
kubectl describe replicaset nginx-replicaset
# Get YAML output
kubectl get replicaset nginx-replicaset -o yaml
# Get JSON output
kubectl get replicaset nginx-replicaset -o json
4. Verify Image Version
# Check image of pods
kubectl get pods -l app=nginx_app -o jsonpath='{.items[*].spec.containers[0].image}'
# Check image via ReplicaSet
kubectl get replicaset nginx-replicaset -o jsonpath='{.spec.template.spec.containers[0].image}'
5. Check Events
# Check events related to ReplicaSet
kubectl get events --field-selector involvedObject.name=nginx-replicaset
# Check events related to pods
kubectl get events --field-selector involvedObject.kind=Pod | grep nginx-replicaset
Understanding ReplicaSets
What is a ReplicaSet?
A ReplicaSet ensures that a specified number of pod replicas are running at any given time. It replaces the older ReplicationController with more advanced label selectors.
┌─────────────────────────────────────────────┐
│ ReplicaSet │
│ (nginx-replicaset) │
│ Desired: 4 pods │
│ Labels: app=nginx_app │
│ type=front-end │
└──────────────────┬──────────────────────────┘
│
┌──────────┼──────────┐
│ │ │
▼ ▼ ▼
┌───────┐ ┌───────┐ ┌───────┐
│Pod 1 │ │Pod 2 │ │Pod 3 │
│Running│ │Running│ │Running│
└───────┘ └───────┘ └───────┘
│
▼
┌───────┐
│Pod 4 │
│Running│
└───────┘
ReplicaSet vs Deployment
| Feature | ReplicaSet | Deployment |
|---|---|---|
| Purpose | Maintain pod count | Manage application lifecycle |
| Rolling Updates | ❌ No | ✅ Yes |
| Rollbacks | ❌ No | ✅ Yes |
| Scaling | ✅ Yes | ✅ Yes |
| Self-healing | ✅ Yes | ✅ Yes |
| Use Case | When you need simple replication | Most production applications |
Why Use a ReplicaSet?
- High Availability: Ensures your application always has the desired number of replicas
- Self-Healing: Automatically replaces failed pods
- Scalability: Easy to scale up or down
- Load Distribution: Distributes traffic across multiple pods
Advanced Operations
1. Scaling the ReplicaSet
# Scale to 6 replicas
kubectl scale replicaset nginx-replicaset --replicas=6
# Verify
kubectl get replicaset nginx-replicaset
# Check pods
kubectl get pods -l app=nginx_app
2. Update Image
# Update the ReplicaSet to use a different image
kubectl set image replicaset nginx-replicaset nginx-container=nginx:1.21
# Check the updated image
kubectl get replicaset nginx-replicaset -o jsonpath='{.spec.template.spec.containers[0].image}'
3. Edit ReplicaSet
# Edit the ReplicaSet directly
kubectl edit replicaset nginx-replicaset
# After editing, the ReplicaSet will start new pods
4. Delete ReplicaSet
# Delete the ReplicaSet (and its pods)
kubectl delete replicaset nginx-replicaset
# Delete using YAML
kubectl delete -f nginx-replicaset.yaml
# Delete only the ReplicaSet, not the pods
kubectl delete replicaset nginx-replicaset --cascade=false
Troubleshooting
Issue 1: Pods Not Creating
Error: ReplicaSet shows 0 pods
Solutions:
# Check ReplicaSet events
kubectl describe replicaset nginx-replicaset
# Check if there's a label selector mismatch
kubectl get replicaset nginx-replicaset -o yaml | grep -A 5 selector
Issue 2: ImagePullBackOff
Error: Pods stuck in ImagePullBackOff
Solutions:
# Check pod details
kubectl describe pod -l app=nginx_app
# Verify image name
kubectl get replicaset nginx-replicaset -o jsonpath='{.spec.template.spec.containers[0].image}'
# Update to correct image
kubectl set image replicaset nginx-replicaset nginx-container=nginx:latest
Issue 3: Pods Not Running
Error: Pods in Pending state
Solutions:
# Check node resources
kubectl describe nodes
# Check pod events
kubectl describe pod -l app=nginx_app
# Check if there are resource constraints
kubectl get replicaset nginx-replicaset -o yaml | grep -A 5 resources
Issue 4: Wrong Container Name
Error: Container name doesn't match
Solutions:
# Check container name
kubectl get replicaset nginx-replicaset -o jsonpath='{.spec.template.spec.containers[0].name}'
# Edit to correct name
kubectl edit replicaset nginx-replicaset
Best Practices
✅ DO's
- Use Deployments Instead of ReplicaSets
# Unless you have a specific reason to use ReplicaSet directly
# Use Deployments for most use cases
- Properly Define Selectors
selector:
matchLabels:
app: nginx_app
type: front-end
- Set Resource Limits
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
- Add Health Checks
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 30
periodSeconds: 10
- Use Meaningful Labels
labels:
app: nginx_app
type: front-end
environment: production
version: "1.0"
- Document Your ReplicaSet
metadata:
annotations:
description: "Nginx web server for front-end applications"
owner: "devops-team"
❌ DON'Ts
-
Don't Use ReplicaSets for Stateful Applications
- Use StatefulSets instead
- ReplicaSets are for stateless applications
-
Don't Create ReplicaSets Without Selectors
- Selectors are required
- They determine which pods the ReplicaSet manages
Don't Directly Modify Pods Managed by ReplicaSets
# BAD - Pod will be recreated
kubectl delete pod nginx-replicaset-xxxxx
# GOOD - Scale the ReplicaSet
kubectl scale replicaset nginx-replicaset --replicas=3
-
Don't Use
:latestin Production
# BAD - Unpredictable
image: nginx:latest
# GOOD - Specific version
image: nginx:1.21.6
Lab Completion Summary
✅ Task Requirements Checklist
- [x] ReplicaSet created:
nginx-replicaset - [x] Image:
nginx:latest - [x] Labels:
app: nginx_app,type: front-end - [x] Container name:
nginx-container - [x] Replica count: 4
- [x] All pods running: 4/4 ready
Final Verification Commands
echo "=== ReplicaSet Status ==="
kubectl get replicaset nginx-replicaset
echo -e "\n=== Pod Status ==="
kubectl get pods -l app=nginx_app
echo -e "\n=== Labels ==="
kubectl get replicaset nginx-replicaset --show-labels
echo -e "\n=== Container Image ==="
kubectl get replicaset nginx-replicaset -o jsonpath='{.spec.template.spec.containers[0].image}'
echo -e "\n\n=== Container Name ==="
kubectl get replicaset nginx-replicaset -o jsonpath='{.spec.template.spec.containers[0].name}'
echo -e "\n\n=== Replica Count ==="
kubectl get replicaset nginx-replicaset -o jsonpath='{.spec.replicas}'
echo -e "\n\n=== All Resources ==="
kubectl get all -l app=nginx_app
Expected output:
=== ReplicaSet Status ===
NAME DESIRED CURRENT READY AGE
nginx-replicaset 4 4 4 2m
=== Pod Status ===
NAME READY STATUS RESTARTS AGE
nginx-replicaset-xxxxx 1/1 Running 0 2m
nginx-replicaset-yyyyy 1/1 Running 0 2m
nginx-replicaset-zzzzz 1/1 Running 0 2m
nginx-replicaset-wwwww 1/1 Running 0 2m
=== Labels ===
NAME DESIRED CURRENT READY AGE LABELS
nginx-replicaset 4 4 4 2m app=nginx_app,type=front-end
=== Container Image ===
nginx:latest
=== Container Name ===
nginx-container
=== Replica Count ===
4
Top comments (0)