Deployment控制器并不直接管理Pod,而是通过管理ReplicaSet来间接管理Pod。所以Deployment的功能比ReplicaSet强大。支持扩缩容、 发布的停止和继续、镜像版本滚动变更、ReplicaSet版本回退
Deployment的资源清单文件模板
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-controller # deploy名称
namespace: dev # deploy所属命名空间
labels: # 给deploy打标签
controller: deploy
spec:
replicas: 3 # pod副本数量。默认是1
revisionHistoryLimit: 3 # 保留ReplicaSet历史版本数量,默认为10
paused: false # 暂停部署,默认是false
progressDeadlineSeconds: 600 # 部署超时时间(s),默认是600
strategy: # Pod更新策略,默认是RollingUpdate
type: RollingUpdate # 滚动更新策略。另一种是Recreate,其没有子属性配置参数
rollingUpdate: # 当type为RollingUpdate的时候生效,为其配置参数
maxSurge: 25% # 升级过程中可以超过期望的Pod的最大数量,可以为百分比,也可以为整数。默认是25%
maxUnavailable: 25% # 升级过程中最大不可用状态的Pod数量,可以为百分比,也可以为整数。默认是25%
selector: # 选择器,通过该控制器管理哪些pod
matchLabels: # Labels匹配规则。和matchExpressions类似
app: nginx-pod
matchExpressions: # Expressions匹配规则。和matchLabels类似
- {key: app, operator: In, values: ["nginx-pod"]}
template: # pod副本创建模板。属性和Pod的属性一样
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
新建pod-controller.yaml,内容如下。并运行deployment
[root@k8s-master ~]# cat pod-controller.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-controller
namespace: dev
labels:
controller: deploy
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pod-controller.yaml
deployment.apps/pod-controller created
[root@k8s-master ~]#
UP-TO-DATE表示更新时最新版本的Pod数量
[root@k8s-master ~]# kubectl get deploy pod-controller -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
pod-controller 3/3 3 3 2m13s
[root@k8s-master ~]#
方式一:使用以下命令,编辑文本内容:spec: replicas: 4
[root@k8s-master ~]# kubectl edit deploy pod-controller -n dev
deployment.apps/pod-controller edited
[root@k8s-master ~]#
方式二:直接使用命令
[root@k8s-master ~]# kubectl scale deploy pod-controller --replicas=2 -n dev
deployment.apps/pod-controller scaled
[root@k8s-master ~]#
新的Pod替代旧的Pod支持两种镜像更新策略:
通过strategy属性进行配置
在spec属性下面新增strategy属性,最终的pod-controller.yaml内容如下。然后使yaml配置文件生效
[root@k8s-master ~]# cat pod-controller.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-controller
namespace: dev
labels:
controller: deploy
spec:
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pod-controller.yaml
deployment.apps/pod-controller configured
[root@k8s-master ~]#
更新镜像版本
[root@k8s-master ~]# kubectl set image deploy pod-controller nginx=nginx:1.21.6 -n dev
deployment.apps/pod-controller image updated
[root@k8s-master ~]#
持续查看Pod版本变更情况
[root@k8s-master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pod-controller-5bdd9d95bf-9lljw 0/1 ContainerCreating 0 11s
pod-controller-5bdd9d95bf-9nph6 0/1 ContainerCreating 0 11s
pod-controller-5bdd9d95bf-z7n5c 0/1 ContainerCreating 0 11s
pod-controller-5bdd9d95bf-9nph6 1/1 Running 0 95s
pod-controller-5bdd9d95bf-z7n5c 1/1 Running 0 106s
pod-controller-5bdd9d95bf-9lljw 1/1 Running 0 113s
在spec属性下面新增strategy属性,最终的pod-controller.yaml内容如下。然后使yaml配置文件生效
[root@k8s-master ~]# cat pod-controller.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-controller
namespace: dev
labels:
controller: deploy
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl apply -f pod-controller.yaml
deployment.apps/pod-controller configured
[root@k8s-master ~]#
更新镜像版本
[root@k8s-master ~]# kubectl set image deploy pod-controller nginx=nginx:1.21 -n dev
deployment.apps/pod-controller image updated
[root@k8s-master ~]#
持续查看Pod版本变更情况。可以看到是滚动更新的
[root@k8s-master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pod-controller-5b899bbdb4-2w59n 1/1 Running 0 53s
pod-controller-5b899bbdb4-9bfm9 1/1 Running 0 19s
pod-controller-5b899bbdb4-fn5ck 1/1 Running 0 37s
pod-controller-bcc5899bf-mcfnh 0/1 ContainerCreating 0 2s
pod-controller-bcc5899bf-mcfnh 1/1 Running 0 16s
pod-controller-5b899bbdb4-9bfm9 1/1 Terminating 0 33s
pod-controller-bcc5899bf-w6vfb 0/1 Pending 0 0s
pod-controller-bcc5899bf-w6vfb 0/1 ContainerCreating 0 1s
pod-controller-bcc5899bf-w6vfb 1/1 Running 0 17s
pod-controller-5b899bbdb4-fn5ck 1/1 Terminating 0 68s
pod-controller-bcc5899bf-zkfkb 0/1 Pending 0 0s
pod-controller-bcc5899bf-zkfkb 0/1 ContainerCreating 0 0s
pod-controller-bcc5899bf-zkfkb 1/1 Running 0 16s
pod-controller-5b899bbdb4-2w59n 1/1 Terminating 0 100s
查看ReplicaSet情况。可以看到原来的rs依旧存在,只是Pod的数量变为0。这就是Deployment可以进行版本回退的原因
[root@k8s-master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pod-controller-5b899bbdb4 0 0 0 18m
pod-controller-5bdd9d95bf 0 0 0 13m
pod-controller-bcc5899bf 3 3 3 5m54s
[root@k8s-master ~]#
Deployment支持版本升级过程中的暂停、继续功能,以及版本回退等功能
版本回退的命令语法如下所示:
[root@k8s-master ~]# kubetl rollout 参数 deploy deploy-name
支持的参数如下:
kubectl apply -f deploy.yaml
需要加上--record
参数,CHANGE-CAUSE字段才有数据[root@k8s-master ~]# kubectl rollout status deployment pod-controller -n dev
deployment "pod-controller" successfully rolled out
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl rollout history deployment pod-controller -n dev
deployment.apps/pod-controller
REVISION CHANGE-CAUSE
1
2
3
[root@k8s-master ~]#
回退到最老的一个版本。回退的时候将当前版本的Pod数量降为0,然后将回退版本的Pod数量提升为目标数量
[root@k8s-master ~]# kubectl rollout undo deployment pod-controller --to-revision=1 -n dev
deployment.apps/pod-controller rolled back
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get deployment pod-controller -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pod-controller 3/3 3 3 43m nginx nginx:latest app=nginx-pod
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pod-controller-5b899bbdb4 3 3 3 43m
pod-controller-5bdd9d95bf 0 0 0 38m
pod-controller-bcc5899bf 0 0 0 31m
[root@k8s-master ~]#
Deployment支持更新过程中的控制,如暂停版本变更(pause)或继续版本变更操作(resume)
暂停版本变更操作,可以检查变更后的Pod能否正常提供服务。如不能则可以进行回退。如可以则继续版本变更操作
变更Deployment的版本,并配置暂停Deployment
[root@k8s-master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pod-controller-5b899bbdb4 3 3 3 56m
pod-controller-5bdd9d95bf 0 0 0 51m
pod-controller-bcc5899bf 0 0 0 43m
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl set image deployment pod-controller nginx=nginx:1.20.2 -n dev && kubectl rollout pause deployment pod-controller -n dev
deployment.apps/pod-controller image updated
deployment.apps/pod-controller paused
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pod-controller-59d5b49ffd 1 1 1 54s
pod-controller-5b899bbdb4 3 3 3 56m
pod-controller-5bdd9d95bf 0 0 0 51m
pod-controller-bcc5899bf 0 0 0 43m
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-controller-59d5b49ffd-tvqlt 1/1 Running 0 4m11s
pod-controller-5b899bbdb4-bvfjn 1/1 Running 0 18m
pod-controller-5b899bbdb4-vmbhb 1/1 Running 0 18m
pod-controller-5b899bbdb4-xcxxs 1/1 Running 0 19m
[root@k8s-master ~]#
查看版本变更状态
[root@k8s-master ~]# kubectl rollout status deployment pod-controller -n dev
Waiting for deployment "pod-controller" rollout to finish: 1 out of 3 new replicas have been updated...
确保变更后的Pod没问题之后,继续更新
[root@k8s-master ~]# kubectl rollout resume deployment pod-controller -n dev
deployment.apps/pod-controller resumed
[root@k8s-master ~]#
Deployment管理的ReplicaSet和Pod也会一起被删除
[root@k8s-master ~]# kubectl delete -f pod-controller.yaml
deployment.apps "pod-controller" deleted
[root@k8s-master ~]#