共计 13980 个字符,预计需要花费 35 分钟才能阅读完成。
Deployment 简介
Deployment 为 Pod 和 ReplicaSet 提供了一个申明式定义 (declarative) 办法,用来代替以前的 ReplicationController 来不便的治理利用。典型的利用场景包含;定义 Deployment 来创立 Pod 和 ReplicaSet
- 滚动降级和回滚利用
- 扩容和缩容
- 暂停和持续 Deploymen
Deployment 无状态服务最罕用的 Pod 管理器
Deployment 理论是治理 ReplicaSet Deployment 默认会保留 10 个 ReplicaSet 历史版本
Deployment 会把 ReplicaSet 之前个版本的正本数批改为 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 <0bject> #滚动更新策略
type <string> #滚动更新类型,可用值有 Recreate 和 Rollingupdate; Recreate 重建更新一次性删除所有 Pod;Rollingupdate 滚动更新默认策略
rollingUpdate <0bject> #滚动更新参数,专用于 RollingUpdate 类型
maxSurge <string> #更新期间可比冀望的 Pod 数量多出的数量或比例
maxUnavailable <string> #更新期间可比冀望的 Pod 数量短少的数量或比例
progressDeadlineSeconds <integer> # 滚动更新故障超时时长,默认为 600 秒
paused <boolean> #是否暂停部暑过程
示例 1: 创立 Deployment 控制器 Pod 正本数为 4
[root@k8s-master PodControl]# cat deployment-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-demo
spec:
replicas: 4 #正本数来 4
selector:
matchLabels:
app: demoapp
release: stable
template:
metadata:
labels :
app: demoapp
release: stable
spec:
containers:
- name: demoapp
image: ikubernetes/demoapp:${VERSION}
ports:
- containerPort: 80
name: http
[root@k8s-master PodControl]# kubectl apply -f deployment-demo.yaml
deployment.apps/deployment-demo created
[root@k8s-master PodControl]# VERSION=v1.0 envsubst < deployment-demo.yaml |kubectl apply -f -
deployment.apps/deployment-demo created
[root@k8s-master PodControl]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-demo 4/4 4 4 54s
#能够看到创立了同名的 rs deployment 理论治理的是 rs 而不是 Pod
#名称中的 fb544c5d8 是通过配置文的生成的 hash 值, 当查看到配置文件 hash 值变动就会启到更新
[root@k8s-master PodControl]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-demo-fb544c5d8 4 4 4 56s
[root@k8s-master PodControl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-demo-fb544c5d8-kz8fn 1/1 Running 0 58s
deployment-demo-fb544c5d8-p6q4v 1/1 Running 0 58s
deployment-demo-fb544c5d8-ttjmt 1/1 Running 0 58s
deployment-demo-fb544c5d8-x2lqw 1/1 Running 0 58s
[root@k8s-master PodControl]# kubectl describe deployment/deployment-demo
Name: deployment-demo
Namespace: default
CreationTimestamp: Mon, 16 Aug 2021 16:17:28 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=demoapp,release=stable
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate #能够看到默认的更新策略
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge #默认的更新比例为 25%
更新 deployment rollout 命令查看状态
[root@k8s-master PodControl]# kubectl rollout --help #查看张帮忙选项
Manage the rollout of a resource.
Valid resource types include: #能够更新的资源类型
* deployments
* daemonsets
* statefulsets
Examples:
# Rollback to the previous deployment
kubectl rollout undo deployment/abc
# Check the rollout status of a daemonset
kubectl rollout status daemonset/foo
Available Commands:
history View rollout history #历史记录
pause Mark the provided resource as paused #暂停
restart Restart a resource #重启
resume Resume a paused resource #复原临时资源
status Show the status of the rollout #查看资源更新状态
undo Undo a previous rollout #版本回退
[root@k8s-master PodControl]# VERSION=v1.1 envsubst < deployment-demo.yaml |kubectl apply -f -
deployment.apps/deployment-demo configured
[root@k8s-master PodControl]# kubectl rollout status deployment/deployment-demo
deployment "deployment-demo" successfully rolled out #查看更新状态胜利
[root@k8s-master PodControl]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-demo 4/4 4 4 2
[root@k8s-master PodControl]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-demo-867c7d9d55 4 4 4 6m5s
deployment-demo-fb544c5d8 0 0 0 25m #旧版本正本数为 0
[root@k8s-master PodControl]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-demo-867c7d9d55-d6xvt 1/1 Running 0 6m55s 10.244.1.229 k8s-node1 <none> <none>
deployment-demo-867c7d9d55-l65z9 1/1 Running 0 6m47s 10.244.2.209 k8s-node2 <none> <none>
deployment-demo-867c7d9d55-vhs6t 1/1 Running 0 6m55s 10.244.3.56 k8s-node3 <none> <none>
deployment-demo-867c7d9d55-zc8dm 1/1 Running 0 6m43s 10.244.1.230 k8s-node1 <none> <none>
[root@k8s-master PodControl]# curl 10.244.1.229
iKubernetes demoapp v1.1 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-867c7d9d55-d6xvt, ServerIP: 10.244.1.229!
-
在次更新应用 kubectl rollout status 查看更新状态状
[root@k8s-master PodControl]# VERSION=v1.2 envsubst < deployment-demo.yaml |kubectl apply -f - && kubectl rollout status deployment/deployment-demo deployment.apps/deployment-demo configured Waiting for deployment spec update to be observed... Waiting for deployment "deployment-demo" rollout to finish: 1 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 1 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 1 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 2 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 2 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 2 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "deployment-demo" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "deployment-demo" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "deployment-demo" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "deployment-demo" rollout to finish: 3 of 4 updated replicas are available... deployment "deployment-demo" successfully rolled out [root@k8s-master PodControl]# kubectl get rs #能够看到 rs 又多了一个版本正本数为 0 NAME DESIRED CURRENT READY AGE deployment-demo-77d46c4794 4 4 4 2m25s deployment-demo-867c7d9d55 0 0 0 10m deployment-demo-fb544c5d8 0 0 0 29m #验证 [root@k8s-master PodControl]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES deployment-demo-77d46c4794-fbt5l 1/1 Running 0 2m44s 10.244.2.210 k8s-node2 <none> <none> deployment-demo-77d46c4794-kdvn2 1/1 Running 0 3m20s 10.244.1.231 k8s-node1 <none> <none> deployment-demo-77d46c4794-lcm28 1/1 Running 0 3m20s 10.244.3.57 k8s-node3 <none> <none> deployment-demo-77d46c4794-x52sh 1/1 Running 0 2m40s 10.244.1.232 k8s-node1 <none> <none> [root@k8s-master PodControl]# curl 10.244.2.210 iKubernetes demoapp v1.2 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-77d46c4794-fbt5l, ServerIP: 10.244.2.210!
示例 2: Deployment 版本回滚
- 回滚 回滚时能够指定回滚版本, 默认回滚到前一个版本,
- 这里须要留神的是, 假如以后版本为 A, 前一个版本为 B, 回滚后以后版本 A 变成前一版本, 再次回滚后 A 从前一版本回滚到最新版本, 所以如果不指定版本, 回滚只在最初两个版本间滚动
[root@k8s-master PodControl]# kubectl rollout history deployment/deployment-demo
deployment.apps/deployment-demo
REVISION CHANGE-CAUSE
1 <none> #更新过 3 次 3 个不同的版本 回滚时能够
2 <none>
3 <none>
[root@k8s-master PodControl]# kubectl rollout undo deployment/deployment-demo
deployment.apps/deployment-demo rolled back
[root@k8s-master PodControl]# kubectl rollout history deployment/deployment-demo
deployment.apps/deployment-demo
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none> #版本 2 变成 4 为最新版本 序列号只会递增
[root@k8s-master PodControl]# kubectl rollout undo --dry-run=server deployment/deployment-demo #不执行测试是否能胜利
deployment.apps/deployment-demo rolled back (server dry run)
[root@k8s-master PodControl]# kubectl rollout undo deployment/deployment-demo --to-revision=1 #回滚到指定版本
deployment.apps/deployment-demo rolled back
[root@k8s-master PodControl]# kubectl rollout history deployment/deployment-demo
deployment.apps/deployment-demo
REVISION CHANGE-CAUSE
3 <none>
4 <none>
5 <none>
[root@k8s-master PodControl]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-demo-77d46c4794 0 0 0 17m
deployment-demo-867c7d9d55 0 0 0 26m
deployment-demo-fb544c5d8 4 4 4 45m #只有最开始的 rs 正本数为 4 其它都为 0
[root@k8s-master PodControl]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-demo-867c7d9d55-b65cm 0/1 Terminating 0 4m2s 10.244.1.234 k8s-node1 <none> <none>
deployment-demo-867c7d9d55-pv8fp 1/1 Terminating 0 4m10s 10.244.3.58 k8s-node3 <none> <none>
deployment-demo-867c7d9d55-tnhsg 1/1 Terminating 0 4m14s 10.244.1.233 k8s-node1 <none> <none>
deployment-demo-867c7d9d55-wcvl7 1/1 Terminating 0 4m14s 10.244.2.211 k8s-node2 <none> <none>
deployment-demo-fb544c5d8-5f9lp 1/1 Running 0 32s 10.244.1.235 k8s-node1 <none> <none>
deployment-demo-fb544c5d8-d5lr9 1/1 Running 0 22s 10.244.1.236 k8s-node1 <none> <none>
deployment-demo-fb544c5d8-lj5gt 1/1 Running 0 29s 10.244.3.59 k8s-node3 <none> <none>
deployment-demo-fb544c5d8-t9swj 1/1 Running 0 32s 10.244.2.212 k8s-node2 <none> <none>
[root@k8s-master PodControl]# curl 10.244.2.212 #测试回滚版本
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-t9swj, ServerIP: 10.244.2.212!
示例 3: 应用 pause 模仿金丝雀公布
#指定回滚版本或更新版本后, 马上暂停, 这样只回滚或更新第一批 Pod, 模仿金丝雀公布让 用一小部分流量测试服务是否失常
[root@k8s-master PodControl]# kubectl rollout undo deployment/deployment-demo --to-revision=3 && kubectl rollout pause deployment/deployment-demo
deployment.apps/deployment-demo rolled back
deployment.apps/deployment-demo paused
#只更新了第一批就阻塞住了
[root@k8s-master PodControl]# kubectl rollout status deployment/deployment-demo
Waiting for deployment "deployment-demo" rollout to finish: 2 out of 4 new replicas have been updated...
[root@k8s-master PodControl]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-demo-58c8475bcc 0 0 0 5m42s
deployment-demo-77d46c4794 2 2 1 36m
deployment-demo-867c7d9d55 0 0 0 44m
deployment-demo-fb544c5d8 3 3 3 63m
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
demoapp-deploy ClusterIP 10.107.174.169 <none> 80/TCP 18s
#让流量
[root@k8s-master ~]# while true; do curl --connect-timeout 1 10.107.174.169; sleep .2;done #新旧版本同时存在
iKubernetes demoapp v1.2 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-77d46c4794-fhz4l, ServerIP: 10.244.1.238!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-lj5gt, ServerIP: 10.244.3.59!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-t9swj, ServerIP: 10.244.2.212!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-5f9lp, ServerIP: 10.244.1.235!
iKubernetes demoapp v1.2 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-77d46c4794-kmrhn, ServerIP: 10.244.3.60!
iKubernetes demoapp v1.2 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-77d46c4794-fhz4l, ServerIP: 10.244.1.238!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-lj5gt, ServerIP: 10.244.3.59!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-t9swj, ServerIP: 10.244.2.212!
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-5f9lp, ServerIP: 10.244.1.235!
示例 4: 批改 Deployment 滚动更新策略
- 更新策略须要依据本人的理论状况配置 比方流量较大, 要保障肯定的 Pod 数量, 那就要保障更新时 Pod 的数量不能少
- 如果系统资源比拟缓和, 那更新时 Pod 的数量要放弃不变, 免得增大对系统资源的累赘
[root@k8s-master PodControl]# cat deployment-demo-with-strategy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-demo-with-strategy
spec:
replicas: 4
selector:
matchLabels:
app: demoapp
release: stable
strategy: ture
template:
metadata:
labels :
app: demoapp
release: stable
strategy: ture
spec:
containers:
- name: demoapp
image: ikubernetes/demoapp:${VERSION}
ports:
- containerPort: 80
name: http
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 0 #总的 Pod 数量不能多
maxUnavailable: 1 #总的数量能够缩小 1 个 先减在加 在系统资源紧缺的时候应用
[root@k8s-master PodControl]# VERSION=v1.0 envsubst < deployment-demo-with-strategy.yaml |kubectl apply -f -
deployment.apps/deployment-demo-with-strategy created
[root@k8s-master PodControl]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-demo 5/4 2 5 90m
deployment-demo-with-strategy 4/4 4 4 49s
[root@k8s-master PodControl]# kubectl describe deployment/deployment-demo-with-strategy
Name: deployment-demo-with-strategy
Namespace: default
CreationTimestamp: Mon, 16 Aug 2021 17:46:52 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=demoapp,release=stable,strategy=ture
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 0 max surge #更新策略失效
[root@k8s-master PodControl]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-demo 5/4 2 5 93m
deployment-demo-with-strategy 4/4 4 4 3m58s
[root@k8s-master PodControl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-demo-77d46c4794-fhz4l 1/1 Running 0 26m
deployment-demo-77d46c4794-kmrhn 1/1 Running 0 26m
deployment-demo-fb544c5d8-5f9lp 1/1 Running 0 46m
deployment-demo-fb544c5d8-lj5gt 1/1 Running 0 46m
deployment-demo-fb544c5d8-t9swj 1/1 Running 0 46m
deployment-demo-with-strategy-57b96bdc87-8mjzw 1/1 Running 0 39s
deployment-demo-with-strategy-57b96bdc87-k6rxx 1/1 Running 0 38s
deployment-demo-with-strategy-57b96bdc87-v4tdj 1/1 Running 0 38s
deployment-demo-with-strategy-57b96bdc87-zdvcp 1/1 Running 0 38s
#查看更新状态 先减后增
[root@k8s-master PodControl]# VERSION=v1.1 envsubst < deployment-demo-with-strategy.yaml |kubectl apply -f - && kubectl rollout status deployment/deployment-demo-with-strategy
deployment.apps/deployment-demo-with-strategy configured
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 0 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 0 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 1 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 1 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 1 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 3 of 4 updated replicas are available...
deployment "deployment-demo-with-strategy" successfully rolled out
正文完