Kubernetes core combat (IV) — deployments

Time:2022-5-20

6. Deployments (key)

A deployment controller provides a descriptive update method for pods and replicasets.

Describe the desired state in deployment, and the deployment controller changes the actual state at a controlled rate to achieve the desired state. You can define deployments to create new replicasets, or delete existing deployments and use all their resources through the new deployments.

Use case

The following are typical deployment use cases:

Create a deployment to expand the replicaset. Replicaset creates pods in the background. Check the status of the replicaset deployment to see if it was successful.

Declare the new status of the pod by updating the podtemplatespec of the deployment. A new replicaset is created and the deployment manager moves the pod from the old replicaset to the new replicaset at a controlled rate. Each new replicaset updates the modification history of the deployment.

Roll back to an earlier deployment version if the current state of the deployment is unstable. Each rollback will update the modification of the deployment.

Expand deployment to take on more load

Pause the deployment, modify its podtemplatespec, and then resume it to start a new deployment.

Use the deployment status as an indicator of stuck deployment.

Clean up older replicasets that are no longer needed.

1) Create deployment
[[email protected] ~/yaml/test]# vim deployments.yaml
[[email protected] ~/yaml/test]# cat deployments.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# kubectl apply -f deployments.yaml 
deployment.apps/nginx-deployment created
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]#
Meaning introduction:

In this example: a deployment named nginx deployment will be created by metadata. The name field indicates.

Deployment creates three replicated pods, indicated by the replicas field.

The selector field defines how deployment finds pods to manage. In this case, just select the tag defined in the pod template (app: nginx). However, more complex selection rules are possible as long as the pod template itself satisfies the rules.

explain:

matchLabelsThe field is the mapping of {key, value}. Single {key, value} inmatchLabelsThe value in the mapping is equivalent tomatchExpressionsThe key field of the element is “key”, the operator is “in”, and the value array only contains “value”. All requirements, frommatchLabelsandmatchExpressions, must be met to match.

The template field contains the following subfields:

Pod is marked as app: nginx, using the labels field.

Pod template specification or template. The spec field instructs pods to run a container, nginx, and run the image of nginx docker hub version 1.7.9.

Create a container and name it nginx using the name field.

View detailed field explanation:
[[email protected] ~]# kubectl  explain Deployment.spec
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the Deployment.

     DeploymentSpec is the specification of the desired behavior of the
     Deployment.

FIELDS:
   minReadySeconds<integer>
     Minimum number of seconds for which a newly created pod should be ready
     without any of its container crashing, for it to be considered available.
     Defaults to 0 (pod will be considered available as soon as it is ready)

   paused<boolean>
     Indicates that the deployment is paused.

   progressDeadlineSeconds<integer>
     The maximum time in seconds for a deployment to make progress before it is
     considered to be failed. The deployment controller will continue to process
     failed deployments and a condition with a ProgressDeadlineExceeded reason
     will be surfaced in the deployment status. Note that progress will not be
     estimated during the time a deployment is paused. Defaults to 600s.

   replicas<integer>
     Number of desired pods. This is a pointer to distinguish between explicit
     zero and not specified. Defaults to 1.

   revisionHistoryLimit<integer>
     The number of old ReplicaSets to retain to allow rollback. This is a
     pointer to distinguish between explicit zero and not specified. Defaults to
     10.

   selector <Object> -required-
     Label selector for pods. Existing ReplicaSets whose pods are selected by
     this will be the ones affected by this deployment. It must match the pod
     template's labels.

   strategy<Object>
     The deployment strategy to use to replace existing pods with new ones.

   template <Object> -required-
     Template describes the pods that will be created.

