Kubernetes Day 3 - pranavkumarpk01/MD-DevOps GitHub Wiki
βοΈ Kubernetes Deployment, ReplicaSet & ReplicationController β Full Guide
In Kubernetes, managing Pods manually is not scalable. Controllers like Deployment, ReplicaSet, and ReplicationController ensure that the desired state of Pods is maintained automaticallyβeven if Pods crash or nodes fail.
π Summary of Concepts
Controller | Purpose | Current Status |
---|---|---|
ReplicationController | Ensures specified number of identical Pods | Deprecated (use Deployment) |
ReplicaSet | Newer version of ReplicationController | Used by Deployment internally |
Deployment | Manages ReplicaSet and enables rolling updates | Most commonly used |
π ReplicationController
β Use Case
Ensures a specified number of Pod replicas are running at all times.
π YAML Example
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-rc
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
π§ Explanation
Field | Meaning |
---|---|
replicas: 2 |
Runs two copies of the Pod |
selector |
Matches Pods that should be managed |
template |
Pod template to create new Pods |
image: nginx:1.21 |
Container image to use |
containerPort |
Port inside the container |
π ReplicaSet
ReplicaSet is the next-gen ReplicationController, supporting set-based selectors (e.g., in
, notin
).
π YAML Example
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
π§ Explanation
Field | Meaning |
---|---|
replicas: 3 |
Desired number of Pods |
matchLabels |
Ensures only Pods with app: nginx are managed |
template |
Pod spec to create when needed |
π Deployment (Most Recommended)
A Deployment manages ReplicaSets which in turn manage Pods. It supports:
- Rolling updates
- Rollbacks
- Scaling
- Version tracking
π Full Deployment YAML with Explanation
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
π§ Field-by-Field Breakdown
Field | Purpose |
---|---|
apiVersion: apps/v1 |
API version for Deployment |
kind: Deployment |
Declares this is a Deployment object |
metadata.name |
Name of the Deployment |
metadata.labels |
Labels applied to Deployment (used for grouping) |
spec.replicas: 3 |
Desired number of Pod replicas |
spec.selector.matchLabels |
Selector to identify Pods managed by this Deployment |
spec.strategy |
Deployment strategy for updates |
rollingUpdate.maxUnavailable: 1 |
Allows one Pod to be down during update |
rollingUpdate.maxSurge: 1 |
Allows one extra Pod during update |
template.metadata.labels |
Labels for Pods created via this template |
containers.image: nginx:1.21 |
Container image version |
containerPort: 80 |
Port exposed by container |
π§ Common Commands
# Apply a deployment YAML
kubectl apply -f deployment.yaml
# View Deployments
kubectl get deployments
# View ReplicaSets
kubectl get rs
# View Pods
kubectl get pods
# View detailed info
kubectl describe deployment nginx-deployment
# Scale the deployment
kubectl scale deployment nginx-deployment --replicas=5
# Trigger a rollout (e.g., change image)
kubectl set image deployment/nginx-deployment nginx=nginx:1.22
# Check rollout status
kubectl rollout status deployment/nginx-deployment
# Rollback if needed
kubectl rollout undo deployment/nginx-deployment
# Delete the deployment
kubectl delete deployment nginx-deployment
βοΈ Deployment Lifecycle
- You
kubectl apply -f deployment.yaml
- Kubernetes creates a ReplicaSet
- ReplicaSet maintains desired Pods
- Deployment watches and updates the ReplicaSet when needed (e.g., version change)
π Best Practices
- Use
Deployment
instead of managing Pods or ReplicaSets directly - Use proper
labels
andselectors
to avoid misconfigurations - Use
RollingUpdate
for smooth version changes - Monitor rollouts using
kubectl rollout status
- Use
readinessProbes
andlivenessProbes
in production - Keep manifests in Git (GitOps)
π§ͺ Test It Yourself
# Apply Deployment
kubectl apply -f nginx-deployment.yaml
# Scale
kubectl scale deployment nginx-deployment --replicas=5
# Update Image
kubectl set image deployment/nginx-deployment nginx=nginx:1.22
# Rollback
kubectl rollout undo deployment nginx-deployment
π Difference Table
Feature | Deployment | ReplicaSet | ReplicationController |
---|---|---|---|
Rolling Updates | β Yes | β No | β No |
Rollback Support | β Yes | β No | β No |
Use in Production | β Recommended | β Only internal | β Deprecated |
Controller for Pods | Indirect (via RS) | Direct | Direct |
π― Conclusion:
Always use Deployments in modern Kubernetes for managing your apps. They provide the abstraction and control needed to safely roll out changes, scale apps, and recover from errors.