当前位置: 首页 > 工具软件 > deployment > 使用案例 >

5.Deployment

全昊焜
2023-12-01

Deployment

  • 虽然ReplicaSet可以确保在任何给定时间运行的Pod副本达到指定的数量,但是Deployment(部署)是一个更高级的概念,它管理ReplicaSet并为Pod和ReplicaSet提供声明性更新以及许多其他有用的功能,所以建议在实际使用中,使用Deployment代替ReplicaSet。
  • 如果在Deployment对象中描述了所需的状态,Deployment控制器就会以可控制的速率将实际状态更改为期望状态。
  • 也可以在Deployment中创建新的ReplicaSet,或者删除现有的Deployment并使用新的Deployment部署所用的资源。

创建Deployment

  • 创建一个Deployment文件,并命名为dc-nginx.yaml,用于部署三个Nginx Pod:
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: workspace
  name: nginx-deployment  # Deployment的名称。
  labels:
    app: nginx
spec:
  replicas: 3   # 创建pod的副本数
  selector:    # 定义Deployment如何找到要管理的Pod,与template的label(标签)对应。
    matchLabels:
      app: nginx  # Pod的label标签
  template:
    metadata:
      labels:
        app: nginx
    spec:  # 表示Pod运行一个名字为nginx的容器
      containers:
      - name: nginx
        image: nginx:1.7.9   # 运行此pod使用的镜像
        ports:
        - containerPort: 80  # 容器用于发送和接收流量的端口
  • 使用kubectlcreate创建此Deployment
[root@master01 yaml]# kubectl create -f dc-nginx.yaml
deployment.apps/nginx-deployment created
  • 使用kubectl get或者kubectl describe查看此Deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           7m56s
  • 查看此时Deployment rollout的状态:
[root@master01 yaml]# kubectl rollout status deployment/nginx-deployment -n workspace
deployment "nginx-deployment" successfully rolled out
  • 查看此Deployment创建的ReplicaSet
[root@master01 yaml]# kubectl get rs -n workspace
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-5d59d67564   3         3         3       12m
  • 查看此Deployment创建的Pod
[root@master01 yaml]# kubectl get po -n workspace --show-labels
NAME                                READY   STATUS    RESTARTS   AGE   LABELS
nginx-deployment-5d59d67564-8cqb4   1/1     Running   0          14m   app=nginx,pod-template-hash=5d59d67564
nginx-deployment-5d59d67564-gltz8   1/1     Running   0          14m   app=nginx,pod-template-hash=5d59d67564
nginx-deployment-5d59d67564-p7trc   1/1     Running   0          14m   app=nginx,pod-template-hash=5d59d67564

更新Deployment

  • 一般对应用程序升级或者版本迭代时,会通过Deployment对Pod进行滚动更新。

  • 将nginx Pod的image版本更新到1.9.1

[root@master01 yaml]# kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 -n workspace --record
deployment.apps/nginx-deployment image updated
  • 当然可以直接编辑Deployment, 效果相同
[root@master01 yaml]# kubectl edit deploy -n workspace  nginx-deployment
  • 查看Deployment的更新状态
[root@master01 yaml]# kubectl  describe deployments.apps -n workspace nginx-deployment
Name:                   nginx-deployment
Namespace:              workspace
CreationTimestamp:      Fri, 20 May 2022 08:43:48 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 2
                        kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --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.9.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-5bfdf46dc6 (3/3 replicas created)
Events:
  Type     Reason                 Age    From                   Message
  ----     ------                 ----   ----                   -------
  Normal   ScalingReplicaSet      4d13h  deployment-controller  Scaled up replica set nginx-deployment-f7ccf9478 to 3
  Warning  ReplicaSetCreateError  2m10s  deployment-controller  Failed to create new replica set "nginx-deployment-5bfdf46dc6": Unauthorized
  Normal   ScalingReplicaSet      2m10s  deployment-controller  Scaled up replica set nginx-deployment-5bfdf46dc6 to 1
  Normal   ScalingReplicaSet      108s   deployment-controller  Scaled down replica set nginx-deployment-f7ccf9478 to 2
  Normal   ScalingReplicaSet      107s   deployment-controller  Scaled up replica set nginx-deployment-5bfdf46dc6 to 2
  Normal   ScalingReplicaSet      61s    deployment-controller  Scaled down replica set nginx-deployment-f7ccf9478 to 1
  Normal   ScalingReplicaSet      61s    deployment-controller  Scaled up replica set nginx-deployment-5bfdf46dc6 to 3
  Normal   ScalingReplicaSet      37s    deployment-controller  Scaled down replica set nginx-deployment-f7ccf9478 to 0

  • 查看Deployment更新历史
[root@master01 ~]# kubectl rollout history -n workspace deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
3         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
4         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
5         kubectl set image deployment nginx-deployment nginx=nginx:1.10.3 --namespace=workspace --record=true
  • 查看Deployment某次更新的详细信息,使用–revision指定版本号
