本文核心问题是:如何升级应用。
对于Pod的更新有两种策略:html
对于应用的版本v1和版本v2:node
金丝雀部署一种增量发布,先是在小范围内发布,而后观察测试,如无问题逐渐发布所有。web
由于kubectl rolling-update的方式已通过时,因此只是作一下简介。
假设如今有一个名为test-v1,Pod选择器为app=order
的ReplicationController要升级为test-v2,则执行下面命令可升级:docker
k rolling-update test-v1 test-v2 --image=test:v2
运行此命令后:json
deployment=xxxx
。app=order
选中的Pod都会被加上一个标签:deployment=yyyy
。如此作法是防止Pod的管理混乱。app=order
,因此新老版本都会接收到流量。过期的缘由是:伸缩的请求时由kubectl发起的,若是由于任何缘由丢失了网络链接,升级将处于中间状态。另外一个缘由是:指望只修改Pod定义中的镜像tag,就能时Kubernetes运行升级工做api
Deployment是一种更高阶的资源,用于部署程序并以声明的方式升级应用,而不是经过ReplicationController或ReplicaSet进行部署。网络
在使用Deployment时,Pod是由Deployment的ReplicaSet建立的。app
将以前的文章(Kubernetes学习笔记(四):服务)里的拿过来作一下微小的改动,生成两个镜像。改动内容就是在输出内容加上版本号。curl
fmt.Fprintf(w,"this is v1, hostname: %v\n",hostname) docker push registry.cn-hangzhou.aliyuncs.com/orzi/goweb:v1 fmt.Fprintf(w,"this is v2, hostname: %v\n",hostname) docker push registry.cn-hangzhou.aliyuncs.com/orzi/goweb:v2
Deployment与ReplicaSet的配置类似,都含有标签选择器、副本数量和Pod模板。此外Deployment还会包含一个部署策略。编辑器
定义了一个NodePort类型的Service
# goweb-svc.yaml apiVersion: v1 kind: Service metadata: name: goweb spec: type: NodePort selector: app: goweb ports: - port: 80 targetPort: 8000 nodePort: 31234
# goweb-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: goweb spec: replicas: 3 selector: matchLabels: app: goweb template: metadata: labels: app: goweb spec: containers: - name: goweb image: registry.cn-hangzhou.aliyuncs.com/orzi/goweb:v1
能够看到名为goweb-fdfcfdcc6
的RS,有个label是pod-template-hash=fdfcfdcc6
,顾名思义,fdfcfdcc6就是pod模板的hash值。
建立Deployment时指定 --record 记录历史版本号,很是有用
-> [root@kube0.vm] [~] k create -f goweb-svc.yaml service/goweb created -> [root@kube0.vm] [~] k create -f goweb-deployment.yaml --record deployment.apps/goweb created -> [root@kube0.vm] [~] k get all -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/goweb-fdfcfdcc6-4wklw 1/1 Running 0 9s 10.244.2.37 kube2.vm <none> <none> pod/goweb-fdfcfdcc6-bw8c4 1/1 Running 0 9s 10.244.2.36 kube2.vm <none> <none> pod/goweb-fdfcfdcc6-xjcwf 1/1 Running 0 9s 10.244.1.33 kube1.vm <none> <none> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/goweb NodePort 10.100.193.94 <none> 80:31234/TCP 28s app=goweb service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 54s <none> NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR deployment.apps/goweb 3/3 3 3 9s goweb registry.cn-hangzhou.aliyuncs.com/orzi/goweb:v1 app=goweb NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR replicaset.apps/goweb-fdfcfdcc6 3 3 3 9s goweb registry.cn-hangzhou.aliyuncs.com/orzi/goweb:v1 app=goweb,pod-template-hash=fdfcfdcc6
只要修改Deployment的Pod模板定义,Kubernetes会自动的将实际状态收敛为修改后的状态。对于升级,只须要修改Pod中镜像的tag。
升级策略由deployment.spec.strategy.type
定义,值是Recreate或RollingUpdate,默认RollingUpdate。
使用kubectl patch定义deployment.spec.minReadySeconds
来减慢滚动升级时间,以便观察升级过程
-> [root@kube0.vm] [~] k patch deployment goweb -p '{"spec":{"minReadySeconds":5}}' deployment.apps/goweb patched
在执行升级前新开窗口,运行下面的命令查看输出
-> [root@kube0.vm] [~] while true; do curl http://192.168.199.231:31234/ ; sleep 1; done this is v1, hostname: goweb-fdfcfdcc6-pw7b4 this is v1, hostname: goweb-fdfcfdcc6-pw7b4 this is v1, hostname: goweb-fdfcfdcc6-x8n7h this is v1, hostname: goweb-fdfcfdcc6-pw7b4 this is v1, hostname: goweb-fdfcfdcc6-x8n7h this is v1, hostname: goweb-fdfcfdcc6-j4mz8 this is v1, hostname: goweb-fdfcfdcc6-j4mz8 # 以上是升级以前的输出、如下是开始升级后的 this is v1, hostname: goweb-fdfcfdcc6-x8n7h this is v1, hostname: goweb-fdfcfdcc6-pw7b4 this is v1, hostname: goweb-fdfcfdcc6-pw7b4 this is v1, hostname: goweb-fdfcfdcc6-j4mz8 this is v2, hostname: goweb-65cc575865-25988 this is v2, hostname: goweb-65cc575865-25988 this is v1, hostname: goweb-fdfcfdcc6-x8n7h this is v1, hostname: goweb-fdfcfdcc6-j4mz8 this is v2, hostname: goweb-65cc575865-bfd98 this is v2, hostname: goweb-65cc575865-bfd98 this is v2, hostname: goweb-65cc575865-25988 this is v2, hostname: goweb-65cc575865-25988 this is v2, hostname: goweb-65cc575865-25988 # 这以后就是升级完成了
使用 kubectl set image 更新任何包含容器资源的镜像。
-> [root@kube0.vm] [~] k set image deployment goweb goweb=registry.cn-hangzhou.aliyuncs.com/orzi/goweb:v2 deployment.apps/goweb image updated
查看升级状态信息。执行完kubectl set image,马上执行下面的命令。手速得快,否则赶不上热乎的。
-> [root@kube0.vm] [~] k rollout status deployment goweb Waiting for deployment "goweb" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "goweb" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "goweb" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "goweb" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "goweb" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "goweb" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "goweb" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "goweb" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "goweb" rollout to finish: 1 old replicas are pending termination... deployment "goweb" successfully rolled out
方法 | 做用 |
---|---|
kubectl edit | 使用编辑器打开资源配置 |
kubectl patch | 在命令行以merge的方式修改配置 |
kubectl apply | 经过yaml或者json文件,修改新改动的值。若是指定的对象不存在则建立。 |
kubectl replace | 使用yaml或者json文件替换一个必须已存在的对象配置。 |
kubectl set image | 修改镜像 |
使用kubectl rollout undo 回滚到上一个版本
-> [root@kube0.vm] [~] k rollout undo deployment goweb deployment.apps/goweb rolled back
使用 kubectl rollout history 查看版本记录
-> [root@kube0.vm] [~] k rollout history deployment goweb deployment.apps/goweb REVISION CHANGE-CAUSE 6 kubectl create --filename=goweb-deployment.yaml --record=true 7 kubectl create --filename=goweb-deployment.yaml --record=true
回滚到特定版本
-> [root@kube0.vm] [~] k rollout undo deployment goweb --to-revision=5 error: unable to find specified revision 5 in history -> [root@kube0.vm] [~] k rollout undo deployment goweb --to-revision=7 deployment.apps/goweb skipped rollback (current template already matches revision 7) -> [root@kube0.vm] [~] k rollout undo deployment goweb --to-revision=6 deployment.apps/goweb rolled back
经过deployment.spec.revisionHistoryLimit
指定历史版本个数,默认为2。也就是当前和上一个版本。
deployment.spec.strategy.rollingUpdate
下有两个字段,用来控制升级速率
kubectl rollout pause
暂停升级kubectl rollout resume
取消暂停DeploymentName+Pod模板Hash
,而ReplicaSet下的Pod是在此基础拼接个随机字符串。deployment.spec.strategy.type
定义,值是Recreate或RollingUpdate,默认RollingUpdate。deployment.spec.minReadySeconds
来减慢滚动升级时间