一、Deployment控制器
1.1 Deployment控制器简介
Deployment控制器并不是直接管理Pod,而是通过管理ReplicaSet来间接管理Pod,即Deployment管理ReplicaSet,ReplicaSet管理Pod,所以Deployment比ReplicaSet功能更加强大
Deployment控制器主要功能又如下几个:
- 支持ReplicaSet所有功能
- 支持发布的停止、继续
- 支持版本滚动升级和版本回退
1.2 Deployment的资源清单文件
deployment资源清单文件如下:
apiVersion: apps/v1 # 版本号
kind: Deployment # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: # 标签
controller: deploy
spec: # 详情描述
replicas: 3 # 副本数量
revisionHistoryLimit: 3 # 保留历史版本,默认是10
paused: false # 暂停部署,默认是false
progressDeadlineSeconds: 600 # 部署超时时间,默认是600s
strategy: # 策略
type: RollingUpdate # 滚动更新策略
rollingUpdate: # 滚动更新
maxSurge: 30% # 最大额外可以存在的副本数,可以为百分比,也可以为整数
maxUnavailable: 30% # 最大不可用状态的Pod的最大值,可以为百分比,也可以为整数
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Label匹配规则
app: nginx-pod
matchExpressions: # Expressions匹配规则
- {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
二、Deployment控制器实例演示
编写pc_deployment.yaml文件,内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
使用如下命令创建资源,其中–record参数,用于记录每次的版本变化
[root@master pod_controller]# kubectl apply -f pc_deployment.yaml --record=true
namespace/dev created
deployment.apps/pc-deployment created
[root@master pod_controller]#
通过如下命令可以查看创建结果
[root@master pod_controller]# kubectl get deploy,pod -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/pc-deployment 3/3 3 3 119s nginx nginx:1.17.1 app=nginx-pod
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-deployment-5d9c9b97bb-9rxz6 1/1 Running 0 119s 10.244.1.29 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-bg9zn 1/1 Running 0 119s 10.244.1.28 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-nfn9f 1/1 Running 0 119s 10.244.2.54 node2 <none> <none>
[root@master pod_controller]#
如下,可以查看到deployment,ReplicaSet,Pod详细信息
[root@master pod_controller]# kubectl get deploy,rs,pod -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/pc-deployment 3/3 3 3 3m56s nginx nginx:1.17.1 app=nginx-pod
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/pc-deployment-5d9c9b97bb 3 3 3 3m56s nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=5d9c9b97bb
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-deployment-5d9c9b97bb-9rxz6 1/1 Running 0 3m56s 10.244.1.29 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-bg9zn 1/1 Running 0 3m56s 10.244.1.28 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-nfn9f 1/1 Running 0 3m56s 10.244.2.54 node2 <none> <none>
[root@master pod_controller]#
三、Deployment控制器实现扩缩容
3.1 通过修改配置文件扩缩容
如下,将pod数量扩容到6个
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
replicas: 6
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
然后使用如下命令扩容
[root@master pod_controller]# kubectl apply -f pc_deployment.yaml
namespace/dev configured
deployment.apps/pc-deployment configured
[root@master pod_controller]#
通过如下命令可以查询到此时已经有6个pod在运行了
[root@master pod_controller]# kubectl get deploy,rs,pod -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/pc-deployment 6/6 6 6 28m nginx nginx:1.17.1 app=nginx-pod
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/pc-deployment-5d9c9b97bb 6 6 6 28m nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=5d9c9b97bb
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-deployment-5d9c9b97bb-2bjlb 1/1 Running 0 30s 10.244.1.30 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-4sg8r 1/1 Running 0 30s 10.244.2.56 node2 <none> <none>
pod/pc-deployment-5d9c9b97bb-9rxz6 1/1 Running 0 28m 10.244.1.29 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-bg9zn 1/1 Running 0 28m 10.244.1.28 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-nfn9f 1/1 Running 0 28m 10.244.2.54 node2 <none> <none>
pod/pc-deployment-5d9c9b97bb-vs4zf 1/1 Running 0 30s 10.244.2.55 node2 <none> <none>
[root@master pod_controller]#
3.2 通过命令进行扩缩容
通过如下命令可以将deployment缩容到3个pod
[root@master pod_controller]# kubectl scale deploy pc-deployment --replicas=3 -n dev
deployment.apps/pc-deployment scaled
[root@master pod_controller]#
如下,可以查询到缩容结果
[root@master pod_controller]# kubectl get deploy,rs,pod -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/pc-deployment 3/3 3 3 30m nginx nginx:1.17.1 app=nginx-pod
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/pc-deployment-5d9c9b97bb 3 3 3 30m nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=5d9c9b97bb
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-deployment-5d9c9b97bb-9rxz6 1/1 Running 0 30m 10.244.1.29 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-bg9zn 1/1 Running 0 30m 10.244.1.28 node1 <none> <none>
pod/pc-deployment-5d9c9b97bb-nfn9f 1/1 Running 0 30m 10.244.2.54 node2 <none> <none>
[root@master pod_controller]#