[root@master01 ~]# kubectl rollout history -n workspace deployment/nginx-deployment --revision=2
deployment.apps/nginx-deployment with revision #2
Pod Template:
  Labels:       app=nginx
        pod-template-hash=69c44dfb78
  Annotations:  kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
  Containers:
   nginx:
    Image:      nginx:1.9.1
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

更新回滚

  • 使用–to-revision参数回到指定版本
[root@master01 ~]# kubectl rollout undo -n workspace deployment/nginx-deployment --to-revision=2
deployment.apps/nginx-deployment rolled back
  • 查看更新历史, revision 2 更改为了最新的7
[root@master01 ~]# kubectl rollout history -n workspace deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
3         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
4         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
5         kubectl set image deployment nginx-deployment nginx=nginx:1.10.3 --namespace=workspace --record=true
6         kubectl set image deployment nginx-deployment nginx=nginx:1.10.4 --namespace=workspace --record=true
7         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
  • 查看Pod的镜像版本
[root@master01 ~]# kubectl describe po -n workspace  nginx-deployment-69c44dfb78-zv9vl | grep Image
    Image:          nginx:1.9.1
    Image ID:       docker-pullable://nginx@sha256:2f68b99bc0d6d25d0c56876b924ec20418544ff28e1fb89a4c27679a40da811b

扩展Deployment

  • 当公司访问量变大,三个Pod已无法支撑业务时,可以对其进行扩展。
  • - 使用kubectl scale动态调整Pod的副本数,比如增加Pod为5个:
[root@master01 ~]# kubectl scale -n workspace deployment/nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled
[root@master01 ~]# kubectl get po -n workspace
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-69c44dfb78-pjkwb   1/1     Running   0          6m3s
nginx-deployment-69c44dfb78-shw9d   1/1     Running   0          33s
nginx-deployment-69c44dfb78-tgsmn   1/1     Running   0          33s
nginx-deployment-69c44dfb78-v4mxp   1/1     Running   0          6m5s
nginx-deployment-69c44dfb78-zv9vl   1/1     Running   0          6m1s

暂停和恢复Deployment更新

  • Deployment支持暂停更新,用于对Deployment进行多次修改操作。

- 使用kubectl rollout pause暂停Deployment更新:

[root@master01 ~]# kubectl rollout pause deployment/nginx-deployment -n workspace
deployment.apps/nginx-deployment paused

- 然后对Deployment进行相关更新操作,比如更新镜像,然后对其资源进行限制:

[root@master01 ~]# kubectl set image -n workspace deployment/nginx-deployment nginx=nginx:1.11.1
deployment.apps/nginx-deployment image updated
[root@master01 ~]# kubectl set resources -n workspace deployment/nginx-deployment -c=nginx --limits=cpu=500m,memory=1Gi --requests=cpu=100m,memory=64Mi
deployment.apps/nginx-deployment resource requirements updated

- 通过rollout history可以看到没有新的更新:

[root@master01 ~]# kubectl rollout history deployment/nginx-deployment -n workspace
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
3         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
4         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true
5         kubectl set image deployment nginx-deployment nginx=nginx:1.10.3 --namespace=workspace --record=true
6         kubectl set image deployment nginx-deployment nginx=nginx:1.10.4 --namespace=workspace --record=true
7         kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --namespace=workspace --record=true

- 使用kubectl rollout resume恢复Deployment更新:

[root@master01 ~]# kubectl rollout resume deployment/nginx-deployment -n workspace
deployment.apps/nginx-deployment resumed

- 可以查看到恢复更新的Deployment创建了一个新的RS(复制集):

[root@master01 ~]# kubectl get rs -n workspace
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-55dcddcb7    0         0         0       16m
nginx-deployment-5c6ccdb5db   0         0         0       24m
nginx-deployment-5d59d67564   0         0         0       8h
nginx-deployment-645d9d5cd7   0         0         0       31m
nginx-deployment-69c44dfb78   0         0         0       8h
nginx-deployment-6f7f5f76dc   5         5         5       65s
nginx-deployment-87f7bfb9     0         0         0       29m

-可以查看Deployment的image(镜像)已经变为nginx:1.11.1

[root@master01 ~]# kubectl describe deploy nginx-deployment -n workspace | grep Image
    Image:      nginx:1.11.1

更新Deployment的注意事项

  • 清理策略:
    • 在默认情况下,revision保留10个旧的ReplicaSet,其余的将在后台进行垃圾回收,可以在.spec.revisionHistoryLimit设置保留ReplicaSet的个数。当设置为0时,不保留历史记录。
  • 更新策略:
    • .spec.strategy.type==Recreate,表示重建,先删掉旧的Pod再创建新的Pod。
    • .spec.strategy.type==RollingUpdate,表示滚动更新,可以指定maxUnavailable和maxSurge来控制滚动更新过程。
    • .spec.strategy.rollingUpdate.maxUnavailable,指定在回滚更新时最大不可用的Pod数量,可选字段,默认为25%,可以设置为数字或百分比,如果maxSurge为0,则该值不能为0。
    • .spec.strategy.rollingUpdate.maxSurge可以超过期望值的最大Pod数,可选字段,默认为25%,可以设置成数字或百分比,如果maxUnavailable为0,则该值不能为0。
 类似资料: