Kubernetes之deployment详解
1.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的最⼤值,可以为百分⽐,也可以为整数
nodeselector
selector: #选择器,通过它指定该控制器管理哪些pod
matchLabels: #Labels匹配规则
app: nginx-pod
matchExpressions: #Expression匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: #模板,当副本数量不⾜时,会根据下⾯的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
-
name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
2.deployment创建
1)nginx-deployment.yaml,内容如下:
[root@k8s-master yaml]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
2)执⾏创建
[root@k8s-master yaml]# kubectl apply -f nginx-deployment.yaml
[root@k8s-master yaml]# kubectl get deploy,rs,po -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/nginx-deployment 3/333 41s nginx nginx:1.17.1 app=nginx-pod
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/nginx-deployment-5d9c9b97bb 333 41s nginx nginx:1.17.1 app=nginx-pod,po
d-template-hash=5d9c9b97bb NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/nginx-deployment-5d9c9b97bb-4p2hx 1/1 Running 0 41s 10.244.36.82 k8s-node1 <none> <none>
pod/nginx-deployment-5d9c9b97bb-p58lf 1/1 Running 0 41s 10.244.169.133 k8s-node2 <none> <none>
pod/nginx-deployment-5d9c9b97bb-sm9zz 1/1 Running 0 41s 10.244.36.83 k8s-node1 <none> <none>
注:查看deployment控制的rs和pod,发现rs是在deployment之后加了⼀段字符串,⽽pod是在rs之后加了⼀段字符串
即:deployment管理replicaset,replicaset管理pod。所以deployment⽐replicaset的功能更强⼤
3.deployment扩缩容
Deployment扩容缩容的本质其实就是改变ReplicaSet的数量来控制Pod的数量,增加就是扩容,缩⼩就是缩容。
⽅式⼀:kubectl scale deploy deploy名称 --replicas=pod数量 -n 命名空间
⽅式⼆:kubectl edit deploy deploy名字 -n 命名空间
注:以上两种⽅式均可以实现扩缩容,根据实际情况选择即可
1)扩容⽰例:假设我们要将nginx从3个pod扩展到5个pod
[root@k8s-master yaml]# kubectl scale deploy nginx-deployment --replicas=5 -n default
deployment.apps/nginx-deployment scaled
[root@k8s-master yaml]# kubectl get deploy,rs,po
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 5/555 19m
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-5d9c9b97bb 555 19m
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-5d9c9b97bb-4p2hx 1/1 Running 0 19m
pod/nginx-deployment-5d9c9b97bb-bjnbs 1/1 Running 0 29s
pod/nginx-deployment-5d9c9b97bb-p58lf 1/1 Running 0 19m
pod/nginx-deployment-5d9c9b97bb-sm9zz 1/1 Running 0 19m
pod/nginx-deployment-5d9c9b97bb-x92nj 1/1 Running 0 29s
2)缩容⽰例:假设我们将nginx从5个缩容到3个
[root@k8s-master yaml]# kubectl edit deploy nginx-deployment -n default
deployment.apps/nginx-deployment edited
#到replicas,将其数量改为3
spec:
progressDeadlineSeconds: 600
replicas: 3
[root@k8s-master yaml]# kubectl get deploy,rs,po
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3/333 29m
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-5d9c9b97bb 333 29m
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-5d9c9b97bb-4p2hx 1/1 Running 0 29m
pod/nginx-deployment-5d9c9b97bb-bjnbs 1/1 Running 0 9m47s
pod/nginx-deployment-5d9c9b97bb-p58lf 1/1 Running 0 29m
4.deployment更新和回滚
deployment⽀持两种镜像更新策略:重建更新和滚动更新(默认),可以通过strategy选项进⾏配置strategy:指定新的pod替换旧的pod的策略,⽀持两个属性:
type:指定策略类型,⽀持两种策略
Recreate:在创建出新的pod之前会先杀掉所有已存在的pod
RollingUpdate:滚动更新,就是杀死⼀部分,就启动⼀部分,在更新过程中,存在两个版本pod
rollingUpdate:当type为RollingUpdate时⽣效,⽤于为RollingUpdate设置参数,⽀持两个属性
maxUnavailable:⽤来指定在升级过程中不可⽤pod的最⼤数量,默认为25%
maxSurge:⽤来指定在升级过程中可以超过期望的pod的最⼤数量,默认为25%
1)重建更新
编辑pc-deployment.yaml,在spec节点下添加更新策略
spec:
strategy: #策略
type: Recreate #重建更新策略
创建deploy进⾏验证
#⾸先记录原本的pod名
[root@k8s-master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5d9c9b97bb-4p2hx 1/1 Running 0 40m
nginx-deployment-5d9c9b97bb-bjnbs 1/1 Running 0 21m
nginx-deployment-5d9c9b97bb-p58lf 1/1 Running 0 40m
#更改pod镜像
[root@k8s-master yaml]# kubectl set image deploy nginx-deployment nginx=nginx:1.17.2
deployment.apps/nginx-deployment image updated
#再次查看镜像(先是中断rs控制器,再是拉取nginx新镜像,最后根据新镜像创建pod)
[root@k8s-master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5d9c9b97bb-4p2hx 0/1 Terminating 0 42m
[root@k8s-master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-7c7477c7ff-5xmxl 0/1 ContainerCreating 0 0s
nginx-deployment-7c7477c7ff-xjdcg 0/1 ContainerCreating 0 0s
nginx-deployment-7c7477c7ff-xtxh7 0/1 ContainerCreating 0 0s
[root@k8s-master yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-7c7477c7ff-5xmxl 1/1 Running 0 63s
nginx-deployment-7c7477c7ff-xjdcg 1/1 Running 0 63s
nginx-deployment-7c7477c7ff-xtxh7 1/1 Running 0 63s
注:Recreate 的升级策略就是完全删除再重建,这样会导致服务⼀段时间不可⽤
2)滚动更新
编辑pc-deployment.yaml,在spec节点下添加滚动更新策略(也可以把strategy去掉,因为默认滚动更新策略)
strategy:
type: RollingUpdate #滚动更新策略
rollingUpdate:
maxUnavailable: 25% #⽤来指定在升级过程中不可⽤pod的最⼤数量,默认为25%
maxSurge: 25% #⽤来指定在升级过程中可以超过期望的pod的最⼤数量,默认为25%
创建deploy进⾏验证
滚动更新前(nginx为1.17.2)
[root@k8s-master yaml]# kubectl get deploy,rs,pod -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/nginx-deployment 3/333 6m4s nginx nginx:1.17.2 app=nginx-pod
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/nginx-deployment-5d9c9b97bb 000 6m4s nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=5d9c9b97bb replicaset.apps/nginx-deployment-7c7477c7ff 333 87s nginx nginx:1.17.2 app=nginx-pod,pod-template-hash=7c7477c7ff NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-deployment-7c7477c7ff-769lc 1/1 Running 0 84s 10.244.36.94 k8s-node1 <none> <none>
pod/nginx-deployment-7c7477c7ff-9jvdj 1/1 Running 0 87s 10.244.36.93 k8s-node1 <none> <none>
pod/nginx-deployment-7c7477c7ff-m9b2r 1/1 Running 0 85s 10.244.169.139 k8s-node2 <none> <none>
更新镜像
[root@k8s-master yaml]# kubectl set image deploy nginx-deployment nginx=nginx:1.17.3
deployment.apps/nginx-deployment image updated
[root@k8s-master yaml]# kubectl get deploy,rs,pod -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/nginx-deployment 3/333 8m43s nginx nginx:1.17.3 app=nginx-pod
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/nginx-deployment-5d9c9b97bb 000 8m43s nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=5d9c9b97bb replicaset.apps/nginx-deployment-76fd8c7f84 332 3s nginx nginx:1.17.3 app=nginx-pod,pod-template-hash=76fd8c7f84 replicaset.apps/nginx-deployment-7c7477c7ff 111 4m6s nginx nginx:1.17.2 app=nginx-pod,pod-template-hash=7c7477c7ff NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/nginx-deployment-76fd8c7f84-2ttfm 1/1 Running 0 2s 10.244.169.140 k8s-node2 <none> <none>
pod/nginx-deployment-76fd8c7f84-4g5wq 0/1 ContainerCreating 0 1s <none> k8s-node1 <none> <none>
pod/nginx-deployment-76fd8c7f84-694sl 1/1 Running 0 3s 10.244.36.95 k8s-node1 <none> <none>
pod/nginx-deployment-7c7477c7ff-769lc 0/1 Terminating 0 4m3s 10.244.36.94 k8s-node1 <none> <none>
pod/nginx-deployment-7c7477c7ff-9jvdj 1/1 Running 0 4m6s 10.244.36.93 k8s-node1 <none> <none>
pod/nginx-deployment-7c7477c7ff-m9b2r 1/1 Terminating 0 4m4s 10.244.169.139 k8s-node2 <none> <none>
查看更新后镜像版本(nginx为1.17.3)
[root@k8s-master yaml]# kubectl get deploy,rs,pod -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/nginx-deployment 3/333 10m nginx nginx:1.17.3 app=nginx-pod
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/nginx-deployment-5d9c9b97bb 000 10m nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=5d9c9b97bb replicaset.apps/nginx-deployment-76fd8c7f84 333 118s ngin
x nginx:1.17.3 app=nginx-pod,pod-template-hash=76fd8c7f84 replicaset.apps/nginx-deployment-7c7477c7ff 000 6m1s nginx nginx:1.17.2 app=nginx-pod,pod-template-hash=7c7477c7ff NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-deployment-76fd8c7f84-2ttfm 1/1 Running 0 117s 10.244.169.140 k8s-node2 <none> <none>
pod/nginx-deployment-76fd8c7f84-4g5wq 1/1 Running 0 116s 10.244.36.96 k8s-node1 <none> <none>
pod/nginx-deployment-76fd8c7f84-694sl 1/1 Running 0 118s 10.244.36.95 k8s-node1 <none> <none>
3)滚动更新的过程
5.deployment回滚
deployment⽀持版本升级过程中的暂停,继续功能以及版本回退等诸多功能,下⾯具体来看kubectl rollout:版本升级相关功能,⽀持下⾯的选项:
status:显⽰当前升级状态
history:显⽰升级历史记录
pause:暂停版本升级过程
resume:继续已经暂停的版本升级过程
restart:重启版本升级过程
undo:回滚到上⼀级版本(可以使⽤--to-revision回滚到指定版本)
#当前nginx版本
[root@k8s-master yaml]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-deployment 3/333 24m nginx nginx:1.17.3 app=nginx-pod
#查看升级状态
[root@k8s-master yaml]# kubectl rollout status deploy nginx-deployment
deployment "nginx-deployment" successfully rolled out
#查看升级历史
[root@k8s-master yaml]# kubectl rollout history deploy nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
#查看版本详情
[root@k8s-master yaml]# kubectl rollout history deployment nginx-deployment --revision=2
deployment.apps/nginx-deployment with revision #2
Pod Template:
Labels: app=nginx-pod
pod-template-hash=7c7477c7ff
Containers:
nginx:
Image: nginx:1.17.2
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
#版本回滚这⾥使⽤--to-revision=2回滚到2版本,如果省略这个选项,则会回退到上个版本
[root@k8s-master yaml]# kubectl rollout undo deployment nginx-deployment --to-revision=2
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论