📘 Kubernetes Notes – Labels, ReplicaSet & Deployment


🔹 LABELS

Labels are key-value pairs attached to Kubernetes objects like:

  • Pods

  • ReplicaSets

  • Deployments

  • Services

  • Nodes

They are mainly used for:

  • Identification

  • Grouping

  • Selection

  • Management

Features

  • Can be added during creation or runtime

  • Multiple labels per object allowed

  • ReplicaSet/Service manages Pods only using labels

Example

kubectl run pod-1 --image=nginx --labels="env=dev,app=swiggy" kubectl get pods --show-labels

Extra useful commands

kubectl label pod pod-1 tier=frontend kubectl label pod pod-1 tier- kubectl get pods -l env=dev

🔹 SELECTORS

Selectors filter Pods using labels.

Used by:

  • ReplicaSet

  • Deployment

  • Service

Types

1️⃣ Equality-based

app=paytm env!=prod

2️⃣ Set-based

env in (dev,test) env notin (prod)

Example

kubectl get pods -l app=paytm

🔹 REPLICASET (RS)

ReplicaSet ensures desired number of Pods are always running.

Features

  • Creates multiple replicas

  • Auto recreates if Pod crashes/deletes

  • All Pods use same template

  • Uses Labels + Selectors

  • Supports scaling

  • No rolling updates (manual only)

Flow

ReplicaSet → Pods

Important

  • ReplicationController = Old version

  • ReplicaSet = New version


ReplicaSet YAML

apiVersion: apps/v1 kind: ReplicaSet metadata: name: movies labels: app: paytm spec: replicas: 3 selector: matchLabels: app: paytm template: metadata: labels: app: paytm spec: containers: - name: cont1 image: rahashaik/movies:latest

ReplicaSet Commands

kubectl create -f rs.yml kubectl get rs kubectl get pods -o wide kubectl describe rs movies kubectl edit rs movies kubectl delete rs movies kubectl scale rs/movies --replicas=10 kubectl get pods -l app=paytm

❌ ReplicaSet Limitations

  • No rolling updates

  • No rollback

  • No version control

  • Not preferred in production

👉 Mostly managed indirectly via Deployment.


🔹 DEPLOYMENT

Deployment = ReplicaSet + Advanced Features

It is the recommended way to manage Pods in production.

Features

✅ Rolling updates
✅ Rollback
✅ Zero downtime
✅ Version history
✅ Easy scaling
✅ Self-healing

Flow

Deployment → ReplicaSet → Pods

Deployment YAML

apiVersion: apps/v1 kind: Deployment metadata: name: movies labels: app: paytm spec: replicas: 3 selector: matchLabels: app: paytm template: metadata: labels: app: paytm spec: containers: - name: cont1 image: rahamshaik/mynetflix:1.0

Deployment Commands

Create

kubectl create -f deployment.yml

List

kubectl get deploy kubectl get rs kubectl get pods

Scale

kubectl scale deploy movies --replicas=10

Edit image (update)

kubectl set image deploy/movies cont1=mynetflix:2.0

Rollout commands

kubectl rollout history deployment movies kubectl rollout status deployment movies kubectl rollout undo deployment movies kubectl rollout pause deployment movies kubectl rollout resume deployment movies kubectl rollout undo deploy movies --to-revision=1

Delete

kubectl delete deploy movies

🔹 Rolling Update Strategy (Important for Interviews)

Types

1️⃣ RollingUpdate (Default)

  • No downtime

  • Gradually replaces Pods

strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 1

2️⃣ Recreate

  • Deletes all Pods first

  • Then creates new Pods

  • Causes downtime

strategy: type: Recreate

🔹 Useful Extra Concepts

Scale automatically (HPA)

kubectl autoscale deploy movies --cpu-percent=50 --min=2 --max=10

Check events

kubectl get events

Logs

kubectl logs pod-name kubectl logs -f pod-name

Exec into container

kubectl exec -it pod-name -- /bin/bash

🔹 Quick Comparison Table

FeatureReplicaSetDeployment
Scaling
Self-healing
Rolling update
Rollback
Version history
Production use

Comments

Popular posts from this blog

Managing Amazon EBS Volumes and Snapshots Across Regions

Git for Beginners: Complete Guide from Installation to First Push on GitHub

AWS - Amazon Web Services