92

Both replica set and deployment have the attribute replica: 3, what's the difference between deployment and replica set? Does deployment work via replica set under the hood?

configuration of deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    my-label: my-value
spec:
  replicas: 3
  selector:
    matchLabels:
      my-label: my-value
  template:
    metadata:
      labels:
        my-label: my-value
    spec:
      containers:
        - name: app-container
          image: my-image:latest

configuration of replica set

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
  labels:
    my-label: my-value
spec:
  replicas: 3
  selector:
    matchLabels:
      my-label: my-value
  template:
    metadata:
      labels:
        my-label: my-value
    spec:
      containers:
        - name: app-container
          image: my-image:latest

Kubernetes Documentation

When to use a ReplicaSet

A ReplicaSet ensures that a specified number of pod replicas are running at any given time. However, Deployment is a higher-level concept that manages ReplicaSets and provides declarative updates to Pods along with a lot of other useful features. Therefore, we recommend using Deployments instead of directly using ReplicaSets, unless you require custom update orchestration or don't require updates at all.

This actually means that you may never need to manipulate ReplicaSet objects: use a Deployment instead, and define your application in the spec section.

4
  • Does this answer your question? k8s - Why we need ReplicaSet when we have Deployments
    – P....
    Commented Oct 6, 2021 at 1:59
  • Thanks for providing the relating questions. It really helps. That question focuses on the usage difference, besides that, I also would like to know something under the hood. And it assumes the Deployment concept has been invented earlier than concept ReplicaSet. I am not sure about this assumption.
    – Ryan Lyu
    Commented Oct 6, 2021 at 4:46
  • this article explains more details. magalix.com/blog/kubernetes-deployments-101
    – Ryan Lyu
    Commented Oct 6, 2021 at 4:48
  • 1
    A replicaset has the responsibility of ensuring pods are available. deployment has the responsibility to manage different versions of an application by controlling one or more replicasets.
    – Thomas
    Commented Oct 6, 2021 at 7:45

5 Answers 5

85

Deployment resource makes it easier for updating your pods to a newer version.

Lets say you use ReplicaSet-A for controlling your pods, then You wish to update your pods to a newer version, now you should create Replicaset-B, scale down ReplicaSet-A and scale up ReplicaSet-B by one step repeatedly (This process is known as rolling update). Although this does the job, but it's not a good practice and it's better to let K8S do the job.

A Deployment resource does this automatically without any human interaction and increases the abstraction by one level.

Note: Deployment doesn't interact with pods directly, it just does rolling update using ReplicaSets.

7
  • 8
    So, the question is, why do we need ReplicaSets? Why do we need this man-in-the-middle? Why don't Deployments interact directly with pods?
    – t7e
    Commented Apr 28, 2022 at 21:39
  • @t7e Read the answer carefully! to avoid doing rolling update manually, and replacing old versions with new version one by one, which is not a good practice. Commented Apr 29, 2022 at 11:35
  • 7
    I am sorry, but your answer does not answer my question. Your answer is what deployment and replicaset do, but I am asking why do we need replicaset, if deployment is a more advanced concept? Why the concept of replicaset is not imbedded directly into deployment so that the deployment just operates and rolling-updates the pods directly? Why do we have those separate entities and not only one that manages pods?
    – t7e
    Commented Apr 29, 2022 at 17:45
  • 4
    @t7e The main question of this post is "What is the difference between between ReplicaSet and Deployment", Which i answered (Deployment creates one more level of abstraction). Also the post's second question is "Does deployment work via replica set under the hood?" Which also i answered. In the previous comment you asked a different question: "Why the concept of replicaset is not imbedded directly into deployment?" Which is out of the title of the question and can make my answer too complicated. Ask that question in another Stack question. Commented Apr 30, 2022 at 8:42
  • 2
    @t7e When you require a custom update procedure or don't require updates at all, you use ReplicaSet. The first reason is usually the reason why. By "custom", I mean it is something different than what a Deployment would automatically do when you tell it to "update". ReplicaSet exists to provide the capabilities to do whatever you want during the update procedure of your pods.
    – Lam Le
    Commented Jul 11, 2023 at 18:59
