15.Deployment相关的实验

创建Deployment

  1. 使用如下配置文件创建资源:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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.7.9
        ports:
        - containerPort: 80

可以为该命令增加--record选项,此时kubectl会将kubectl apply -f deployment1.yaml --record写入Deployment的注解kubernetes.io/change-cause中。这样,这样可以在将来就可以回顾某一个 Deployment 版本变化的原因。

我实验了--record选项,其表现如下,我觉得意义不大:

1
2
3
4
5
6
7
8

metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
    kubectl.kubernetes.io/last-applied-configuration: |
            {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=deployment1.yaml --record=true"},"labels":{"app":"nginx"},"name":"nginx-deployment","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx:1.7.9","name":"nginx","ports":[{"containerPort":80}]}]}}}}
    kubernetes.io/change-cause: kubectl apply --filename=deployment1.yaml --record=true

  1. 执行如下基本操作,查看deployment资料
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

# 查看创建情况
kubectl get deployments
kubectl get rs
kubectl get pods --show-labels

# 查看发布状态
kubectl rollout status deployment/nginx-deployment
kubectl rollout status deployment.v1.apps/nginx-deployment



更新Deployment

当且仅当Deployment的Pod Template字段中的内容发生变更时(例如标签、容器的镜像),Deployment的rollout将被触发。

  1. 修改容器镜像
1
2
3
4
5
6
7

# 这条指令我现在还不理解
kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1

# 编辑Deployment,修改image从nginx:1.7.9到nginx:1.9.1
kubectl edit deployment.v1.apps/nginx-deployment

  1. 查看发布更新的状态
1
2
3

kubectl rollout status deployment.v1.apps/nginx-deployment

  1. 查看各个资源的状态
1
2
3
4
5

kubectl get deployments
kubectl get rs
kubectl get pods


回滚Deployment

默认情况下,kubernetes将保存Deployment的所有更新历史,可以设定revision history limit来确定保存的历史版本数量。

  1. 模拟滚动更新操作失误(将1.9.1写成了1.91):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10

kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.91

# 查看发布状态
kubectl rollout status deployment.v1.apps/nginx-deployment

# 查看各个资源
kubectl get rs
kubectl get pods

  1. 检查Deployment的更新历史

kubectl rollout history deployment.v1.apps/nginx-deployment

  1. 回滚到前一个版本或指定某个版本
1
2
3
4

kubectl rollout undo deployment.v1.apps/nginx-deployment
kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=2 

  1. 检查资源状态

kubectl get deployment nginx-deployment
kubectl describe deployment nginx-deployment

关于CHANGE-CAUSE:执行kubectl rollout history deployment.v1.apps/nginx-deployment时,返回结果如下:


deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
2         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1 --record=true
4         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.7.9 --record=true
5         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.7.9 --record=true

CHANGE-CAUSE列是该revision创建时从Deployment的kubernetes.io/change-cause注解拷贝而来,可以通过如下方式制定CHANGE-CAUSE

  1. 为Deployment增加注解:kubectl annotate deployment.v1.apps/nginx-deployment kubernetes.io/change-cause="image updated to 1.9.1"

  2. 执行 kubectl apply 命令时,增加--record选项

  3. 手动编辑Deployment的metadata.annotation信息

(我为什么要记录CHANGE-CAUSE列呢,因为我不知道生产实践中有没有人利用这个字段记录一些版本信息。)


伸缩Deployment

  1. 执行指令如下:

kubectl scale deployment.v1.apps/nginx-deployment --replicas=10

  1. 如果集群启用了HPA(这个还不太熟悉),则执行以下指令可以基于CPU的利用率在一个最大和最小的区间自动伸缩:

kubectl autoscale deployment.v1.apps/nginx-deployment --min=10 --max=15 --cpu-percent=80

暂停和继续Deployment

可以先暂停Deployment,然后再触发一个或多个更新,最后在恢复该Deployment。这种做法使得您可以在暂停时对Deployment做多次更新,而无需触发不必要的滚动更新。

执行如下指令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 暂停Deployment
kubectl rollout pause deployment.v1.apps/nginx-deployment

# 编辑Deployment
kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1

# 可以看到尚未生成新的Deployment版本
kubectl rollout history deployment.v1.apps/nginx-deployment

# 可以查看到没有新的滚动更新开始执行
kubectl get rs

# 再次更新
kubectl set resources deployment.v1.apps/nginx-deployment -c=nginx --limits=cpu=200m,memory=512Mi

# 查看Deployment定义,确保Deployment已被修改
kubectl describe deployment nginx-deployment

# 恢复Deployment
kubectl rollout resume deployment.v1.apps/nginx-deployment

不能回滚一个已暂停的Deployment,除非先恢复这个Deployment。