linuxea:kubernetes pod控制器Deployment(10)


在前面一篇中提到过ReplicaSet,而ReplicaSet很多时候是无法满足现状,ReplicaSet确保在任何给定时间运行指定数量的pod副本。但是,Deployment是一个更高级别的概念
replicaset本身仅仅并不是直接被控制,在replicaset之上是由Deployment来控制,Deployment通过控制replicaset来控制pod。
Deployment支持扩缩容,滚动更新,回滚等,并且提供声明式配置功能,可随时声明,声明后可改变apiserver上期望的目标状态。只需要支持动态资源修改!
Deployment善于管控无状态应用(关注群体,不关注个体)

在Deployment中,Deployment控制两个(replicaset)rs,刚开始在v1 rs删除一个,并在v2 rs新建一个,紧接着在v1 在删除一个,在v2在创建一个,直到将v1全部迁移到v2上。如果想滚回v1,也可。如下图:

不但如此,Deployment可以控制更新节奏和更新逻辑来实现更新场景。如:灰度,蓝绿,金丝雀

如上图所示,倘若3个pod正好符合当前业务场景负载,此刻删除则会影响,那么就需要临时加一个pod,而后在删除一个。以最多多少个,最少多少个进行控制。

I. explain

strategy : 定义更新策略
​ Recreate: 重建更新(删掉就重建,依次重复)
​ rollingUpdate:滚动更新
如果type等于RollingUpdate,则可以使用RollingUpdate来进行定义
如果type等于Recreate,则RollingUpdate失效
查看详情

