在执行升级前新开窗口,运行下面的命令查看输出
-> [root@kube0.vm] [~] while true; do curl :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使用 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 rollout查看升级状态信息。执行完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 修改Deployment或其他资源的方式 方法 作用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下有两个字段,用来控制升级速率
maxSurge:超出期望副本数的Pod实例的比例或个数。默认25%,转换成绝对值后四舍五入,也可以直接指定为绝对值。
maxUnavailable:滚动升级时,最多允许有多少实例不可用,默认25%,转换成绝对值后四舍五入,也可以直接指定为绝对值。
暂停、恢复升级使用kubectl rollout pause暂停升级
使用kubectl rollout resume取消暂停
阻止出错版本的滚动升级minReadySeconds:指定新创建的Pod至少要运行多久才视为可用。
配置就绪探针
为滚动升级配置progressDeadlineSeconds
小结kubectl rolling-update 过时的原因:伸缩的请求时由kubectl发起的,如果因为任何原因丢失了网络连接,升级将处于中间状态
Deployment是一种更高阶的资源,用于部署程序并以声明的方式升级应用,而不是通过ReplicationController或ReplicaSet进行部署。
创建Deployment时指定 --record 记录历史版本号
Deployment下的ReplicaSet命名是DeploymentName+Pod模板Hash,而ReplicaSet下的Pod是在此基础拼接个随机字符串。
升级策略由deployment.spec.strategy.type定义,值是Recreate或RollingUpdate,默认RollingUpdate。