[[email protected] ~]#
View pod
[[email protected] ~/yaml/test]# kubectl get pod 
NAME                                     READY   STATUS        RESTARTS     AGE
ingress-demo-app-694bf5d965-q4l7m        1/1     Terminating   0            23h
ingress-demo-app-694bf5d965-v28sl        1/1     Running       0            3m9s
ingress-demo-app-694bf5d965-v652j        1/1     Running       0            23h
nfs-client-provisioner-dc5789f74-nnk77   1/1     Running       1 (8h ago)   22h
nginx-deployment-66b6c48dd5-5hhjq        1/1     Running       0            3m9s
nginx-deployment-66b6c48dd5-9z2n5        1/1     Running       0            3m19s
nginx-deployment-66b6c48dd5-llq7c        1/1     Running       0            9m10s
[[email protected] ~/yaml/test]#
View deployments
[[email protected] ~/yaml/test]# kubectl get deployments.apps 
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
ingress-demo-app         2/2     2            2           23h
nfs-client-provisioner   1/1     1            1           22h
nginx-deployment         3/3     3            3           9m45s
[[email protected] ~/yaml/test]#
Explanation:

When you check deployments in a cluster, the following fields are displayed:

Name lists the names of deployments in the cluster.

Detailed displays the required number of copies of the application, which are defined when the deployment is created. This is the desired state.

Current displays the number of copies currently running.

Up-to-date displays the number of copies that have been updated to achieve the desired state.

Available displays the number of copies of the application available to users.

Age displays the amount of time the application has been running.