[root@linuxea linuxea]# kubectl describe deploy dpment-linuxea
Name:                   dpment-linuxea
Namespace:              default
CreationTimestamp:      Fri, 31 Aug 2018 14:23:33 +0100
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision=1
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"dpment-linuxea","namespace":"default"},"spec":{"replicas":3,"selector"...
Selector:               app=linuxea_app,version=v0.1.32
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge

RollingUpdate可控制更新粒度
​ maxSurge: 对应的更新过程当中,最多能超出指定目标数
​ 取值方式:数量和百分比(如:原本5个,20%就是1个,40%就是2个)
maxUnavilable:最多有几个不可用(如:原本5个,最多有1个不可用,那就意味着,最少有4个可用)
revisionHistoryLimit:在滚动更新后,保存多少个历史版本,默认10个,设置为0不保存
paused : 暂停,不更新
其中,replica sets上下共有三级结构,如下:

II. apply声明创建

[root@linuxea linuxea]# cat deploy-demt.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dpment-linuxea
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: linuxea_app
      version: v0.1.32
  template:
    metadata:
      labels:
        app: linuxea_app
        version: v0.1.32
    spec:
      containers:
      - name: nginx-a
        image: marksugar/nginx:1.14.b
        ports:
        - name: http
          containerPort: 80

创建

[root@linuxea linuxea]# kubectl apply -f deploy-demt.yaml 
deployment.apps/dpment-linuxea created
[root@linuxea linuxea]# kubectl get deploy
NAME             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
dpment-linuxea   2         2         2            2           31s
[root@linuxea linuxea]# kubectl get rs
NAME                        DESIRED   CURRENT   READY     AGE
dpment-linuxea-65b8d94cc6   2         2         2         45s

在dpment-linuxea-65b8d94cc6中的65b8d94cc6是模板的哈希值(且每一个模板的哈希值都不一样),dpment-linuxea是name

而在pod中的name段dpment-linuxea-65b8d94cc6-q7hw8,dpment-linuxea-65b8d94cc6-已经作为前缀

[root@linuxea linuxea]# kubectl get pods
NAME                              READY     STATUS    RESTARTS   AGE
client-linuxea                    1/1       Running   0          7d
dpment-linuxea-65b8d94cc6-q7hw8   1/1       Running   0          3m
dpment-linuxea-65b8d94cc6-tvn2d   1/1       Running   0          3m

倘若现在想扩展,直接修改yaml文件(直接vim打开,为了清楚的记录我修改的值,直接sed)

[root@linuxea linuxea]# sed -i 's/replicas: 2/replicas: 3/g' deploy-demt.yaml

apply是可以执行多次的。每次的变化会同步到etcd,apiserver发现与etcd不同,更新状态现有状态到预期状态。

[root@linuxea linuxea]# kubectl apply -f deploy-demt.yaml 
deployment.apps/dpment-linuxea configured
[root@linuxea linuxea]# kubectl get rs
NAME                        DESIRED   CURRENT   READY     AGE
dpment-linuxea-65b8d94cc6   3         3         3         10m
linuxea.com                 5         5         5         1d
nginx-linuxea-5786698598    3         3         3         5d
[root@linuxea linuxea]# kubectl get deploy
NAME             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
dpment-linuxea   3         3         3            3           10m
nginx-linuxea    3         3         3            3           5d
[root@linuxea linuxea]# kubectl get pods
NAME                              READY     STATUS    RESTARTS   AGE
client-linuxea                    1/1       Running   0          7d
dpment-linuxea-65b8d94cc6-kcckt   1/1       Running   0          23s
dpment-linuxea-65b8d94cc6-q7hw8   1/1       Running   0          11m
dpment-linuxea-65b8d94cc6-tvn2d   1/1       Running   0          11m

查看详情kubectl describe deploy dpment-linuxea,其中
StrategyType默认为RollingUpdate

[root@linuxea linuxea]# kubectl describe deploy dpment-linuxea
Name:                   dpment-linuxea
Namespace:              default
CreationTimestamp:      Fri, 31 Aug 2018 14:23:33 +0100
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision=1
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"dpment-linuxea","namespace":"default"},"spec":{"replicas":3,"selector"...
Selector:               app=linuxea_app,version=v0.1.32
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge

如上,RollingUpdateStrategy: 25% max unavailable, 25% max surge
可以使用kubectl get pods -l app=linuxea_app -w关注到整个过程
为了看到效果,在修改一次image

[root@linuxea linuxea]# sed -i 's/nginx:1.14.a/nginx:1.14.b/'g deploy-demt.yaml
[root@linuxea linuxea]# kubectl apply -f deploy-demt.yaml 
deployment.apps/dpment-linuxea configured

kubectl get pods -l app=linuxea_app -w观察自动滚动更新的过程

[root@linuxea linuxea]# kubectl get pods -l app=linuxea_app -w
NAME                              READY     STATUS    RESTARTS   AGE
dpment-linuxea-65b8d94cc6-kcckt   1/1       Running   0          16m
dpment-linuxea-65b8d94cc6-q7hw8   1/1       Running   0          27m
dpment-linuxea-65b8d94cc6-tvn2d   1/1       Running   0          27m
dpment-linuxea-648d599b5f-xh7xp   0/1       Pending   0         0s
dpment-linuxea-648d599b5f-xh7xp   0/1       Pending   0         0s
dpment-linuxea-648d599b5f-xh7xp   0/1       ContainerCreating   0         0s
dpment-linuxea-648d599b5f-xh7xp   1/1       Running   0         1s
dpment-linuxea-65b8d94cc6-kcckt   1/1       Terminating   0         18m
dpment-linuxea-648d599b5f-94pd2   0/1       Pending   0         0s
dpment-linuxea-648d599b5f-94pd2   0/1       Pending   0         0s
dpment-linuxea-648d599b5f-94pd2   0/1       ContainerCreating   0         0s
dpment-linuxea-648d599b5f-94pd2   1/1       Running   0         1s
dpment-linuxea-65b8d94cc6-tvn2d   1/1       Terminating   0         29m
dpment-linuxea-648d599b5f-blrzr   0/1       Pending   0         0s
dpment-linuxea-648d599b5f-blrzr   0/1       Pending   0         0s
dpment-linuxea-648d599b5f-blrzr   0/1       ContainerCreating   0         0s
dpment-linuxea-648d599b5f-blrzr   1/1       Running   0         2s
dpment-linuxea-65b8d94cc6-q7hw8   1/1       Terminating   0         29m
dpment-linuxea-65b8d94cc6-kcckt   0/1       Terminating   0         19m
dpment-linuxea-65b8d94cc6-kcckt   0/1       Terminating   0         19m
dpment-linuxea-65b8d94cc6-kcckt   0/1       Terminating   0         19m
dpment-linuxea-65b8d94cc6-tvn2d   0/1       Terminating   0         30m
dpment-linuxea-65b8d94cc6-q7hw8   0/1       Terminating   0         30m
dpment-linuxea-65b8d94cc6-tvn2d   0/1       Terminating   0         30m
dpment-linuxea-65b8d94cc6-tvn2d   0/1       Terminating   0         30m
dpment-linuxea-65b8d94cc6-q7hw8   0/1       Terminating   0         30m
dpment-linuxea-65b8d94cc6-q7hw8   0/1       Terminating   0         30m

Pending表示尚未调度完成,完成则会创建,依次重复更新完成结束

  • 历史版本信息

当,使用kubectl get rs -o wide会发现,此前创建的dpment-linuxea-65b8d94cc6依然存在。DESIRED却为0

[root@linuxea linuxea]# kubectl get rs -o wide
NAME                        DESIRED   CURRENT   READY     AGE       CONTAINERS          IMAGES                   SELECTOR
dpment-linuxea-648d599b5f   3         3         3         3m        nginx-a             marksugar/nginx:1.14.b   app=linuxea_app,pod-template-hash=2048155619,version=v0.1.32
dpment-linuxea-65b8d94cc6   0         0         0         33m       nginx-a             marksugar/nginx:1.14.a   app=linuxea_app,pod-template-hash=2164850772,version=v0.1.32

也可查看历史

[root@linuxea linuxea]# kubectl rollout history deployment dpment-linuxea
deployments "dpment-linuxea"
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

[root@linuxea linuxea]# 

III. patch补丁

打补丁的临时方式修改,并不会修改原文件
假如修改replicas个数

[root@linuxea linuxea]# kubectl patch deployment dpment-linuxea -p '{"spec":{"replicas":5}}'
deployment.extensions/dpment-linuxea patched

其中,kubectl patch后跟name,使用-p花括号的方式,大致这样:'{"spec":{"replicas":5}}',修改完成使用kubectl get rs查看

[root@linuxea linuxea]# kubectl get rs
NAME                        DESIRED   CURRENT   READY     AGE
dpment-linuxea-648d599b5f   5         5         5         1d
dpment-linuxea-65b8d94cc6   0         0         0         1d
linuxea.com                 5         5         5         2d
nginx-linuxea-5786698598    3         3         3         6d

查看pods,已经打补丁修改完成

[root@linuxea linuxea]# kubectl get pods
NAME                              READY     STATUS    RESTARTS   AGE
client-linuxea                    1/1       Running   0          9d
dpment-linuxea-648d599b5f-8shps   1/1       Running   0          26s
dpment-linuxea-648d599b5f-94pd2   1/1       Running   0          1d
dpment-linuxea-648d599b5f-blrzr   1/1       Running   0          1d
dpment-linuxea-648d599b5f-ccj9c   1/1       Running   0          26s

IV. patch2

当前五个pod,最多不可用0个,最大在线可用数量为6个,并且更新第一个就暂停下来

  • 修改策略

打补丁进行修改上述策略

[root@linuxea linuxea]# kubectl patch deployment dpment-linuxea -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
deployment.extensions/dpment-linuxea patched
[root@linuxea linuxea]# kubectl describe deployment dpment-linuxea|grep RollingUpdateStrategy
RollingUpdateStrategy:  0 max unavailable, 1 max surge

上述RollingUpdateStrategy: 0 max unavailable, 1 max surge已经从25%修改

  • 更新

V. set image

set image deployment dpment-linuxea,其中nginx-a是yaml文件中name值,而后使用pause 停止kubectl rollout pause deployment dpment-linuxea
使用set image修改成nginx:1.14.c(这个镜像内容和nginx:1.14.b一样)

  • pause: 暂停更新
  • resume: 继续此前的更新
[root@linuxea linuxea]# kubectl set image deployment dpment-linuxea nginx-a=marksugar/nginx:1.14.c &&kubectl rollout pause deployment dpment-linuxea
deployment.extensions/dpment-linuxea image updated
deployment.extensions/dpment-linuxea paused

更新完成第一个就会停止,kubectl get pods -l version=v0.1.32 --show-labels -w监视,或者不看标签

[root@linuxea linuxea]# kubectl get pods -l version=v0.1.32 -w
NAME                              READY     STATUS    RESTARTS   AGE
dpment-linuxea-65b8d94cc6-dlmjw   1/1       Running   0          3m
dpment-linuxea-65b8d94cc6-fxg9h   1/1       Running   0          3m
dpment-linuxea-65b8d94cc6-nlnjd   1/1       Running   0          3m
dpment-linuxea-65b8d94cc6-w4grl   1/1       Running   0          11m
dpment-linuxea-65b8d94cc6-x79t2   1/1       Running   0          3m
dpment-linuxea-745fd95894-ln886   0/1       Pending   0         0s
dpment-linuxea-745fd95894-ln886   0/1       Pending   0         0s
dpment-linuxea-745fd95894-ln886   0/1       ContainerCreating   0         0s
dpment-linuxea-745fd95894-ln886   1/1       Running   0         5s

或者使用kubectl rollout status deployment dpment-linuxea查看

[root@linuxea ~]# kubectl rollout status deployment dpment-linuxea
Waiting for deployment "dpment-linuxea" rollout to finish: 1 out of 5 new replicas have been updated...
  • rollout

但被更新的这台是想要的结果,而后在更新其他

[root@linuxea ~]# kubectl rollout resume deployment dpment-linuxea
deployment.extensions/dpment-linuxea resumed

在观察kubectl rollout status deployment dpment-linuxea结果

[root@linuxea ~]# kubectl rollout status deployment dpment-linuxea
Waiting for deployment "dpment-linuxea" rollout to finish: 1 out of 5 new replicas have been updated...
Waiting for deployment spec update to be observed...
Waiting for deployment spec update to be observed...
Waiting for deployment "dpment-linuxea" rollout to finish: 1 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 1 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 2 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 2 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "dpment-linuxea" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "dpment-linuxea" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "dpment-linuxea" rollout to finish: 1 old replicas are pending termination...
deployment "dpment-linuxea" successfully rolled out

在观察更新过程

[root@linuxea linuxea]# kubectl get pods -l version=v0.1.32 -w
NAME                              READY     STATUS    RESTARTS   AGE
dpment-linuxea-65b8d94cc6-dlmjw   1/1       Running   0          3m
dpment-linuxea-65b8d94cc6-fxg9h   1/1       Running   0          3m
dpment-linuxea-65b8d94cc6-nlnjd   1/1       Running   0          3m
dpment-linuxea-65b8d94cc6-w4grl   1/1       Running   0          11m
dpment-linuxea-65b8d94cc6-x79t2   1/1       Running   0          3m
dpment-linuxea-745fd95894-ln886   0/1       Pending   0         0s
dpment-linuxea-745fd95894-ln886   0/1       Pending   0         0s
dpment-linuxea-745fd95894-ln886   0/1       ContainerCreating   0         0s
dpment-linuxea-745fd95894-ln886   1/1       Running   0         5s
dpment-linuxea-65b8d94cc6-dlmjw   1/1       Terminating   0         5m
dpment-linuxea-745fd95894-f58tj   0/1       Pending   0         0s
dpment-linuxea-745fd95894-f58tj   0/1       Pending   0         0s
dpment-linuxea-745fd95894-f58tj   0/1       ContainerCreating   0         0s
dpment-linuxea-745fd95894-f58tj   1/1       Running   0         5s
dpment-linuxea-65b8d94cc6-nlnjd   1/1       Terminating   0         5m
dpment-linuxea-745fd95894-4c2nr   0/1       Pending   0         0s
dpment-linuxea-745fd95894-4c2nr   0/1       Pending   0         0s
dpment-linuxea-745fd95894-4c2nr   0/1       ContainerCreating   0         0s
dpment-linuxea-745fd95894-4c2nr   1/1       Running   0         4s
dpment-linuxea-65b8d94cc6-fxg9h   1/1       Terminating   0         6m
dpment-linuxea-745fd95894-2xwcp   0/1       Pending   0         0s
dpment-linuxea-745fd95894-2xwcp   0/1       Pending   0         0s
dpment-linuxea-745fd95894-2xwcp   0/1       ContainerCreating   0         0s
dpment-linuxea-745fd95894-2xwcp   1/1       Running   0         1s
dpment-linuxea-65b8d94cc6-x79t2   1/1       Terminating   0         6m
dpment-linuxea-745fd95894-5rpdc   0/1       Pending   0         0s
dpment-linuxea-745fd95894-5rpdc   0/1       Pending   0         0s
dpment-linuxea-745fd95894-5rpdc   0/1       ContainerCreating   0         0s
dpment-linuxea-745fd95894-5rpdc   1/1       Running   0         1ss

在查看rs,此刻历史版本有marksugar/nginx:1.14.a,marksugar/nginx:1.14.b,marksugar/nginx:1.14.c三个镜像

[root@linuxea linuxea]# kubectl get rs -o wide -l app=linuxea_app
NAME                        DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES                   SELECTOR
dpment-linuxea-648d599b5f   0         0         0         1d        nginx-a      marksugar/nginx:1.14.b   app=linuxea_app,pod-template-hash=2048155619,version=v0.1.32
dpment-linuxea-65b8d94cc6   0         0         0         1d        nginx-a      marksugar/nginx:1.14.a   app=linuxea_app,pod-template-hash=2164850772,version=v0.1.32
dpment-linuxea-745fd95894   5         5         5         3m        nginx-a      marksugar/nginx:1.14.c   app=linuxea_app,pod-template-hash=3019851450,version=v0.1.32
  • 回滚

此刻,发现c镜像有问题,即可进行回滚,undo(如果不指定,默认回滚上一个)
首先,需要知道当前版本号REVISION,使用kubectl rollout history deployment dpment-linuxea获取

[root@linuxea linuxea]# kubectl rollout history deployment dpment-linuxea 
deployments "dpment-linuxea"
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
4         <none>

而后进行回滚。--to-revision制定版本号REVISION

[root@linuxea linuxea]# kubectl rollout undo deployment dpment-linuxea --to-revision=2
deployment.extensions/dpment-linuxea

按照之前的rs顺序,结合rollout history,指定版本2的镜像应该是marksugar/nginx:1.14.b(此前回滚一次,1被滚动成4)

[root@linuxea linuxea]# kubectl get rs -o wide
NAME                        DESIRED   CURRENT   READY     AGE       CONTAINERS          IMAGES                   SELECTOR
dpment-linuxea-648d599b5f   5         5         5         1d        nginx-a             marksugar/nginx:1.14.b   app=linuxea_app,pod-template-hash=2048155619,version=v0.1.32
dpment-linuxea-65b8d94cc6   0         0         0         1d        nginx-a             marksugar/nginx:1.14.a   app=linuxea_app,pod-template-hash=2164850772,version=v0.1.32
dpment-linuxea-745fd95894   0         0         0         12m       nginx-a             marksugar/nginx:1.14.c   app=linuxea_app,pod-template-hash=3019851450,version=v0.1.32

在看历史版本

[root@linuxea linuxea]# kubectl rollout history deployment dpment-linuxea 
deployments "dpment-linuxea"
REVISION  CHANGE-CAUSE
3         <none>
4         <none>
5         <none>

其中第二版已经被滚动到REVISION5

0 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2018-09-15分类: kubernetes

标签: kubernetes

发表评论