K8SDeployment

Posted lqbyz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K8SDeployment相关的知识,希望对你有一定的参考价值。

一、概述

Deployment对象,是用于部署应用的对象,它是K8S中最常用的一个对象,它为ReplicaSet和Pod的创建提供了一种声明式的定义方法,从而无需手动创建RS和pod 对象,使用Deployment而不直接创建RS是因为Deployment对象拥有许多RS没有的特性,如滚动升级和回滚等。

(二)、Deployment的创建和更新

最简单的Deployment应用如下:

[root@k8s-master deployment]# cat 2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # 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
[root@k8s-master deployment]# kubectl apply -f 2.yml
deployment.apps/nginx-deployment created
[root@k8s-master deployment]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           55s
root@k8s-master deployment]# kubectl describe nginx-deployment
error: the server doesnt have a resource type "nginx-deployment"
[root@k8s-master deployment]# kubectl describe deployment/nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 28 Dec 2021 02:57:29 -0500
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
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=nginx
  Containers:
   nginx:
    Image:        nginx:1.14.2
    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-66b6c48dd5 (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  99s   deployment-controller  Scaled up replica set nginx-deployment-66b6c48dd5 to 2
[root@k8s-master deployment]# kubectl get pods,rs -l app=nginx
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-5vxzv                         1/1     Running   0          8d
pod/nginx-6m64m                         1/1     Running   0          8d
pod/nginx-9f8n8                         1/1     Running   0          8d
pod/nginx-deployment-66b6c48dd5-cpd7h   1/1     Running   0          14m
pod/nginx-deployment-66b6c48dd5-hxnl2   1/1     Running   0          14m

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-66b6c48dd5   2         2         2       14m

(三)、Deployment的更新

可以通过更新一个新的 YAML 文件来更新 Deployment。下面的 YAML 文件指定该 Deployment 镜像更新为 nginx 1.16.1。

3.1、通过更新yml文件进行更新

[root@k8s-master deployment]# cat 2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1
        ports:
        - containerPort: 80

[root@k8s-master deployment]# kubectl apply -f 2.yml
deployment.apps/nginx-deployment configured
[root@k8s-master deployment]# kubectl get pods,rs -l app=nginx
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-5vxzv                         1/1     Running   0          8d
pod/nginx-6m64m                         1/1     Running   0          8d
pod/nginx-9f8n8                         1/1     Running   0          8d
pod/nginx-deployment-559d658b74-49qvd   1/1     Running   0          5m52s
pod/nginx-deployment-559d658b74-b9g49   1/1     Running   0          5m52s
pod/nginx-deployment-559d658b74-kgnr6   1/1     Running   0          6m24s

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-559d658b74   3         3         3       6m45s
replicaset.apps/nginx-deployment-5d7bb69767   0         0         0       3m32s
replicaset.apps/nginx-deployment-66b6c48dd5   0         0         0       24m

3.2、通过命令进行更新,一种通过set,另一种通过edit命令进行编辑

[root@k8s-master deployment]# kubectl set image deployment/nginx-deployment nginx=1.9.1
deployment.apps/nginx-deployment image updated
[root@k8s-master deployment]# kubectl edit deployment/nginx-deployment
deployment.apps/nginx-deployment edited
[root@k8s-master deployment]# kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
[root@k8s-master deployment]# kubectset image deployment/nginx-deployment nginx=nginx:1.15.1
deployment.apps/nginx-deployment image updated

####查看更新的进度
[root@k8s-master deployment]# kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 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...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 3 of 4 updated replicas are available...
deployment "nginx-deployment" successfully rolled out

(四)、Deployment的回滚

如果发现nginx的1.16.1版本不稳定,可以回滚到1.14.1版本中,此时不需要手动更改deployment文件,而可以利用deployment的rollout回滚功能。

4.1、查看历史版本

[root@k8s-master deployment]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
3         <none>
4         <none>
5         <none>
7         <none>
8         <none>
9         <none>
10        <none>

[root@k8s-master deployment]# kubectl rollout history deployment/nginx-deployment --revision=7
deployment.apps/nginx-deployment with revision #7
Pod Template:
Labels: app=nginx
pod-template-hash=79dcf65f46
Containers:
nginx:
Image: 1.14.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>

> 发下版本7的nginx是1.14.1,可以通过rollout undo来回滚到指定的版本中
### 4.3、回滚指定的版本
```python
[root@k8s-master deployment]# kubectl rollout undo deployment/nginx-deployment --to-revision=7
deployment.apps/nginx-deployment rolled back
[root@k8s-master deployment]# kubectl describe deployment/nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 28 Dec 2021 02:57:29 -0500
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 11
Selector:               app=nginx
Replicas:               4 desired | 2 updated | 5 total | 3 available | 2 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        1.14.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  nginx-deployment-69cc985499 (3/3 replicas created)
NewReplicaSet:   nginx-deployment-79dcf65f46 (2/2 replicas created)
Events:
  Type    Reason             Age                 From                   Message
  ----    ------             ----                ----                   -------
  Normal  ScalingReplicaSet  54m                 deployment-controller  Scaled up replica set nginx-deployment-66b6c48dd5 to 2
  Normal  ScalingReplicaSet  37m                 deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 1
  Normal  ScalingReplicaSet  36m                 deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 1
  Normal  ScalingReplicaSet  36m                 deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 2
  Normal  ScalingReplicaSet  36m                 deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 0
  Normal  ScalingReplicaSet  33m                 deployment-controller  Scaled up replica set nginx-deployment-5d7bb69767 to 1
  Normal  ScalingReplicaSet  30m                 deployment-controller  Scaled down replica set nginx-deployment-5d7bb69767 to 0
  Normal  ScalingReplicaSet  20m                 deployment-controller  Scaled up replica set nginx-deployment-698694d5d4 to 1
  Normal  ScalingReplicaSet  20m (x2 over 33m)   deployment-controller  Scaled down replica set nginx-deployment-559d658b74 to 3
  Normal  ScalingReplicaSet  20m (x2 over 36m)   deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 4
  Normal  ScalingReplicaSet  18m                 deployment-controller  Scaled up replica set nginx-deployment-59dd995d49 to 2
  Normal  ScalingReplicaSet  17m (x15 over 20m)  deployment-controller  (combined from similar events): Scaled down replica set nginx-deployment-559d658b74 to 0
  Normal  ScalingReplicaSet  32s                 deployment-controller  Scaled up replica set nginx-deployment-79dcf65f46 to 1
  Normal  ScalingReplicaSet  32s                 deployment-controller  Scaled down replica set nginx-deployment-69cc985499 to 3
  Normal  ScalingReplicaSet  32s                 deployment-controller  Scaled up replica set nginx-deployment-79dcf65f46 to 2

总结:

  • .spec.minReadySeconds: 新创建的Pod状态为Ready持续的时间至少为.spec.minReadySeconds才认为Pod Available(Ready)。
  • .spec.strategy.rollingUpdate.maxSurge: specifies the maximum number of Pods that can be created over the desired number of Pods. The value cannot be 0 if MaxUnavailable is 0. 可以为整数或者百分比,默认为desired Pods数的25%. Scale Up新的ReplicaSet时,按照比例计算出允许的MaxSurge,计算时向上取整(比如3.4,取4)。
  • .spec.strategy.rollingUpdate.maxUnavailable: specifies the maximum number of Pods that can be unavailable during the update process. The value cannot be 0 if maxSurge is 0.可以为整数或者百分比,默认为desired Pods数的25%. Scale Down旧的ReplicaSet时,按照比例计算出允许的maxUnavailable,计算时向下取整(比如3.6,取3)。
    因此,在Deployment rollout时,需要保证Available(Ready) Pods数不低于 desired pods number - maxUnavailable; 保证所有的Pods数不多于 desired pods number + maxSurge。

以上是关于K8SDeployment的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数