View RS
[[email protected] ~/yaml/test]# kubectl get replicasets.apps 
NAME                               DESIRED   CURRENT   READY   AGE
ingress-demo-app-694bf5d965        2         2         2       23h
nfs-client-provisioner-dc5789f74   1         1         1       23h
nginx-deployment-66b6c48dd5        3         3         3       19m
[[email protected] ~/yaml/test]#
View the labels of pods
[[email protected] ~/yaml/test]# kubectl get pods --show-labels
NAME                                     READY   STATUS        RESTARTS     AGE   LABELS
ingress-demo-app-694bf5d965-q4l7m        1/1     Terminating   0            23h   app=ingress-demo-app,pod-template-hash=694bf5d965
ingress-demo-app-694bf5d965-v28sl        1/1     Running       0            15m   app=ingress-demo-app,pod-template-hash=694bf5d965
ingress-demo-app-694bf5d965-v652j        1/1     Running       0            23h   app=ingress-demo-app,pod-template-hash=694bf5d965
nfs-client-provisioner-dc5789f74-nnk77   1/1     Running       1 (8h ago)   23h   app=nfs-client-provisioner,pod-template-hash=dc5789f74
nginx-deployment-66b6c48dd5-48k9j        0/1     Terminating   0            21m   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-5hhjq        1/1     Running       0            15m   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-9z2n5        1/1     Running       0            15m   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-kvzft        0/1     Terminating   0            21m   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-llq7c        1/1     Running       0            21m   app=nginx,pod-template-hash=66b6c48dd5
[[email protected] ~/yaml/test]#
2) Update rollback deployment
Using the mirror upgrade command line
[[email protected] ~/yaml/test]# kubectl get deployments -o wide
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS               IMAGES                                                                                    SELECTOR
ingress-demo-app         2/2     2            2           23h   whoami                   traefik/whoami:v1.6.1                                                                     app=ingress-demo-app
nfs-client-provisioner   1/1     1            1           23h   nfs-client-provisioner   registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2   app=nfs-client-provisioner
nginx-deployment         3/3     3            3           18m   nginx                    nginx:1.14.2                                                                              app=nginx
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# kubectl --record deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.20.1
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx-deployment image updated
deployment.apps/nginx-deployment image updated
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# kubectl get deployments -o wide
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS               IMAGES                                                                                    SELECTOR
ingress-demo-app         2/2     2            2           23h   whoami                   traefik/whoami:v1.6.1                                                                     app=ingress-demo-app
nfs-client-provisioner   1/1     1            1           23h   nfs-client-provisioner   registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2   app=nfs-client-provisioner
nginx-deployment         3/3     1            3           24m   nginx                    nginx:1.20.1                                                                              app=nginx
[[email protected] ~/yaml/test]#
Modifying images in yaml mode
[[email protected] ~/yaml/test]# kubectl edit deployments.apps nginx-deployment
Edit cancelled, no changes made.
[[email protected] ~/yaml/test]#
View the update process
[[email protected] ~/yaml/test]# kubectl rollout status deployment.v1.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
[[email protected] ~/yaml/test]#
View details
[[email protected] ~/yaml/test]# kubectl describe deployments
Name:                   ingress-demo-app
Namespace:              default
CreationTimestamp:      Tue, 16 Nov 2021 13:28:26 +0800
Labels:                 app=ingress-demo-app
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=ingress-demo-app
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=ingress-demo-app
  Containers:
   whoami:
    Image:        traefik/whoami:v1.6.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   ingress-demo-app-694bf5d965 (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  23h   deployment-controller  Scaled up replica set ingress-demo-app-694bf5d965 to 2


Name:               nfs-client-provisioner
Namespace:          default
CreationTimestamp:  Tue, 16 Nov 2021 14:07:33 +0800
Labels:             app=nfs-client-provisioner
Annotations:        deployment.kubernetes.io/revision: 1
Selector:           app=nfs-client-provisioner
Replicas:           1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:       Recreate
MinReadySeconds:    0
Pod Template:
  Labels:           app=nfs-client-provisioner
  Service Account:  nfs-client-provisioner
  Containers:
   nfs-client-provisioner:
    Image:      registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2
    Port:       <none>
    Host Port:  <none>
    Environment:
      PROVISIONER_NAME:  k8s-sigs.io/nfs-subdir-external-provisioner
      NFS_SERVER:        192.168.1.66
      NFS_PATH:          /nfs/
    Mounts:
      /persistentvolumes from nfs-client-root (rw)
  Volumes:
   nfs-client-root:
    Type:      NFS (an NFS mount that lasts the lifetime of a pod)
    Server:    192.168.1.66
    Path:      /nfs/
    ReadOnly:  false
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nfs-client-provisioner-dc5789f74 (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  23h   deployment-controller  Scaled up replica set nfs-client-provisioner-dc5789f74 to 1


Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Wed, 17 Nov 2021 12:54:46 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 3
                        kubernetes.io/change-cause:
                          kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.20.1 --record=true
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.16.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-559d658b74 (3/3 replicas created)
Events:
  Type    Reason             Age                From                   Message
  ----    ------             ----               ----                   -------
  Normal  ScalingReplicaSet  30m                deployment-controller  Scaled up replica set nginx-deployment-66b6c48dd5 to 3
  Normal  ScalingReplicaSet  5m55s              deployment-controller  Scaled up replica set nginx-deployment-58b9b8ff79 to 1
  Normal  ScalingReplicaSet  5m27s              deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 2
  Normal  ScalingReplicaSet  5m27s              deployment-controller  Scaled up replica set nginx-deployment-58b9b8ff79 to 2
  Normal  ScalingReplicaSet  5m                 deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 1
  Normal  ScalingReplicaSet  5m                 deployment-controller  Scaled up replica set nginx-deployment-58b9b8ff79 to 3
  Normal  ScalingReplicaSet  4m56s              deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 0
  Normal  ScalingReplicaSet  78s                deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 1
  Normal  ScalingReplicaSet  63s                deployment-controller  Scaled down replica set nginx-deployment-58b9b8ff79 to 2
  Normal  ScalingReplicaSet  63s                deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 2
  Normal  ScalingReplicaSet  49s (x3 over 61s)  deployment-controller  (combined from similar events): Scaled down replica set nginx-deployment-58b9b8ff79 to 0
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]#
3) Deployment history
View history
[[email protected] ~/yaml/test]# kubectl rollout history deployment.v1.apps/nginx-deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.20.1 --record=true
3         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.20.1 --record=true

