1.Deployment
1.0 Deployment资源定义规范
apiVersion: apps/v1 # API群组及版本
kind: Deployment # 资源类型特有标识
metadata:
name <string> # 资源名称,在作用域中要唯一
namespace <string> # 名称空间;Deployment隶属名称空间级别
spec:
minReadySeconds <integer> # Pod就绪后多少秒内任一容器无crash方可视为“就绪”
replicas <integer> # 期望的Pod副本数,默认为1
selector <object> # 标签选择器,必须匹配template字段中Pod模板中的标签
template <object> # Pod模板对象
revisionHistoryLimit <integer> # 滚动更新历史记录数量,默认为10
strategy <Object> # 滚动更新策略
type <string> # 滚动更新类型,可用值有Recreate和RollingUpdate;
rollingUpdate <Object> # 滚动更新参数,专用于RollingUpdate类型
maxSurge <string> # 更新期间可比期望的Pod数量多出的数量或比例;
maxUnavailable <string> # 更新期间可比期望的Pod数量缺少的数量或比例,10,
progressDeadlineSeconds <integer> # 滚动更新故障超时时长,默认为600秒
paused <boolean> # 是否暂停部署过程
apiVersion: apps/v1 # API群组及版本
kind: Deployment # 资源类型特有标识
metadata:
name <string> # 资源名称,在作用域中要唯一
namespace <string> # 名称空间;Deployment隶属名称空间级别
spec:
minReadySeconds <integer> # Pod就绪后多少秒内任一容器无crash方可视为“就绪”
replicas <integer> # 期望的Pod副本数,默认为1
selector <object> # 标签选择器,必须匹配template字段中Pod模板中的标签
template <object> # Pod模板对象
revisionHistoryLimit <integer> # 滚动更新历史记录数量,默认为10
strategy <Object> # 滚动更新策略
type <string> # 滚动更新类型,可用值有Recreate和RollingUpdate;
rollingUpdate <Object> # 滚动更新参数,专用于RollingUpdate类型
maxSurge <string> # 更新期间可比期望的Pod数量多出的数量或比例;
maxUnavailable <string> # 更新期间可比期望的Pod数量缺少的数量或比例,10,
progressDeadlineSeconds <integer> # 滚动更新故障超时时长,默认为600秒
paused <boolean> # 是否暂停部署过程
1.1 什么是deployment
DepLoyment(简称为depLoy)是Kubernetes控制器的一种高级别实现,他构建于RepLicaSet控制器之上。
我们只需要描述DepLoyment中的目标Pod期望状态,而DepLoyment控制器以受控速率更改实际状态
,使其变为期望状态
,也就是说,后期我们部署应用不直接使用Pod和RepLicaSet,而是使用DepLoyment控制器来调用RepLicaSet来实现,DepLoyment控制器在RepLicaSet原有基础上,添加了部分特性。
- 事件和状态查看:可以通过特定的命令查看Deployment对象的更新进度和状态
- 版本记录:将Deployment对象的更新操作都进行保存,以便后续执行回滚操作使用
- 多种更新方案:Recreate重建,可以实现单批次更新所有Pod。RoLLingUpdate可以实现多批次逐步替换Pod
1.2 deployment组成部分
Deployment 资源对象的格式和 ReplicaSet ⼏乎⼀致,Deployment
控制器也包含了3个基本的组成部分
- selector 标签选择器:匹配并关联Pod对象,并对授其管控的Pod对象计数;
- replicas期望的副本数:期望在集群中所运⾏的Pod对象数量
- template Pod模板:实际上就是定义的 Pod 内容,相当于把⼀个Pod 的描述以模板的形式嵌⼊到了 ReplicaSet
示例
- 编写资源配置清单, deployment-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mall-deploy # 定义deploy资源名称
spec:
replicas: 2 # 定义deploy控制Pod的副本数
minReadySeconds: 10 # 定义Pod启动后,至少需要等待10秒钟,才能被认为是可用的
selector:
matchLabels: # 定义Pod选择器
app: mall
template: # Pod 模板,申明Pod名称、使用镜像
metadata:
labels: # 定义Pod标签
app: mall
spec:
containers:
- name: mall
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1
ports:
- containerPort: 8080
apiVersion: apps/v1
kind: Deployment
metadata:
name: mall-deploy # 定义deploy资源名称
spec:
replicas: 2 # 定义deploy控制Pod的副本数
minReadySeconds: 10 # 定义Pod启动后,至少需要等待10秒钟,才能被认为是可用的
selector:
matchLabels: # 定义Pod选择器
app: mall
template: # Pod 模板,申明Pod名称、使用镜像
metadata:
labels: # 定义Pod标签
app: mall
spec:
containers:
- name: mall
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1
ports:
- containerPort: 8080
2.应用配置清单
VERSION=v1 envsubst < deployment-demo.yaml | kubectl apply -f -
VERSION=v1 envsubst < deployment-demo.yaml | kubectl apply -f -
#查看deployment信息
[root@kube-master yaml]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-demo 4/4 4 4 24s
#查看dp
[root@kube-master yaml]# kubectl get pods -l 'app=demoapp,release=stable'
NAME READY STATUS RESTARTS AGE
deployment-demo-54c55c8847-7qw8x 1/1 Running 0 116s
deployment-demo-54c55c8847-ch7hp 1/1 Running 0 116s
deployment-demo-54c55c8847-vq9l5 1/1 Running 0 115s
deployment-demo-54c55c8847-x99k6 1/1 Running 0 116s
# 可以看到第一段为deployment名字,最后一段为随机值,中间的fb544c5d8为replicaset中Pod模板的哈希值,也就是template字段的哈希值
# 查看replicaset信息
[root@kube-master yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-demo-54c55c8847 4 4 4 5m40s
# 一旦pod模板发生变更,会导致ReplicaSet的哈希值发生变化,然后出发deployment更新的
#查看deployment信息
[root@kube-master yaml]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-demo 4/4 4 4 24s
#查看dp
[root@kube-master yaml]# kubectl get pods -l 'app=demoapp,release=stable'
NAME READY STATUS RESTARTS AGE
deployment-demo-54c55c8847-7qw8x 1/1 Running 0 116s
deployment-demo-54c55c8847-ch7hp 1/1 Running 0 116s
deployment-demo-54c55c8847-vq9l5 1/1 Running 0 115s
deployment-demo-54c55c8847-x99k6 1/1 Running 0 116s
# 可以看到第一段为deployment名字,最后一段为随机值,中间的fb544c5d8为replicaset中Pod模板的哈希值,也就是template字段的哈希值
# 查看replicaset信息
[root@kube-master yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-demo-54c55c8847 4 4 4 5m40s
# 一旦pod模板发生变更,会导致ReplicaSet的哈希值发生变化,然后出发deployment更新的
3.对deployment
实行滚动更新
VERSION=v2 envsubst < deployment-demo.yaml | kubectl apply -f -
#使用rollout查看滚动更新的状态
[root@kube-master yaml]# kubectl rollout status deployment deployment-demo
deployment "deployment-demo" successfully rolled out
#查看pod是否更新为v1.2版本
[root@kube-master yaml]# curl 172.30.0.181
hsuing demoapp v1.2 !! ClientIP: 172.25.244.192, PodName: deployment-demo-74b48bd6db-kd2j2, PodIP: 172.30.0.181!
VERSION=v2 envsubst < deployment-demo.yaml | kubectl apply -f -
#使用rollout查看滚动更新的状态
[root@kube-master yaml]# kubectl rollout status deployment deployment-demo
deployment "deployment-demo" successfully rolled out
#查看pod是否更新为v1.2版本
[root@kube-master yaml]# curl 172.30.0.181
hsuing demoapp v1.2 !! ClientIP: 172.25.244.192, PodName: deployment-demo-74b48bd6db-kd2j2, PodIP: 172.30.0.181!
4.回滚
#查看deployment的更新历史信息
[root@kube-master yaml]# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION CHANGE-CAUSE
1 <none>
2 <none> # 此为当前版本信息
#将其回滚到上一个版本
[root@kube-master yaml]# kubectl rollout undo deployment deployment-demo
deployment.apps/deployment-demo rolled back
#查看replicaset信息
[root@kube-master yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-demo-54c55c8847 4 4 4 13m
deployment-demo-74b48bd6db 0 0 0 6m15s
#再次查看更新历史
[root@kube-master yaml]# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION CHANGE-CAUSE
2 <none>
3 <none>
# 可以看到原本的1没有了新增了3,3表示当前的
#查看deployment的更新历史信息
[root@kube-master yaml]# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION CHANGE-CAUSE
1 <none>
2 <none> # 此为当前版本信息
#将其回滚到上一个版本
[root@kube-master yaml]# kubectl rollout undo deployment deployment-demo
deployment.apps/deployment-demo rolled back
#查看replicaset信息
[root@kube-master yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-demo-54c55c8847 4 4 4 13m
deployment-demo-74b48bd6db 0 0 0 6m15s
#再次查看更新历史
[root@kube-master yaml]# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION CHANGE-CAUSE
2 <none>
3 <none>
# 可以看到原本的1没有了新增了3,3表示当前的
2.Deployment重建策略
2.1 什么是Recreate
重建(Recreate)当更新策略设定为Recreate,在更新镜像时,它会先杀死正在运行的Pod,等彻底杀死后,重新创建新的RS,然后启动对应的Pod,那么在这个更新过程中,会造成服务一段时间无法提供服务;
第一步:同时杀死所有旧版本的Pod,此时Pod无法正常对外提供服务;)
第二步:创建新的RS,启动新的Pod;
第三步:等待Pod就绪,对外提供服务;
❌ 注意
通常只有当应用的新旧版本不兼容(例如依赖的后端数据的格式不同且无法兼容)时才会使用Recreate重建策略。
2.2 Recreate实践
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate #定义名字不能有下划线
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate #定义名字不能有下划线
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
- 观察现象
[root@kube-master yaml]# kubectl apply -f demoapp_recreate.yaml
[root@kube-master yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo-deployment-bf8f88dbc-4d298 1/1 Terminating 3 (7h45m ago) 10d
demo-deployment-bf8f88dbc-b5dmc 1/1 Terminating 3 (7h45m ago) 10d
demo-deployment-bf8f88dbc-tfl2g 1/1 Terminating 3 (7h45m ago) 10d
demo-deployment-bf8f88dbc-xrjfm 1/1 Terminating 3 (7h45m ago) 9d
[root@kube-master yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo-deployment-5684f48684-7zx87 1/1 Running 0 87s
demo-deployment-5684f48684-nlwhp 1/1 Running 0 87s
demo-deployment-5684f48684-zz6kc 1/1 Running 0 87s
[root@kube-master yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
demo-deployment-5684f48684 3 3 3 108s
demo-deployment-bf8f88dbc 0 0 0 10d
[root@kube-master yaml]# kubectl apply -f demoapp_recreate.yaml
[root@kube-master yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo-deployment-bf8f88dbc-4d298 1/1 Terminating 3 (7h45m ago) 10d
demo-deployment-bf8f88dbc-b5dmc 1/1 Terminating 3 (7h45m ago) 10d
demo-deployment-bf8f88dbc-tfl2g 1/1 Terminating 3 (7h45m ago) 10d
demo-deployment-bf8f88dbc-xrjfm 1/1 Terminating 3 (7h45m ago) 9d
[root@kube-master yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo-deployment-5684f48684-7zx87 1/1 Running 0 87s
demo-deployment-5684f48684-nlwhp 1/1 Running 0 87s
demo-deployment-5684f48684-zz6kc 1/1 Running 0 87s
[root@kube-master yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
demo-deployment-5684f48684 3 3 3 108s
demo-deployment-bf8f88dbc 0 0 0 10d
3.Deployment滚动更新
https://kubernetes.io/zh-cn/docs/tutorials/kubernetes-basics/update/update-intro/
kubectl explain deploy.spec.strategy
kubectl explain deploy.spec.strategy
3.1 什么是滚动更新
滚动更新(RoLLingUpdate),一次仅更新一批Pod,当更新的Pod就绪后,在更新另一批,直到全部更新完成为止;该策略实现了不间断服务的目标,在更新过程中可能会出现不同的应用版本并存且,同时提供服务的情况。
- 第一步:创建新的RepLicaSet,然后根据新的镜像运行新的Pod;
- 第二步:删除l旧的Pod,启动新的Pod,新Pod就绪后,继续删除旧Pod,启动新Pod;
- 第三步:持续第二步过程,一直到所有Pod都被更新成功。
3.2 实践
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 3
strategy:
type: RollingUpdate
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 3
strategy:
type: RollingUpdate
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
- 应用
[root@kube-master yaml]# kubectl apply -f demoapp_RollingUpdate.yaml
deployment.apps/demo-deployment configured
[root@kube-master yaml]# kubectl apply -f demoapp_RollingUpdate.yaml
deployment.apps/demo-deployment configured
- 观察
[root@kube-master yaml]# kubectl describe deployments.apps demo-deployment
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 5m33s deployment-controller Scaled down replica set demo-deployment-5684f48684 to 0
Normal ScalingReplicaSet 4m59s deployment-controller Scaled up replica set demo-deployment-645bbb7877 to 3
Normal ScalingReplicaSet 63s deployment-controller Scaled up replica set demo-deployment-5684f48684 to 1
Normal ScalingReplicaSet 60s deployment-controller Scaled down replica set demo-deployment-645bbb7877 to 2
Normal ScalingReplicaSet 60s deployment-controller Scaled up replica set demo-deployment-5684f48684 to 2
Normal ScalingReplicaSet 58s deployment-controller Scaled down replica set demo-deployment-645bbb7877 to 1
Normal ScalingReplicaSet 58s deployment-controller Scaled up replica set demo-deployment-5684f48684 to 3
Normal ScalingReplicaSet 56s deployment-controller Scaled down replica set demo-deployment-645bbb7877 to 0
[root@kube-master yaml]# kubectl describe deployments.apps demo-deployment
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 5m33s deployment-controller Scaled down replica set demo-deployment-5684f48684 to 0
Normal ScalingReplicaSet 4m59s deployment-controller Scaled up replica set demo-deployment-645bbb7877 to 3
Normal ScalingReplicaSet 63s deployment-controller Scaled up replica set demo-deployment-5684f48684 to 1
Normal ScalingReplicaSet 60s deployment-controller Scaled down replica set demo-deployment-645bbb7877 to 2
Normal ScalingReplicaSet 60s deployment-controller Scaled up replica set demo-deployment-5684f48684 to 2
Normal ScalingReplicaSet 58s deployment-controller Scaled down replica set demo-deployment-645bbb7877 to 1
Normal ScalingReplicaSet 58s deployment-controller Scaled up replica set demo-deployment-5684f48684 to 3
Normal ScalingReplicaSet 56s deployment-controller Scaled down replica set demo-deployment-645bbb7877 to 0
❌ 注意
会发现新老版本存在交替,但并不会出现服务不可用状态;
3.3 应⽤回退实践
有时候,你可能想要回滚 DepLoyment;例如,当DepLoyment 不稳定进入反复崩溃状态。默认情况下,DepLoyment的所有上线记录都保留在系统中,以便可以随时回滚。(你可以通过修改revisionHistoryLimit调整保留的数量,默认10条)
检查 Deployment 上线的历史版本
[root@kube-master yaml]# kubectl rollout history deployment <deployment_name>
deployment.apps/demo-deployment
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
[root@kube-master yaml]# kubectl rollout history deployment <deployment_name>
deployment.apps/demo-deployment
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
查看revision对应版本
[root@kube-master yaml]# kubectl rollout history deployment demo-deployment --revision=4
deployment.apps/demo-deployment with revision #4
Pod Template:
Labels: app=demo
pod-template-hash=645bbb7877
Containers:
container-recreate:
Image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
[root@kube-master yaml]# kubectl rollout history deployment demo-deployment --revision=4
deployment.apps/demo-deployment with revision #4
Pod Template:
Labels: app=demo
pod-template-hash=645bbb7877
Containers:
container-recreate:
Image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
开始回退
通过--to-revision参数回退
[root@kube-master yaml]# kubectl rollout undo deployment demo-deployment --to-revision=4
deployment.apps/demo-deployment rolled back
[root@kube-master yaml]# kubectl rollout undo deployment demo-deployment --to-revision=4
deployment.apps/demo-deployment rolled back
4.Deployment更新策略
kubectl explain deploy.spec.strategy
kubectl explain deploy.spec.strategy
Deployment 会在 .spec.strategy.type=RollingUpdate
时,采取滚动更新的方式更新 Pods。可以指定maxUnavailable
和maxSurgeS
来控制滚动更新过程
maxSurge最大可用Pod
- 用来指定可以创建超出期望Pod个数的Pod数量。可以是数字,也可以是百分比(例如10%)此字段的默认值为25%
- 例如,当此值为20%时,启动滚动更新后,会立即对新的ReplicaSet扩容,同时保证新l旧 Pod 的总数不超过所需Pod 总数的 120%。一旦l旧 Pods 被杀死,新的ReplicaSet 可以进一步扩容,同时确保更新期间的任何时候运行中的 Pods 总数最多为所需 Pods 总数的 120%。计算公式:
10+(10x20%)=12
maxUnavailable最大不可用Pod
- 用来指定更新过程中不可用的 Pod 的个数上限。可以是数字,也可以是百分比 (例如10%)此字段的默认值为25%
- 例如,当此值设置为20%时,滚动更新开始时会立即将旧RepLicaSet 缩容到期望 Pod 个数的7θ%。新Pod 准备就绪后,继续缩容l旧有的 ReplicaSet,然后对新的ReplicaSet 扩容,确保在更新期间可用的 Pods总数在任何时候都是所需的 Pod 个数的 70%。计算公式:
10-(10x20%)=8
❌ 注意
maxSurge和maxUnavaiLable两个属性协同工作,可组合定义出3中不同的策略完成多批次的应用更新。
1-先增新,后减旧:将maxSurge设置为30%,将maxUnavailable的值设为θ;
2-先减旧,后增新:将maxUnavaiLabLe设置为30%,将maxSurge的值设为θ;
3-同时增减,将maxSurge和maxUnavaiLabLe分别设定为20%;期望是12Pod,至少就绪8个Pod
4.1 maxSurge
指定升级期间存在的总Pod对象数量最多可超出期望值的个数,可以是0,也可以是整数,也可以是⼀个百分⽐
例如:期望的的值为10,maxSurge属性为2,则表示Pod对象总数不能超过12个。计算公式:10+(10x20%)=12
实践
[root@kube-master yaml]# cat demoapp_RollingUpdate_max.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 10
strategy:
rollingUpdate:
maxSurge: 20% # 最⼤可⽤20%
maxUnavailable: 0 # 不可⽤状态设定为0
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
[root@kube-master yaml]# cat demoapp_RollingUpdate_max.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 10
strategy:
rollingUpdate:
maxSurge: 20% # 最⼤可⽤20%
maxUnavailable: 0 # 不可⽤状态设定为0
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
- 查看
watch -n 1 kubectl get rs
kubectl describe deployments.apps demo-deployment
watch -n 1 kubectl get rs
kubectl describe deployments.apps demo-deployment
4.2 maxUnavailable
升级期间不可⽤的Pod副本数(包括新版本),最多不能低于期望的个数,默认为1
如:期望的值为1θ,maxunavaiabLe属性为2,则表示Pod处于正常状态至少有8个。计算公式:: 10-(10x20%)=8
实践
[root@kube-master yaml]# cat demoapp_RollingUpdate_max.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 5
strategy:
rollingUpdate:
maxSurge: 0 # 最⼤可⽤0
maxUnavailable: 20% # 不可⽤Pod副本最低20%
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
[root@kube-master yaml]# cat demoapp_RollingUpdate_max.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 5
strategy:
rollingUpdate:
maxSurge: 0 # 最⼤可⽤0
maxUnavailable: 20% # 不可⽤Pod副本最低20%
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
- 观察
watch -n 1 kubectl get rs
watch -n 1 kubectl get rs
4.3 maxSurge & MaxUnavailable
同时设定maxSurge 以及 MaxUnavailable
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 5
strategy:
rollingUpdate:
maxSurge: 20% # 最⼤可⽤0
maxUnavailable: 20% # 不可⽤Pod副本最低20%
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 5
strategy:
rollingUpdate:
maxSurge: 20% # 最⼤可⽤0
maxUnavailable: 20% # 不可⽤Pod副本最低20%
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: container-recreate
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v2
ports:
- containerPort: 80
4.4 minReadySeconds
Deployment支持使用spec.minReadySeconds
字段来控制滚动更新的速度,默认值为θ,表示新建的Pod对象一旦“就绪"将立即被视作可用,随后即可开始下一轮更新过程。如果设定了spec.minReadySeconds:3
及表示新建的Pod对象至少要成功运行多久才会被视作可用,即就绪之后还要等待指定的3s才能开始下一批次的更新。在一个批次内新建的所有Pod就绪后在转为可用状态前,更新操作会被阻塞,并且任何一个Pod就绪探测失败,都会导致滚动更新被终止。
因此,为minReadySeconds
设定一个合理的值,不仅能够减缓更新的速度,还能够让DepLoyment提前发现一部分程序因为Bug导致的升级故障。
4.5 revisionHistoryLimit
Deployment保留一部分更新历史中旧版本的ReplicaSet
对象,当我们执行回滚操作的时候,就直接使用旧版本的ReplicaSet,在Deployment资源保存历史版本数量有spec.revisionHistoryLimit
属性进行定义
[root@kube-master yaml]# kubectl rollout history deployment
deployment.apps/demo-deployment
REVISION CHANGE-CAUSE
2 <none>
3 <none>
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
[root@kube-master yaml]# kubectl rollout history deployment
deployment.apps/demo-deployment
REVISION CHANGE-CAUSE
2 <none>
3 <none>
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
4.6 progressDeadlineSeconds
滚动更新故障超时时长,默认为600秒,k8s在升级过程中有可能由于各种原因升级卡住(这个时候还没有明确的升级失败),比如在拉取被墙的镜像,权限不够等错误。如果配置progressDeadlineSeconds,当达到了时间如果还卡着,则会上报这个异常情况,这个时候这个Deployment状态就被标记为False,并且注明原因。但是它并不会阻止Deployment继续进行卡住后面的升级操作.
4.7 红绿发布
1.0
[root@kube-master yaml]# cat hong_lu1.0.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-version-10-prod
spec:
replicas: 3
selector:
matchLabels:
app: demoapp
version: v1.0
template:
metadata:
labels:
app: demoapp
version: v1.0
spec:
containers:
- name: demoapp
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1.0
---
apiVersion: v1
kind: Service
metadata:
name: demoapp-version-service
spec:
selector:
app: demoapp
ports:
- name: http
port: 80
targetPort: 80
[root@kube-master yaml]# cat hong_lu1.0.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-version-10-prod
spec:
replicas: 3
selector:
matchLabels:
app: demoapp
version: v1.0
template:
metadata:
labels:
app: demoapp
version: v1.0
spec:
containers:
- name: demoapp
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1.0
---
apiVersion: v1
kind: Service
metadata:
name: demoapp-version-service
spec:
selector:
app: demoapp
ports:
- name: http
port: 80
targetPort: 80
1.1
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-version-11-prod
spec:
replicas: 1
selector:
matchLabels:
app: demoapp
version: v1.1
template:
metadata:
labels:
app: demoapp
version: v1.1
spec:
containers:
- name: demoapp
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1.1
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-version-11-prod
spec:
replicas: 1
selector:
matchLabels:
app: demoapp
version: v1.1
template:
metadata:
labels:
app: demoapp
version: v1.1
spec:
containers:
- name: demoapp
image: registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:v1.1
- 访问clusterIP
kubectl get svc
curl clusterIp/version
kubectl get svc
curl clusterIp/version