64

A ReplicaSet ensures that a number of Pods is created in a cluster. The pods are called replicas and are the mechanism of availability in Kubernetes. But changing the ReplicaSet will not take effect on existing Pods, so it is not possible to easily change, for example, the image version.

A deployment is a higher abstraction that manages one or more ReplicaSets to provide a controlled rollout of a new version. When the image version is changed in the Deployment, a new ReplicaSet for this version will be created with initially zero replicas. Then it will be scaled to one replica, after that is running, the old ReplicaSet will be scaled down. (The number of newly created pods, the step size so to speak, can be tuned.)

As long as you don't have a rollout in progress, a deployment will result in a single replicaset with the replication factor managed by the deployment.

I would recommend to always use a Deployment and not a bare ReplicaSet.

2
  • so a deployment is basically an upgraded replicaset, and it supersedes ever using replicasets?
    – Blaisem
    Commented Jul 9, 2022 at 20:41
  • 1
    It has a different responsibility and in general you would prefer it over using a replicaset directly due to the added benefits.
    – Thomas
    Commented Jun 27, 2023 at 15:14
23

One of the differences between Deployments and ReplicaSet is changes made to container are not reflected once the ReplicaSet is created

For example: This is my replicaset.yaml

apiVersion: apps/v1
kind: ReplicaSet

metadata:
  name: nginx-replicaset
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 5 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.13.2
        ports:
        - containerPort: 80

I will apply this replicaset using this command

kubectl apply -f replicaset.yaml
kubectl get pods
kubectl describe pod <<name_of_pod>>

So from pod definition, we can observe that nginx is using 1.13.2. Now let's change image version to 1.14.2 in replicaset.yaml Again apply changes

kubectl apply -f replicaset.yaml
kubectl get pods
kubectl describe pod <<name_of_pod>>

Now we don't see any changes in Pod and they are still using old image.


Now let us repeat the same with a deployment (deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 5 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.13.2
        ports:
        - containerPort: 80

I will apply this deployment using this command

kubectl apply -f deployment.yaml
kubectl get pods
kubectl describe pod <<name_of_pod>>

Change the deployment.yaml file with some other version of nginx image

apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 5 # tells deployment to run 2 pods matching the template
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.2
            ports:
            - containerPort: 80

I will again apply this deployment using this command

kubectl apply -f deployment.yaml
kubectl get pods
kubectl describe pod <<name_of_pod>>

Now we can see these pods and we can see updated image in the description of pod

1
  • Basically, the same point as @faramarz is saying regarding rolling update. Commented Jul 13, 2022 at 6:09
8

TLDR:

Deployment manages → Replica set manages → pod(s) abstraction of → container (e.g docker container)


A Deployment in Kubernetes is a higher-level abstraction that represents a set of replicas of your application. It ensures that your desired number of replicas of your application are running and available.

A ReplicaSet, on the other hand, is a lower-level resource that is responsible for maintaining a stable set of replicas of your application. ReplicaSet ensures that a specified number of replicas are running and available.

Example: Suppose you have a web application that you want to run on 3 replicas for high availability.
You would create a Deployment resource in Kubernetes, specifying the desired number of replicas as 3 pods. The deployment would then create and manage the ReplicaSet, which would in turn create and manage 3 replicas of your web application pod.

In summary, Deployment provides higher-level abstractions for scaling, rolling updates and rolling back, while ReplicaSet provides a lower-level mechanism for ensuring that a specified number of replicas of your application are running.

1

A ReplicaSet is used to ensure that a specific number of replicas (copies) of a pod are running at any given time, while a Deployment manages updates to a ReplicaSet by creating a new ReplicaSet with the updated pod template and gradually scaling it up while scaling down the old ReplicaSet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.