DEV Community

Dechen Tshering
Dechen Tshering

Posted on

Learning Kubernetes: ReplicationController vs ReplicaSet

As I dive deeper into Kubernetes, I’ve been exploring the concepts of ReplicationControllers and ReplicaSets—two core constructs used to ensure high availability and scalability of pods in a Kubernetes cluster.

In this post, I’ll share what I learned, along with sample YAML definitions and key commands. Hopefully, it helps anyone else navigating this space too!

What is a ReplicationController?

A ReplicationController (RC) ensures that a specified number of pod replicas are running at any given time. While it’s considered a bit old-school now (ReplicaSet and Deployments have largely replaced it), it’s still a fundamental concept worth understanding.

Here’s a sample YAML to create a ReplicationController:

apiVersion: v1
kind: ReplicationController
metadata: 
  name: myrc
spec: 
  replicas: 5
  template:
    metadata:
      labels: 
        app: web
    spec:
      containers:
      - name: c1
        image: nginx

Enter fullscreen mode Exit fullscreen mode

Basic ReplicationController Commands

List all RCs:

kubectl get rc or kubectl get replicationcontroller
Enter fullscreen mode Exit fullscreen mode

Delete an RC:

kubectl delete rc <rc-name>
Enter fullscreen mode Exit fullscreen mode

Scale the RC:

kubectl scale --replicas=<number> rc/<rc-name>
Enter fullscreen mode Exit fullscreen mode

What is ReplicaSet?

A ReplicaSet (RS) is the next-gen version of RC, introduced with better support for label selectors, including set-based ones. It ensures that a specified number of pod replicas are maintained and is mostly used under the hood by Deployments.

Example 1: Equality-Based Selector:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: mywebapp
spec:
  replicas: 5
  selector:
    matchLabels: 
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: c1
        image: nginx

Enter fullscreen mode Exit fullscreen mode

Example 2: Set-Based Selector:

apiVersion: apps/v1
kind: ReplicaSet
metadata: 
  name: webdata
spec:
  replicas: 4
  selector:
    matchExpressions:
    - key: app
      operator: In
      values:
        - nginx
        - web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: c1
        image: nginx

Enter fullscreen mode Exit fullscreen mode

Basic Replica Set Commands:

List all ReplicaSets

kubectl get rs
Enter fullscreen mode Exit fullscreen mode

This shows all the ReplicaSets in your current namespace.

Describe a ReplicaSet

kubectl describe rs <replicaset-name>
Enter fullscreen mode Exit fullscreen mode

Provides detailed information including events, pod template, and selector.

Delete a ReplicaSet

kubectl delete rs <replicaset-name>
Enter fullscreen mode Exit fullscreen mode

Deletes the ReplicaSet

Scale a ReplicaSet

kubectl scale rs/<replicaset-name> --replicas=3
Enter fullscreen mode Exit fullscreen mode

Adjusts the number of desired pod replicas.

Edit a ReplicaSet

kubectl edit rs <replicaset-name>
Enter fullscreen mode Exit fullscreen mode

Opens the ReplicaSet manifest in your default editor so you can make changes on the fly.

Apply from a YAML file

kubectl apply -f webrs.yml
Enter fullscreen mode Exit fullscreen mode

Creates or updates a ReplicaSet from a manifest file.

View pods managed by a ReplicaSet

kubectl get pods -l app=web
Enter fullscreen mode Exit fullscreen mode

Use label selectors to filter pods created by the ReplicaSet.

Key difference between ReplicationController and ReplicaSet:

Feature ReplicationController (RC) ReplicaSet (RS)
API Version v1 apps/v1
Label Selector Support Equality-based only Equality & set-based
Usage Legacy / older systems Modern replacement for RC
Used by Deployment No Yes
Flexibility Less flexible More flexible
Community Support Deprecated Actively supported

After getting comfortable with Replication Controllers and Replica Sets, I’ve realized they’re just the beginning. Now, I’m excited to dive into Deployments—the real game-changer for managing apps in Kubernetes!

Top comments (0)

OSZAR »