Kubernetes: Deployment and ReplicaSet

Keywords: Web Server Nginx Kubernetes

Background

Deployment and ReplicaSet are two important objects in Kubernetes. This paper briefly discusses some differences and connections between them.

Two Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-gysl
spec:
  replicas: 2
  selector:
    matchLabels:
      app-1: nginx
      app-2: busybox
  template:
    metadata:
      labels:
        app-1: nginx
        app-2: busybox
    spec:
      containers:
        - name: app-1
          image: nginx:1.16.0
          imagePullPolicy: Always
          ports:
            - containerPort: 80
            - containerPort: 8080
        - name: app-2
          image: busybox
          imagePullPolicy: Never
          command: ['/bin/sh', '-c']
          args:
            - while :;do sleep 20;done

This is a very simple Deployment that ensures that the number of pods carrying app-1=nginx and app-2=busybox tags equals the total number of spec.replicas specified. That is to say, when the number of PDs in this Deployment is large enough to be equal to 2, a Pod will be deleted, and vice versa, a Pod will be created.

This Deployment consists of two parts. Lines 1-10 of yaml in the example defines the Deployment Controller, and lines 10 later defines the controlled Pod. The latter part of template finds that it is much the same as the previous definition of Pod.

By the way, here's a command (update Deployment's mirror):

kubectl set image deployment/deployment-gysl app-1=nginx:latest

This command can also update the following objects:

  env            Update environment variables on a pod template
  image          Update one pod template Mirror image
  resources      In the object's pod templates Updated resource requests/limits
  selector       Set up resource Of selector
  serviceaccount Update ServiceAccount of a resource
  subject        Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding

Three Replica Sets

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replica-set-gysl
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest

A ReplicaSet object consists of a definition of the number of replicas and a Pod template, whose definition is a subset of Deployment. Deployment controllers actually manipulate ReplicaSet objects, not Pod objects.

ReplicaSet is responsible for ensuring that the number of Pods in the system is always equal to the specified number (for example, three) through the "controller mode". This is the main reason Deployment only allows restart Policy = Always for containers: ReplicaSet adjusts the number of Pods only if the container can guarantee that it is always in a Running state.

Deployment operates on the number and attributes of ReplicaSet through "Controller Mode" to achieve the two choreography actions of "Horizontal Expansion/Contraction" and "Rolling Update".

Four others

The rolling update in Kubernetes has been discussed in previous articles, which is the biggest difference between Deployment Controller and ReplicaSet, and the main reason why Deployment is widely used.

Posted by agulaid on Thu, 08 Aug 2019 05:33:12 -0700