[[email protected] ~/yaml/test]#
Rollback to last
[[email protected] ~/yaml/test]# kubectl rollout undo deployment.v1.apps/nginx-deployment
deployment.apps/nginx-deployment rolled back
[[email protected] ~/yaml/test]# kubectl rollout status deployment.v1.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
[[email protected] ~/yaml/test]#
Rollback to the specified version
[[email protected] ~/yaml/test]# kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=3
deployment.apps/nginx-deployment rolled back
[[email protected] ~/yaml/test]# kubectl rollout status deployment.v1.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
[[email protected] ~/yaml/test]#
4) Zoom deployment
Expand to ten pods
[[email protected] ~/yaml/test]# kubectl scale deployment.v1.apps/nginx-deployment --replicas=10
deployment.apps/nginx-deployment scaled
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# kubectl  get deployments.apps
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
ingress-demo-app         0/2     2            0           24h
nfs-client-provisioner   0/1     1            0           23h
nginx-deployment         5/10    10           5           45m
[[email protected] ~/yaml/test]#

Assuming that the horizontal autoscale pod is enabled, in a cluster, you can set the autoscaler for deployment and select the minimum and maximum pods to run based on the CPU utilization of existing pods.

[[email protected] ~/yaml/test]# kubectl autoscale deployment.v1.apps/nginx-deployment --min=10 --max=15 --cpu-percent=80
horizontalpodautoscaler.autoscaling/nginx-deployment autoscaled
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]#

https://blog.csdn.net/qq\_33921750

https://my.oschina.net/u/3981543

https://www.zhihu.com/people/…

https://segmentfault.com/u/hp…

https://juejin.cn/user/331578…

https://space.bilibili.com/35…

https://cloud.tencent.com/dev…

Zhihu, CSDN, open source China, Sifu, Nuggets, BiliBili, Tencent cloud

This article usesArticle synchronization assistantSynchronization ##### 6. Deployments (key)

A deployment controller provides a descriptive update method for pods and replicasets.

Describe the desired state in deployment, and the deployment controller changes the actual state at a controlled rate to achieve the desired state. You can define deployments to create new replicasets, or delete existing deployments and use all their resources through the new deployments.

Use case

The following are typical deployment use cases:

Create a deployment to expand the replicaset. Replicaset creates pods in the background. Check the status of the replicaset deployment to see if it was successful.

Declare the new status of the pod by updating the podtemplatespec of the deployment. A new replicaset is created and the deployment manager moves the pod from the old replicaset to the new replicaset at a controlled rate. Each new replicaset updates the modification history of the deployment.

Roll back to an earlier deployment version if the current state of the deployment is unstable. Each rollback will update the modification of the deployment.

Expand deployment to take on more load

Pause the deployment, modify its podtemplatespec, and then resume it to start a new deployment.

Use the deployment status as an indicator of stuck deployment.

Clean up older replicasets that are no longer needed.

1) Create deployment
[[email protected] ~/yaml/test]# vim deployments.yaml
[[email protected] ~/yaml/test]# cat deployments.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# kubectl apply -f deployments.yaml 
deployment.apps/nginx-deployment created
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]#
Meaning introduction:

In this example: a deployment named nginx deployment will be created by metadata. The name field indicates.

Deployment creates three replicated pods, indicated by the replicas field.

The selector field defines how deployment finds pods to manage. In this case, just select the tag defined in the pod template (app: nginx). However, more complex selection rules are possible as long as the pod template itself satisfies the rules.

explain:

matchLabelsThe field is the mapping of {key, value}. Single {key, value} inmatchLabelsThe value in the mapping is equivalent tomatchExpressionsThe key field of the element is “key”, the operator is “in”, and the value array only contains “value”. All requirements, frommatchLabelsandmatchExpressions, must be met to match.

The template field contains the following subfields:

Pod is marked as app: nginx, using the labels field.

