Ryan Lyu
Ryan Lyu

Reputation: 5115

Kubernetes: what's the difference between Deployment and Replica set?

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.

Upvotes: 96

Views: 55461

Answers (5)

Willem van der Veen
Willem van der Veen

Reputation: 36580

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.

Upvotes: 9

Hamza Ali
Hamza Ali

Reputation: 11

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.

Upvotes: 1

Thomas
Thomas

Reputation: 12009

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.

Upvotes: 66

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

Upvotes: 24

Faramarz Qoshchi
Faramarz Qoshchi

Reputation: 1632

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.

Upvotes: 89

Related Questions