Stateless application: Deployment replica application and horizontal expansion

Words in this article:

  • replace(/r ɪˈ ple ɪ s /) replacement and substitution; In this article, the modified yaml file replaces the old yaml file

  • rollout(/ ɑː ®/) record; In this article is a copy version of deploy that records history

  • describe(/d ɪˈ skra ɪ b /) description; In this article, it means to record the process

  • scale(/ske ɪ l /) zoom; In this article, we mean to scale the number of copies

  • pause(/p ɔː z /) suspension; In this article, it means to pause the update

  • resume(/r ɪˈ zju ː m /) recovery; In this article, it means to resume from pause to normal state


----------------
RC (Replication Controller) and RS (ReplicaSet)

(almost obsolete) RC is a replication controller that ensures that the number of pods reaches the desired value and that replicas are always available.

(not recommended to be used alone) RS is the next generation of RC and supports tag set.

----------------

Stateless service Deployment means stateless... Used to deploy stateless services.

Requests such as web page access are stateless. Each request contains all the required information, and each request has nothing to do with the last time.
Deployment is the most commonly used controller. It is generally used to manage and maintain stateless microservices within the enterprise, such as configserver, zuul and springboot. The Pod that can manage multiple copies can realize seamless migration, automatic capacity expansion and reduction, automatic disaster recovery, one click rollback and other functions.

Creation of deployment

You can create a Deployment through the command:

kubectl create deployment nginx --image=nginx:1.20.2

You can generate the created copy into a yaml file:

kubectl get deployment nginx -o yaml > nginx.yaml

You can then modify the parameters of the yaml file and reload the new yaml file:

kubectl replace -f nginx.yaml

You can also modify yaml files online, which will take effect directly after exiting:

kubectl edit deploy nginx

Generate deployment from yaml file:

apiVersion: v1
items:
- apiVersion: apps/v1
  kind: Deployment
  metadata:
    annotations:
      deployment.kubernetes.io/revision: "2"
    creationTimestamp: "2022-02-08T07:52:05Z"
    generation: 2
    labels:
      app: nginx
    name: nginx
    namespace: default
    resourceVersion: "266395"
    uid: d4a3dbe9-5156-42cf-aaf5-3dfefccf4753
  spec:
    progressDeadlineSeconds: 600
    replicas: 1  # Number of copies
    revisionHistoryLimit: 10  # Keep the version of history
    selector:
      matchLabels:
        app: nginx
    strategy:
      rollingUpdate:
        maxSurge: 25%
        maxUnavailable: 25%
      type: RollingUpdate
    template:
      metadata:
        creationTimestamp: null
        labels:
          app: nginx
      spec:
        containers:
        - image: nginx:1.18.0
          imagePullPolicy: Always
          name: nginx
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
        dnsPolicy: ClusterFirst
        restartPolicy: Always
        schedulerName: default-scheduler
        securityContext: {}
        terminationGracePeriodSeconds: 30
  status:
    availableReplicas: 1
    conditions:
    - lastTransitionTime: "2022-02-08T07:52:10Z"
      lastUpdateTime: "2022-02-08T07:52:10Z"
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: "True"
      type: Available
    - lastTransitionTime: "2022-02-08T07:52:05Z"
      lastUpdateTime: "2022-02-08T07:55:54Z"
      message: ReplicaSet "nginx-86dddd6686" is progressing.
      reason: ReplicaSetUpdated
      status: "True"
      type: Progressing
    observedGeneration: 2
    readyReplicas: 1
    replicas: 2
    unavailableReplicas: 1
    updatedReplicas: 1
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

Description of deployment status:

[root@k8s-master01 ~]# kubectl get deployments.apps -o wide 
NAME      READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES   SELECTOR
mynginx   1/1     1            1           4d5h   mynginx      nginx    k8s-app=mynginx
  • NAME: Deployment NAME
  • Ready: the status of Pod, which has reached the number of ready
  • UP-TO-DATE: the number of copies that have reached the desired state
  • AVAILABLE: the number of AVAILABLE
  • AGE: the time the program runs
  • CONTAINERS: custom name of the container
  • IMAGES: container image name
  • SELECTOR: managed Pod tag

Upgrade of deployment

Check the version of the image created in the deploy of nginx: you can see that it is 1.18.0

[root@k8s-master01 ~]# kubectl get deploy -o yaml | grep image
        - image: nginx:1.18.0
          imagePullPolicy: Always

We can use the set command to update the version of the image: set here means setting.

