deployment控制pod进行滚动更新以及回滚

更新pod镜像两种方式:

方式一:kubectl set image deployment/${deployment name} ${container name}=${image}  例: kubectl set image deployment/nginx-deployment nginx=nginx:1.13 --record
方式二: kubectl edit 修改deployment配置, 将spec.template.spec,containers[0].image 从nginx:1.12 修改成nginx:1.13

查看deployment更新过程:

kubectl rollout status deployment/nginx-deployment
可以通过deployment状态来判断pod更新是否完成, kubectl rollout status deployment nginx-deployment --watch=false | grep -ic waiting1 如果更新成功, 返回值为0

deployment更新策略: Recreate(重建)杀掉所有的pod, 然后创建新的pod 和 RollingUpdate(滚动更新) 通过参数maxUnavailable与maxSurge来控制滚动更新过程

滚动更新策略:

deployment.spec.strategy.rollingUpdate.maxUnavailable:不可用状态的pod数量,该值可以是绝对值, 也可以是pod期望的副本数的百分比
deployment.spec.strategy.rollingUpdate.maxSurge: 超过pod期望副本数的最大值
当maxSurge设置为30% 和 maxUnavailable设置为30% : 生成一个新的ReplicaSet立即进行副本数扩容, 扩容的数量为不超过期望副本数的130%即可, 旧的ReplicaSet立即缩容到所需副本数的70%

pod回滚: 建议deployment滚动更新的时候加上--record参数, 这样在change-cause看个每个版本使用的命令

kubectl rollout history deployment/nginx-deployment 检查deployment升级的历史记录
kubectl rollout history deployment/nginx-deployment --revision=2 查看单个revision 的详细信息
kubectl rollout undo deployment/nginx-deployment 回退到上一个版本
kubectl rollout undo deployment/nginx-deployment --to-revision=2 根据--revision参数指定某个历史版本

spec.revisonHistoryLimit项来指定 deployment 最多保留多少 revision 历史记录

注意: deployment的pod模板被更改时才会创建新的修订版本,例如更新模板标签或者容器镜像可以触发滚动更新, 其它操作例如扩展副本数将不会触发deployment的更新操作

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

kubectl rollout pause deployment/nginx-deployment #暂停deployment , 不影响pod正常运行, 可以修改的deployment资源, 不会触发滚动更新
kubectl set image deploy/nginx nginx=nginx:1.9.1
kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi
kubectl rollout resume deployment/nginx-deployment #恢复deployment, 如果有变动, 马上进行滚动更新
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 10
strategy:
rollingUpdate:
maxSurge: 30%
maxUnavailable: 30%
template:
metadata:
labels:
app: nginx spec:
initContainers:
- name: install
image: busybox
imagePullPolicy: IfNotPresent
command:
- wget
- "-O"
- "/work-dir/index.html"
- http://kubernetes.io
volumeMounts:
- name: workdir
mountPath: "/work-dir"
containers:
- name: nginx
image: nginx:1.12
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: workdir
mountPath: "/usr/share/nginx/html"
volumes:
- name: workdir
emptyDir: {} ---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default
labels:
app: nginx
spec:
type: NodePort
selector:
app: nginx
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
上一篇:linux运维、架构之路-K8s滚动更新及回滚


下一篇:k8s deployment controller源码分析