Cloud Native 23 min read

Mastering Kubernetes ReplicaSet and Deployment: Concepts, YAML, and Hands‑On Examples

This guide explains the fundamentals of Kubernetes ReplicaSet and Deployment controllers, their specifications, how to write YAML manifests, and provides step‑by‑step command‑line examples for creating, scaling, updating, and rolling back applications in a cluster.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Mastering Kubernetes ReplicaSet and Deployment: Concepts, YAML, and Hands‑On Examples

ReplicaSet Overview

ReplicaSet is a Kubernetes controller that guarantees a specified number of pod replicas are running at any time. It watches pod health, restarts failed pods, and creates new pods when the current count falls below the desired number. Although still supported, the recommended practice is to manage pods through Deployments, which own ReplicaSets and add declarative update capabilities.

Key Components of a ReplicaSet

Desired replica count – the number of pod copies the user wants.

Label selector – selects the pods that belong to the ReplicaSet.

Pod template – defines the pod specification used to create new pods when the replica count is insufficient.

ReplicaSet YAML Specification Example

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: yecc/gcr.io-google_samples-gb-frontend:v3
        ports:
        - name: http
          containerPort: 80

Practical Example: Deploying a Guestbook with a ReplicaSet

Load the container image onto each node, create replicaset.yaml (as shown above), and apply it:

# Load image onto nodes
docker load -i frontend.tar.gz
# Apply the ReplicaSet
kubectl apply -f replicaset.yaml
# Verify the controller and pods
kubectl get rs
kubectl get pods

The command output shows three pods named frontend-*, confirming that the desired replica count is satisfied.

Scaling and Updating a ReplicaSet

To change the number of replicas, edit the replicas field in replicaset.yaml (e.g., from 3 to 4) and re‑apply, or use kubectl edit rs <name> to modify the object directly:

# Edit the ReplicaSet in place
kubectl edit rs frontend
# Change spec.replicas to the desired value and save

Updating the container image requires changing the image field in the pod template and re‑applying the manifest. New pods will be created with the new image, while existing pods continue to run the old image until they are deleted or replaced.

Deployment Overview

A Deployment is a higher‑level controller that creates and manages ReplicaSets. It adds features such as declarative updates, rolling upgrades, and rollbacks. When a Deployment is created, it generates a ReplicaSet; the ReplicaSet then creates the pods.

Deployment Specification Highlights

replicas – desired pod count.

selector – label selector that identifies the pods managed by the Deployment.

strategy – update strategy, either Recreate or RollingUpdate. For RollingUpdate, maxSurge and maxUnavailable control how many extra pods may be created or how many may be unavailable during an upgrade.

template – pod template (metadata and spec) used by the underlying ReplicaSet.

Deployment YAML Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: janakiramm/myapp:v1
        ports:
        - containerPort: 80

Deployment Operations

kubectl apply -f deployment.yaml

– create or update the Deployment declaratively. kubectl get deploy, kubectl get rs, kubectl get pods – inspect the Deployment, its ReplicaSet, and the pods.

Scaling: modify spec.replicas in the YAML and re‑apply, or run kubectl scale deployment <name> --replicas=<n>.

Rolling update: change the container image in the pod template and apply the manifest. Kubernetes creates a new ReplicaSet and gradually replaces old pods while respecting maxSurge and maxUnavailable.

Rollback: kubectl rollout undo deployment <name> reverts the Deployment to the previous revision.

History: kubectl rollout history deployment <name> shows revision numbers and change causes.

Key Command Summary

kubectl explain <resource>

– view API field documentation. kubectl apply -f <file.yaml> – create or update resources declaratively. kubectl edit rs <name> – edit a ReplicaSet in place. kubectl rollout history deployment <name> – view revision history. kubectl rollout undo deployment <name> – rollback to a previous revision.

deploymentKubernetesYAMLkubectlReplicaSet
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.