Deployment

一、简介

Deployment实际上是一个两层控制器,遵循一种滚动更新的方式来实现升级现有的容器。Deployment在内部使用了ReplicaSet来实现目的,无论冲Deployment的作用与目的、YAML定义、还是从它的具体命令操作来看,它都是对RC的一个升级。

Deployment的典型应用场景有以下几个:

  • 创建一个Deployment对象来生成对应的Replica Set 并完成Pod副本的创建

  • 检查Deployment的状态来看部署动作是否完成(Pod副本数量是否达到预期的值)

  • 更新Deployment以创建新的Pod(比如镜像升级)

  • 如果当前Deployment不稳定,则回滚到一个早先的Deployment版本

  • 暂停Deployment以便于一次性修改多个PodTemplateSpec的配置项,之后再恢复Deployment,进行更新发布

  • 扩展Deployment以应对高负载

  • 查看Deployment的状态,以此作为发布是否成功的指标

  • 清理不再需要的旧版本ReplicaSets

 

二、创建Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-web
        image: nginx:1.14.2
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80

 

在该例中:

  • 将创建一个名为nginx-deployment的Deployment,由 .metadata.name 字段指明

  • Deployment将创建2个Pod副本,由 .spec.replicas 字段指明

  • .spec.selector 字段定义Deployment如何查找要管理的Pods。

  • template.spec 字段指示Pods运行一个容器, nginx

  • 创建一个容器并使用 name 字段将其命名为 nginx-web

 

2.1 运行Deployment

# kubectl apply -f nginx-deployment.yaml

查看Deployment信息

# kubectl get deployments
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           10m
​
# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-559954d9f9   2         2         2       11m
# kubectl describe deployment/nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 09 Jun 2020 13:51:00 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 3
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-web:
    Image:        nginx:1.19.0
    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-798656bd4b (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  27m   deployment-controller  Scaled up replica set nginx-deployment-674964b5cc to 1
  Normal  ScalingReplicaSet  26m   deployment-controller  Scaled down replica set nginx-deployment-674964b5cc to 0
  Normal  ScalingReplicaSet  26m   deployment-controller  Scaled up replica set nginx-deployment-798656bd4b to 1
  Normal  ScalingReplicaSet  23m   deployment-controller  Scaled down replica set nginx-deployment-559954d9f9 to 1
  Normal  ScalingReplicaSet  23m   deployment-controller  Scaled up replica set nginx-deployment-798656bd4b to 2
  Normal  ScalingReplicaSet  19m   deployment-controller  Scaled down replica set nginx-deployment-559954d9f9 to 0
You have new mail in /var/spool/mail/root
 

2.2 Pod镜像更新

  1. 命令行更新

# kubectl set image deployment/nginx-deployment nginx=nginx:1.19.0 --record=true

 

  1. kubectl edit

# kubectl edit deployment/nginx-deployment

 

  1. 修改deployment YAML文件

 

2.3 回滚

1.返回上一个版本

# kubectl rollout undo deployment/nginx-deployment

 

2.返回指定版本

使用--record=true参数时,就能在CHANGE-CAUSE中看到每个版本使用的命令
# 先查看更新记录
#kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>
如果要查看每个更新的详细信息
# kubectl rollout history deployment/nginx-deployment --revision=3
deployment.apps/nginx-deployment with revision #3
Pod Template:
  Labels:    app=nginx
    pod-template-hash=798656bd4b
  Containers:
   nginx-web:
    Image:    nginx:1.19.0
    Port:    80/TCP
    Host Port:    0/TCP
    Environment:    <none>
    Mounts:    <none>
  Volumes:    <none>

 回滚到指定版本

#kubectl rollout undo deployment/nginx-deployment --to-revision=1

 

2.4 暂停和恢复Deployment部署操作

对于一次复杂的Deployment配置修改,为了避免频繁的触发Deployment的更新操作,可以先暂停Deployment的更新操作,然后进行配置修改,在恢复Deployment,一次性触发完整的更新操作。

以之前创建的nginx为例

# kubectl get deployments
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           100m

 

  1. 通过kubectl rollout pause 命令暂停Deployment的更新操作

    # kubectl rollout pause deployment/nginx-deployment
    deployment.apps/nginx-deployment paused

     

  2. 然后修改Deployment的镜像信息

    # kubectl set image deployment/nginx-deployment nginx-web=nginx:1.18.0 --record=true
    deployment.apps/nginx-deployment image updated
    # kubectl rollout history deployment/nginx-deployment
    deployment.apps/nginx-deployment 
    REVISION  CHANGE-CAUSE
    1         <none>
    2         <none>
    3         kubectl set image deployment/nginx-deployment nginx=nginx:1.18.0 --record=true

    查看具体pod中nginx的版本信息,其实nginx并没有更新为1.18.0

  3. 再次更新容器资源限制

    # kubectl set resources deployment/nginx-deployment -c nginx-web --limits=cpu=200m,memory=256Mi
    deployment.apps/nginx-deployment resource requirements updated

     

  4. 最后恢复Deployment的部署操作

    # kubectl rollout resume deployment/nginx-deployment
    deployment.apps/nginx-deployment resumed
    ​
    # kubectl get pods
    NAME                                READY   STATUS              RESTARTS   AGE
    mysql-lh5dv                         1/1     Running             5          3d22h
    myweb-nxn6s                         1/1     Running             5          3d21h
    nginx-deployment-74564bc45-6nmtq    0/1     ContainerCreating   0          7s
    nginx-deployment-798656bd4b-4z52s   1/1     Running             0          63m
    nginx-deployment-798656bd4b-lhpns   1/1     Running             0          66m
    volume-pod                          2/2     Running             2          22h

     

    其实能看到nginx在做更新操作了

 

 

 

 

posted @ 2020-08-12 11:35  Bigberg  阅读(370)  评论(0编辑  收藏  举报