Pod template specification or template. The spec field instructs pods to run a container, nginx, and run the image of nginx docker hub version 1.7.9.

Create a container and name it nginx using the name field.

View detailed field explanation:
[[email protected] ~]# kubectl  explain Deployment.spec
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the Deployment.

     DeploymentSpec is the specification of the desired behavior of the
     Deployment.

FIELDS:
   minReadySeconds<integer>
     Minimum number of seconds for which a newly created pod should be ready
     without any of its container crashing, for it to be considered available.
     Defaults to 0 (pod will be considered available as soon as it is ready)

   paused<boolean>
     Indicates that the deployment is paused.

   progressDeadlineSeconds<integer>
     The maximum time in seconds for a deployment to make progress before it is
     considered to be failed. The deployment controller will continue to process
     failed deployments and a condition with a ProgressDeadlineExceeded reason
     will be surfaced in the deployment status. Note that progress will not be
     estimated during the time a deployment is paused. Defaults to 600s.

   replicas<integer>
     Number of desired pods. This is a pointer to distinguish between explicit
     zero and not specified. Defaults to 1.

   revisionHistoryLimit<integer>
     The number of old ReplicaSets to retain to allow rollback. This is a
     pointer to distinguish between explicit zero and not specified. Defaults to
     10.

   selector <Object> -required-
     Label selector for pods. Existing ReplicaSets whose pods are selected by
     this will be the ones affected by this deployment. It must match the pod
     template's labels.

   strategy<Object>
     The deployment strategy to use to replace existing pods with new ones.

   template <Object> -required-
     Template describes the pods that will be created.

[[email protected] ~]#
View pod
[[email protected] ~/yaml/test]# kubectl get pod 
NAME                                     READY   STATUS        RESTARTS     AGE
ingress-demo-app-694bf5d965-q4l7m        1/1     Terminating   0            23h
ingress-demo-app-694bf5d965-v28sl        1/1     Running       0            3m9s
ingress-demo-app-694bf5d965-v652j        1/1     Running       0            23h
nfs-client-provisioner-dc5789f74-nnk77   1/1     Running       1 (8h ago)   22h
nginx-deployment-66b6c48dd5-5hhjq        1/1     Running       0            3m9s
nginx-deployment-66b6c48dd5-9z2n5        1/1     Running       0            3m19s
nginx-deployment-66b6c48dd5-llq7c        1/1     Running       0            9m10s
[[email protected] ~/yaml/test]#
View deployments
[[email protected] ~/yaml/test]# kubectl get deployments.apps 
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
ingress-demo-app         2/2     2            2           23h
nfs-client-provisioner   1/1     1            1           22h
nginx-deployment         3/3     3            3           9m45s
[[email protected] ~/yaml/test]#
Explanation:

When you check deployments in a cluster, the following fields are displayed:

Name lists the names of deployments in the cluster.

Detailed displays the required number of copies of the application, which are defined when the deployment is created. This is the desired state.

Current displays the number of copies currently running.

Up-to-date displays the number of copies that have been updated to achieve the desired state.

Available displays the number of copies of the application available to users.

Age displays the amount of time the application has been running.