[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:1.20.2 --record
[root@k8s-master01 ~]# kubectl get deployments.apps nginx -o yaml | grep image
    kubernetes.io/change-cause: kubectl set image deploy nginx nginx=1.18.1 --record=true
      - image: nginx:1.20.2
        imagePullPolicy: Always

You can view the upgrade process using the following command:

[root@k8s-master01 ~]# kubectl rollout status deployment nginx
[root@k8s-master01 ~]# kubectl describe deployments.apps nginx

The upgrade process is as follows: when creating deploy, the system will also create depoly rs. when updating deploy, the system will add a new RS and reduce the old rs by 1 until the replacement is completed.

Rollback of deployment

The rollback of deployment can be rolled back to the previous version or the specified version. The number of historical versions is controlled by the following parameter:

    revisionHistoryLimit: 10

View the historical version: you can see that the copy has changed several times

[root@k8s-master01 ~]# kubectl rollout history deployment nginx 
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
8         kubectl set image deploy nginx nginx=1.18.1 --record=true
9         kubectl set image deploy nginx nginx=nginx:1.20.2 --record=true

Roll back to the previous version: undo means undo (my understanding is to undo the current version and return to the previous version)

[root@k8s-master01 ~]# kubectl rollout undo deployment nginx
deployment.apps/nginx rolled back

Check that version 1.18.0 has been returned from 1.20.2:

[root@k8s-master01 ~]# kubectl get deployments.apps nginx -o yaml | grep image
    kubernetes.io/change-cause: kubectl set image deploy nginx nginx=1.18.1 --record=true
      - image: nginx:1.18.0
        imagePullPolicy: Always

If you publish multiple times, roll back to the specified version:

[root@k8s-master01 ~]# kubectl rollout history deployment nginx 
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
9         kubectl set image deploy nginx nginx=nginx:1.20.2 --record=true
10        kubectl set image deploy nginx nginx=1.18.1 --record=true

You are /var/spool/mail/root New messages in
[root@k8s-master01 ~]# kubectl rollout history deployment nginx --revision=9
deployment.apps/nginx with revision #9
Pod Template:
  Labels:	app=nginx
	pod-template-hash=67d5b4548c
  Annotations:	kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.20.2 --record=true
  Containers:
   nginx:
    Image:	nginx:1.20.2
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@k8s-master01 ~]# kubectl rollout undo deployment nginx --to-revision=9
deployment.apps/nginx rolled back
[root@k8s-master01 ~]# kubectl get deployments.apps nginx -o yaml | grep image
    kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.20.2
      - image: nginx:1.20.2
        imagePullPolicy: Always

You can see that the above information version has been rolled back to version 1.20.2; In addition, even if the update fails and the new image pull fails, the old copy will not stop working and will continue to work until the upgrade is successful.

Capacity expansion and deployment

Use the command to expand the capacity: change the nginx copy from one to two. Note that the expansion of RS will not change because the essence of Pod has not changed.
The word scale here means zoom.

[root@k8s-master01 ~]# kubectl get deployments.apps 
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           17h
[root@k8s-master01 ~]# kubectl scale --replicas=2 deployment nginx 
deployment.apps/nginx scaled
[root@k8s-master01 ~]# kubectl get deployments.apps 
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   2/2     2            2           17h

Suspension and resumption of deployment update

Update pause: this command is used to pause the update. After you use kubectl set command to modify the configuration of Pod, it will not take effect immediately. Even if multiple modifications are accumulated, they will not take effect immediately.

[root@k8s-master01 ~]# kubectl rollout pause deployment nginx

Pause and resume: release the previous update pause function, and the modifications made during the pause will take effect

[root@k8s-master01 ~]# kubectl rollout resume deployment nginx

Considerations for deployment

  • . spec.revisionHistoryLimit: 10: set the number of old versions of RS to be reserved. If it is 0, it will not be saved
  • . spec.minReadySeconds: optional. Specifies that the newly created Pod is the minimum number of seconds Ready when no container crashes. The default is 0, which is considered available once it is created

The strategy of rolling update is to create a new Pod, then delete an old Pod and replace the old one with a new one.

  • .spec.strategy.type: the method of updating deploy. RollingUpdate is the default
  • RollingUpdate: scrolling update. maxSurge and maxUnavailable can be specified
  • maxUnavailable: Specifies the maximum number of unavailable pods during rollback or update. The default is 25%. You can also set a number; If it is 0, maxSurge cannot be 0.
  • maxSurge: the maximum number of pods that can exceed the expected value. Optional. The default is 25%. If it is 0, maxUnavailable cannot be 0.
  • Recreate: to rebuild, delete the old Pod and create a new Pod.

Keywords: Java Operation & Maintenance Docker Kubernetes

Added by free2code on Thu, 10 Feb 2022 00:31:19 +0200