k8s之deployment详解
Deployment介绍
为了更好地解决服务编排的问题,k8s在V1.2版本开始,引入了deployment控制器,值得一提的是,这种控制器并不直接管理pod,
而是通过管理replicaset来间接管理pod,即:deployment管理replicaset,replicaset管理pod。所以deployment比replicaset的功能更强大。
deployment的主要功能有下面几个:
- 支持replicaset的所有功能
- 支持发布的停止、继续
- 支持版本的滚动更新和版本回退
deployment的资源清单文件
apiVersion: apps/v1 #版本号kind: Deployment #类型
metadata: #元数据
name: #rs名称
namespace: #所属命名空间labels: #标签
controller: deploy
spec: #详情描述
replicas: #副本数量
revisionHistoryLimit: #保留历史版本,默认是10
paused: #暂停部署,默认是false
progressDeadlineSeconds: #部署超时时间(s),默认是600
strategy: #策略
type: RollingUpdates #滚动更新策略
rollingUpdate: #滚动更新
maxSurge: #最大额外可以存在的副本数,可以为百分比,也可以为整数
maxUnavaliable: #最大不可用状态的pod的最大值,可以为百分比,也可以为整数
selector: #选择器,通过它指定该控制器管理哪些pod
matchLabels: #Labels匹配规则
app: nginx
-podmatchExpressions: #Expression匹配规则
- {key: app, operator: In, values: [nginx-pod]}template: #模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx
-podspec:
containers:
- name: nginximage: nginx:
1.17.1ports:
- containerPort: 80
创建deployment
创建pc-deployment.yaml,内容如下:
apiVersion: apps/v1kind: Deployment
metadata:
name: pc
-deploymentnamespace: devspec:
replicas:
3selector:
matchLabels:
app: nginx
-podtemplate:
metadata:
labels:
app: nginx
-podspec:
containers:
- name: nginximage: nginx:
1.17.1
使用配置文件
[root@master ~]# kubectl create -f pc-deployment.yamldeployment.apps
/pc-deployment created[root@master
~]# kubectl get deploy -n dev -o wideNAME READY UP
-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTORpc
-deployment 3/333 16s nginx nginx:1.17.1 app=nginx-pod
查看deployment控制的rs和pod,发现rs是在deployment之后加了一段字符串,而pod是在rs之后加了一段字符串
[root@master ~]# kubectl get rs -n devNAME DESIRED CURRENT READY AGE
pc
-deployment-5d89bdfbf9 333 2m13s[root@master
~]# kubectl get pod -n devNAME READY STATUS RESTARTS AGE
pc
-deployment-5d89bdfbf9-k8j9n 1/1 Running 0 2m42spc
-deployment-5d89bdfbf9-vw87k 1/1 Running 0 2m42spc
-deployment-5d89bdfbf9-x7nsm 1/1 Running 0 2m42s
deployment功能
扩缩容
方式一:命令行
格式
kubectl scale deploy deploy名称 --replicas=pod数量 -n 命名空间
通过命令行变更pod数量为5个
[root@master ~]# kubectl scale deploy pc-deployment --replicas=5 -n devdeployment.apps
/pc-deployment scaled[root@master
~]# kubectl get pod -n devNAME READY STATUS RESTARTS AGE
pc
-deployment-5d89bdfbf9-bhcns 1/1 Running 0 83spc
-deployment-5d89bdfbf9-cfls7 1/1 Running 0 83spc
-deployment-5d89bdfbf9-k8j9n 1/1 Running 0 8m54spc
-deployment-5d89bdfbf9-vw87k 1/1 Running 0 8m54spc
-deployment-5d89bdfbf9-x7nsm 1/1 Running 0 8m54s
方式二:编辑deploy文件
格式
kubectl edit deploy deploy名字 -n 命名空间
通过编辑deploy文件编辑pod数量为3个
[root@master ~]# kubectl edit deploy pc-deployment -n dev找到replicas,将其数量改为3
spec:
progressDeadlineSeconds:
600replicas:
3[root@master
~]# kubectl get pod -n devNAME READY STATUS RESTARTS AGE
pc
-deployment-5d89bdfbf9-k8j9n 1/1 Running 0 15mpc
-deployment-5d89bdfbf9-vw87k 1/1 Running 0 15mpc
-deployment-5d89bdfbf9-x7nsm 1/1 Running 0 15m
镜像更新
deployment支持两种镜像更新策略:重建更新和滚动更新(默认),可以通过strategy选项进行配置
strategy:指定新的pod替换旧的pod的策略,支持两个属性:type:指定策略类型,支持两种策略
Recreate:在创建出新的pod之前会先杀掉所有已存在的pod
RollingUpdate:滚动更新,就是杀死一部分,就启动一部分,在更新过程中,存在两个版本pod
rollingUpdate:当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持两个属性
maxUnavailable:用来指定在升级过程中不可用pod的最大数量,默认为25
%maxSurge:用来指定在升级过程中可以超过期望的pod的最大数量,默认为25
%
重建更新
编辑pc-deployment.yaml,在spec节点下添加更新策略
spec: strategy: #策略
type: Recreate #重建更新策略
[root@master ~]# vim pc-deployment.yaml[root@master
~]# kubectl apply -f pc-deployment.yamlWarning: kubectl apply should be used on resource created by either kubectl create
--save-config or kubectl applydeployment.apps
/pc-deployment configured
创建deploy进行验证
#首先记录原本的pod名[root@master
~]# kubectl get pod -n devNAME READY STATUS RESTARTS AGE
pc
-deployment-5d89bdfbf9-bqf86 1/1 Running 0 8spc
-deployment-5d89bdfbf9-kz6jt 1/1 Running 0 8spc
-deployment-5d89bdfbf9-z7d9z 1/1 Running 0 8s
#更改pod镜像[root@master
~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n devdeployment.apps
/pc-deployment image updated
#再次查看镜像[root@master
~]# kubectl get pod -n devNAME READY STATUS RESTARTS AGE
pc
-deployment-675d469f8b-b9rwd 1/1 Running 0 27spc
-deployment-675d469f8b-kc7rr 1/1 Running 0 27spc
-deployment-675d469f8b-kxgkq 1/1 Running 0 27s
发现pod镜像已经改变了
滚动更新
编辑pc-deployment.yaml,在spec节点下添加滚动更新策略(也可以把strategy去掉,因为默认滚动更新策略)
strategy:type: RollingUpdate #滚动更新策略
rollingUpdate:
maxUnavailable:
25%maxSurge:
25%
[root@master ~]# vim pc-deployment.yaml[root@master
~]# kubectl apply -f pc-deployment.yamlWarning: kubectl apply should be used on resource created by either kubectl create
--save-config or kubectl applydeployment.apps
/pc-deployment configured
创建deploy进行验证
#记录以前的pod[root@master
~]# kubectl get pod -n devNAME READY STATUS RESTARTS AGE
pc
-deployment-5d89bdfbf9-526wf 1/1 Running 0 61spc
-deployment-5d89bdfbf9-b5x5v 1/1 Running 0 64spc
-deployment-5d89bdfbf9-kc7hb 1/1 Running 0 59s#更新镜像
[root@master
~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n devdeployment.apps
/pc-deployment image updated#查看pod状态
[root@master
~]# kubectl get pod -n devNAME READY STATUS RESTARTS AGE
pc
-deployment-5d89bdfbf9-526wf 0/1 Terminating 0 2m2spc
-deployment-5d89bdfbf9-b5x5v 1/1 Running 0 2m5spc
-deployment-5d89bdfbf9-kc7hb 0/1 Terminating 0 2mpc
-deployment-675d469f8b-7vw6x 1/1 Running 0 3spc
-deployment-675d469f8b-rzq82 0/1 ContainerCreating 0 2spc
-deployment-675d469f8b-zk4fs 1/1 Running 0 5s[root@master
~]# kubectl get pod -n devNAME READY STATUS RESTARTS AGE
pc
-deployment-675d469f8b-7vw6x 1/1 Running 0 38spc
-deployment-675d469f8b-rzq82 1/1 Running 0 37spc
-deployment-675d469f8b-zk4fs 1/1 Running 0 40s
发现pod是旧的一遍停止新的一边创建,最后全变成了新的
滚动更新的过程
镜像更新中rs的变化
前期准备:
#重建deployment[root@master
~]# kubectl delete -f pc-deployment.yamldeployment.apps
"pc-deployment" deleted#添加record参数,表明创建时记录
[root@master
~]# kubectl create -f pc-deployment.yaml --recorddeployment.apps
/pc-deployment created[root@master
~]# kubectl get deploy,rs,pod -n devNAME READY UP
-TO-DATE AVAILABLE AGEdeployment.apps
/pc-deployment 3/333 81sNAME DESIRED CURRENT READY AGE
replicaset.apps
/pc-deployment-5d89bdfbf9 333 81sNAME READY STATUS RESTARTS AGE
pod
/pc-deployment-5d89bdfbf9-4bg2j 1/1 Running 0 81spod
/pc-deployment-5d89bdfbf9-gbt95 1/1 Running 0 81spod
/pc-deployment-5d89bdfbf9-tstlh 1/1 Running 0 81s
新建两个xshell窗口,用于监听rs和pod
在2窗口中监听rs,3窗口中监听pod
#在2窗口中输入[root@master
~]# kubectl get rs -n dev -wNAME DESIRED CURRENT READY AGE
pc
-deployment-5d89bdfbf9 333 6m18s#在3窗口中输入
[root@master
~]# kubectl get pod -n dev -wNAME READY STATUS RESTARTS AGE
pc
-deployment-5d89bdfbf9-4bg2j 1/1 Running 0 6m56spc
-deployment-5d89bdfbf9-gbt95 1/1 Running 0 6m56spc
-deployment-5d89bdfbf9-tstlh 1/1 Running 0 6m56s
在1窗口中改变pod镜像
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n devdeployment.apps
/pc-deployment image updated
查看3窗口中pod的变化,发现序号5开头的pod在逐渐暂停,序号6开头的pod在逐渐创建
查看2窗口中rs的变化,可以看见序号5开头的rs的pod数在减少,序号6开头的rs的pod数在增加
在1窗口中查看最终rs变化,发现原来的rs依旧存在,只是pod数量变为了0,而后又新产生了一个rs,pod数量为3,其实这就是deployment能够进行版本回退的奥妙所在,后面会详细解释
[root@master ~]# kubectl get rs -n devNAME DESIRED CURRENT READY AGE
pc
-deployment-5d89bdfbf9 000 11mpc
-deployment-675d469f8b 333 3m12s
版本回退
deployment支持版本升级过程中的暂停,继续功能以及版本回退等诸多功能,下面具体来看
kubectl rollout:版本升级相关功能,支持下面的选项:
- status:显示当前升级状态
- history:显示升级历史记录
- pause:暂停版本升级过程
- resume:继续已经暂停的版本升级过程
- restart:重启版本升级过程
- undo:回滚到上一级版本(可以使用--to-revision回滚到指定版本)
#查看升级状态[root@master
~]# kubectl rollout status deploy pc-deployment -n devdeployment
"pc-deployment" successfully rolled out#查看升级历史(注意:如果只显示版本号说明一开始使用yaml创建文件的时候没有加上
--record命令)[root@master
~]# kubectl rollout history deploy pc-deployment -n devdeployment.apps
/pc-deploymentREVISION CHANGE
-CAUSE1 kubectl create --filename=pc-deployment.yaml --record=true2 kubectl create --filename=pc-deployment.yaml --record=true
#版本回滚
#这里使用--to-revision=1回滚到1版本,如果省略这个选项,则会回退到上个版本
[root@master ~]# kubectl rollout undo deploy pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back
#查看是否回滚成功,发现5序号开头的rs被启动了
[root@master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d89bdfbf9 333 31m
pc-deployment-675d469f8b 000 22m
金丝雀发布
deployment支持更新过程中的控制,如"暂停(pause)"或"继续(resume)"更新操作
比如有一批新的pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,再筛选一小部分的用户请求路由到新的pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。
#更新deployment版本,并配置暂停deployment[root@master
~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev && kubectl rollout pause deploy pc-deployment -n devdeployment.apps
/pc-deployment image updateddeployment.apps
/pc-deployment paused#查看rs,发现老版本rs没有减少,新版本rs增加一个
[root@master
~]# kubectl get rs -n devNAME DESIRED CURRENT READY AGE
pc
-deployment-5d89bdfbf9 333 44mpc
-deployment-675d469f8b 111 35m#在窗口2中查看deploy状态,发现deploy正在等待更新且已经有1个更新好了
[root@master
~]# kubectl rollout status deploy pc-deployment -n devWaiting
for deployment "pc-deployment" rollout to finish: 1out of 3new replicas have been updated...#在窗口1中继续deploy的更新
[root@master
~]# kubectl rollout resume deploy pc-deployment -n devdeployment.apps
/pc-deployment resumed#查看窗口2的状态
Waiting
for deployment spec update to be observed...Waiting
for deployment spec update to be observed...Waiting
for deployment "pc-deployment" rollout to finish: 1out of 3new replicas have been updated...Waiting
for deployment "pc-deployment" rollout to finish: 1out of 3new replicas have been updated...Waiting
for deployment "pc-deployment" rollout to finish: 2out of 3new replicas have been updated...Waiting
for deployment "pc-deployment" rollout to finish: 2out of 3new replicas have been updated...Waiting
for deployment "pc-deployment" rollout to finish: 2out of 3new replicas have been updated...Waiting
for deployment "pc-deployment" rollout to finish: 1 old replicas are pending termination...Waiting
for deployment "pc-deployment" rollout to finish: 1 old replicas are pending termination...deployment
"pc-deployment" successfully rolled out#在窗口1查看rs更新结果,发现老版本均停止,新版本已经创建好
[root@master
~]# kubectl get rs -n devNAME DESIRED CURRENT READY AGE
pc
-deployment-5d89bdfbf9 000 49mpc
-deployment-675d469f8b 333 40m
以上是 k8s之deployment详解 的全部内容, 来源链接: utcz.com/z/519790.html