View RS
[[email protected] ~/yaml/test]# kubectl get replicasets.apps 
NAME                               DESIRED   CURRENT   READY   AGE
ingress-demo-app-694bf5d965        2         2         2       23h
nfs-client-provisioner-dc5789f74   1         1         1       23h
nginx-deployment-66b6c48dd5        3         3         3       19m
[[email protected] ~/yaml/test]#
View the labels of pods
[[email protected] ~/yaml/test]# kubectl get pods --show-labels
NAME                                     READY   STATUS        RESTARTS     AGE   LABELS
ingress-demo-app-694bf5d965-q4l7m        1/1     Terminating   0            23h   app=ingress-demo-app,pod-template-hash=694bf5d965
ingress-demo-app-694bf5d965-v28sl        1/1     Running       0            15m   app=ingress-demo-app,pod-template-hash=694bf5d965
ingress-demo-app-694bf5d965-v652j        1/1     Running       0            23h   app=ingress-demo-app,pod-template-hash=694bf5d965
nfs-client-provisioner-dc5789f74-nnk77   1/1     Running       1 (8h ago)   23h   app=nfs-client-provisioner,pod-template-hash=dc5789f74
nginx-deployment-66b6c48dd5-48k9j        0/1     Terminating   0            21m   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-5hhjq        1/1     Running       0            15m   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-9z2n5        1/1     Running       0            15m   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-kvzft        0/1     Terminating   0            21m   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-llq7c        1/1     Running       0            21m   app=nginx,pod-template-hash=66b6c48dd5
[[email protected] ~/yaml/test]#
2) Update rollback deployment
Using the mirror upgrade command line
[[email protected] ~/yaml/test]# kubectl get deployments -o wide
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS               IMAGES                                                                                    SELECTOR
ingress-demo-app         2/2     2            2           23h   whoami                   traefik/whoami:v1.6.1                                                                     app=ingress-demo-app
nfs-client-provisioner   1/1     1            1           23h   nfs-client-provisioner   registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2   app=nfs-client-provisioner
nginx-deployment         3/3     3            3           18m   nginx                    nginx:1.14.2                                                                              app=nginx
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# kubectl --record deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.20.1
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx-deployment image updated
deployment.apps/nginx-deployment image updated
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# kubectl get deployments -o wide
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS               IMAGES                                                                                    SELECTOR
ingress-demo-app         2/2     2            2           23h   whoami                   traefik/whoami:v1.6.1                                                                     app=ingress-demo-app
nfs-client-provisioner   1/1     1            1           23h   nfs-client-provisioner   registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2   app=nfs-client-provisioner
nginx-deployment         3/3     1            3           24m   nginx                    nginx:1.20.1                                                                              app=nginx
[[email protected] ~/yaml/test]#
Modifying images in yaml mode
[[email protected] ~/yaml/test]# kubectl edit deployments.apps nginx-deployment
Edit cancelled, no changes made.
[[email protected] ~/yaml/test]#
View the update process
[[email protected] ~/yaml/test]# kubectl rollout status deployment.v1.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
[[email protected] ~/yaml/test]#
View details
[[email protected] ~/yaml/test]# kubectl describe deployments
Name:                   ingress-demo-app
Namespace:              default
CreationTimestamp:      Tue, 16 Nov 2021 13:28:26 +0800
Labels:                 app=ingress-demo-app
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=ingress-demo-app
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=ingress-demo-app
  Containers:
   whoami:
    Image:        traefik/whoami:v1.6.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   ingress-demo-app-694bf5d965 (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  23h   deployment-controller  Scaled up replica set ingress-demo-app-694bf5d965 to 2


Name:               nfs-client-provisioner
Namespace:          default
CreationTimestamp:  Tue, 16 Nov 2021 14:07:33 +0800
Labels:             app=nfs-client-provisioner
Annotations:        deployment.kubernetes.io/revision: 1
Selector:           app=nfs-client-provisioner
Replicas:           1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:       Recreate
MinReadySeconds:    0
Pod Template:
  Labels:           app=nfs-client-provisioner
  Service Account:  nfs-client-provisioner
  Containers:
   nfs-client-provisioner:
    Image:      registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2
    Port:       <none>
    Host Port:  <none>
    Environment:
      PROVISIONER_NAME:  k8s-sigs.io/nfs-subdir-external-provisioner
      NFS_SERVER:        192.168.1.66
      NFS_PATH:          /nfs/
    Mounts:
      /persistentvolumes from nfs-client-root (rw)
  Volumes:
   nfs-client-root:
    Type:      NFS (an NFS mount that lasts the lifetime of a pod)
    Server:    192.168.1.66
    Path:      /nfs/
    ReadOnly:  false
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nfs-client-provisioner-dc5789f74 (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  23h   deployment-controller  Scaled up replica set nfs-client-provisioner-dc5789f74 to 1


Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Wed, 17 Nov 2021 12:54:46 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 3
                        kubernetes.io/change-cause:
                          kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.20.1 --record=true
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.16.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-559d658b74 (3/3 replicas created)
Events:
  Type    Reason             Age                From                   Message
  ----    ------             ----               ----                   -------
  Normal  ScalingReplicaSet  30m                deployment-controller  Scaled up replica set nginx-deployment-66b6c48dd5 to 3
  Normal  ScalingReplicaSet  5m55s              deployment-controller  Scaled up replica set nginx-deployment-58b9b8ff79 to 1
  Normal  ScalingReplicaSet  5m27s              deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 2
  Normal  ScalingReplicaSet  5m27s              deployment-controller  Scaled up replica set nginx-deployment-58b9b8ff79 to 2
  Normal  ScalingReplicaSet  5m                 deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 1
  Normal  ScalingReplicaSet  5m                 deployment-controller  Scaled up replica set nginx-deployment-58b9b8ff79 to 3
  Normal  ScalingReplicaSet  4m56s              deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 0
  Normal  ScalingReplicaSet  78s                deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 1
  Normal  ScalingReplicaSet  63s                deployment-controller  Scaled down replica set nginx-deployment-58b9b8ff79 to 2
  Normal  ScalingReplicaSet  63s                deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 2
  Normal  ScalingReplicaSet  49s (x3 over 61s)  deployment-controller  (combined from similar events): Scaled down replica set nginx-deployment-58b9b8ff79 to 0
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]#
3) Deployment history
View history
[[email protected] ~/yaml/test]# kubectl rollout history deployment.v1.apps/nginx-deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.20.1 --record=true
3         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.20.1 --record=true

[[email protected] ~/yaml/test]#
Rollback to last
[[email protected] ~/yaml/test]# kubectl rollout undo deployment.v1.apps/nginx-deployment
deployment.apps/nginx-deployment rolled back
[[email protected] ~/yaml/test]# kubectl rollout status deployment.v1.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
[[email protected] ~/yaml/test]#
Rollback to the specified version
[[email protected] ~/yaml/test]# kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=3
deployment.apps/nginx-deployment rolled back
[[email protected] ~/yaml/test]# kubectl rollout status deployment.v1.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
[[email protected] ~/yaml/test]#
4) Zoom deployment
Expand to ten pods
[[email protected] ~/yaml/test]# kubectl scale deployment.v1.apps/nginx-deployment --replicas=10
deployment.apps/nginx-deployment scaled
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]# kubectl  get deployments.apps
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
ingress-demo-app         0/2     2            0           24h
nfs-client-provisioner   0/1     1            0           23h
nginx-deployment         5/10    10           5           45m
[[email protected] ~/yaml/test]#

Assuming that the horizontal autoscale pod is enabled, in a cluster, you can set the autoscaler for deployment and select the minimum and maximum pods to run based on the CPU utilization of existing pods.

[[email protected] ~/yaml/test]# kubectl autoscale deployment.v1.apps/nginx-deployment --min=10 --max=15 --cpu-percent=80
horizontalpodautoscaler.autoscaling/nginx-deployment autoscaled
[[email protected] ~/yaml/test]# 
[[email protected] ~/yaml/test]#

https://blog.csdn.net/qq_3392…

https://my.oschina.net/u/3981543

https://www.zhihu.com/people/…

https://segmentfault.com/u/hp…

https://juejin.cn/user/331578…

https://space.bilibili.com/35…

https://cloud.tencent.com/dev…

Zhihu, CSDN, open source China, Sifu, Nuggets, BiliBili, Tencent cloud

This article usesArticle synchronization assistantsynchronization