创建deployment一个示例
~# kubectl create ns demo
~# kubectl get ns
...
demo Active 2s
...
~# kubectl create deploy nginx --image=nginx:1.24-alpine --replicas=3 --dry-run=client -o yaml -n demo > deploy.nginx.yaml
~# cat deploy.nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
namespace: demo
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.24-alpine
name: nginx
~# kubectl apply -f deploy.nginx.yaml
~# kubectl get pod -n demo
NAME READY STATUS RESTARTS AGE
nginx-6686c79596-7wfvt 1/1 Running 0 105s
nginx-6686c79596-fl26s 1/1 Running 0 105s
nginx-6686c79596-zd49g 1/1 Running 0 105s
更新策略
Deployment控制器支持两种更新策略
◼ 滚动式更新(rolling updates)
◆逐批次更新Pod的方式,支持按百分比或具体的数量定义批次规模,默认策略
◆触发条件:podTemplate的hash码变动
⚫ 仅podTemplate的配置变动才会导致hash码改变
⚫ replicas和selector的变更不会导致podTemplate的hash变动
◼ 重建式更新(recreate)
◆在Pod资源被删除时,使用新的模板定义被足缺失的Pod数量,完成更新
◆触发条件:现有Pod被删除
spec:
strategy:
rollingUpdate:
maxSurge: 1 # 一次只更新一个 ,这里也可以写百分比,例如: 20%
maxUnavailable: 0 # 不可以状态为0个
type: RollingUpdate
# 只有新加上一个pod,才会删除一个pod可以保证服务不中断
~# cat deploy.nginx.yaml
...
spec:
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
....
containers:
- image: nginx:1.22-alpine
name: nginx
~# kubectl apply -f deploy.nginx.yaml
`可以看到他是一个一个更新的`
~# kubectl rollout status deploy nginx -n demo
Waiting for deployment "nginx" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
...
deployment "nginx" successfully rolled out
实现金丝雀更新
~# cat deploy.nginx.yaml
...
spec:
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
....
containers:
- image: nginx:1.20-alpine
name: nginx
`启用金丝雀更新`
~# kubectl apply -f deploy.nginx.yaml && kubectl rollout pause deploy/nginx -n demo && kubectl rollout status deploy/nginx -n demo
`另起终端查看pod信息,边为了4个,但是对应的有一个hash值不一样,这个就是更新的pod,`
~# kubectl get pod -n demo
NAME READY STATUS RESTARTS AGE
nginx-5c98c87f8b-2gtn2 1/1 Running 0 8m42s
nginx-5c98c87f8b-2jqdf 1/1 Running 0 6m57s
nginx-5c98c87f8b-m5s69 1/1 Running 0 9m52s
nginx-7696df55c6-54fd9 1/1 Running 0 37s # 这个
`通过一段时间service自动调度到nginx-7696df55c6-54fd9验证没有问题`
`对剩下所有pod进行更新`
~# kubectl rollout resume deploy/nginx -n demo
`再次查看pod,发现已经全部更新完成,7696df55c6哈希值7696df55c6一样`
~# kubectl get pod -n demo
NAME READY STATUS RESTARTS AGE
nginx-7696df55c6-54fd9 1/1 Running 0 5m17s
nginx-7696df55c6-w5mgw 1/1 Running 0 36s
nginx-7696df55c6-x67fv 1/1 Running 0 37s
查看历史版本
~# kubectl rollout history deploy/nginx -n demo
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
回滚历史版本
回滚到上一个版本
~# kubectl rollout undo deployment/nginx -n demo
`看哈希值5c98c87f8b发现已经回滚到上一个版本`
~# kubectl get pod -n demo
NAME READY STATUS RESTARTS AGE
nginx-5c98c87f8b-5584q 1/1 Running 0 18s
nginx-5c98c87f8b-8nqzl 1/1 Running 0 17s
nginx-5c98c87f8b-9dr5g 1/1 Running 0 19s
回滚到指定版本
~# kubectl rollout history deploy/nginx -n demo
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
4 <none>
5 <none>
`回滚到第一个版本`
~# kubectl rollout undo deployment/nginx --to-revision=1 -n demo
`看哈希值6686c79596发现已经回滚到上一个版本`
~# kubectl get pod -n demo
NAME READY STATUS RESTARTS AGE
nginx-6686c79596-24pq7 1/1 Running 0 8s
nginx-6686c79596-hjmmv 1/1 Running 0 6s
nginx-6686c79596-thl2q 1/1 Running 0 7s
文章评论