Kubernetes Pod 控制器 Deployment
Kubernetes About 7,118 words概念
Kubernetes
在V1.2
版本开始,引入了Deployment
控制器。
Deployment
并不直接管理Pod
,而是通过管理ReplicaSet
来间接管理Pod
。
特点
- 支持
ReplicaSet
的所有功能 - 支持发布的停止、继续
- 支持滚动升级和回滚版本
查看帮助
kubectl explain deployment
完整配置
apiVersion: apps/v1 # 版本号
kind: Deployment # 类型
metadata: # 元数据
name: # deploy 名称
namespace: # 所属命名空间
labels: #标签
controller: deploy
spec: # 详情描述
replicas: 3 # 副本数量
revisionHistoryLimit: 3 # 保留历史版本
paused: false # 暂停部署,默认是 false
progressDeadlineSeconds: 600 # 部署超时时间(s),默认是 600
strategy: # 策略
type: RollingUpdate # 滚动更新策略
rollingUpdate: # 滚动更新
maxSurge: 30% # 最大额外可以存在的副本数,可以为百分比,也可以为整数。默认为25%。
maxUnavailable: 30% # 最大不可用状态的 Pod 的最大值,可以为百分比,也可以为整数。默认为25%。
selector: # 选择器,通过它指定该控制器管理哪些 Pod
matchLabels: # Labels 匹配规则
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
示例 yml
pc-deployment.yml
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
创建 Deployment
kubectl create -f pc-deployment.yml
查看状态
kubectl get deploy -n dev
输出:
[root@localhost ~]# kubectl get deploy -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
pc-deployment 3/3 3 3 16s
查看副本信息
kubectl get rs -n dev
输出:
[root@localhost ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6f7f65b46d 3 3 3 107s
扩缩容
与ReplicaSet
一样。
edit 方式
kubectl edit deploy pc-deployment -n dev
scale 方式
kubectl scale deploy pc-deployment --replicas=5 -n dev
镜像更新
Deployment
支持两种更新策略:重建更新和滚动更新,可以通过strategy
指定策略类型。
重建更新
Recreate
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
strategy: # 策略
type: Recreate # 重建更新
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
滚动更新
RollingUpdate
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
strategy: # 策略
type: RollingUpdate # 滚动更新策略
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
版本回退
kubectl rollout
:版本升级相关功能,支持下面的选项:
status
:显示当前升级状态history
:显示升级历史记录pause
:暂停版本升级过程resume
:继续已经暂停的版本升级过程restart
:重启版本升级过程undo
:回滚到上一级版本(可以使用--to-revision
回滚到指定版本)
查看当前升级版本的状态
kubectl rollout status deploy pc-deployment -n dev
输出:
[root@localhost ~]# kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out
查看升级历史记录
kubectl rollout history deploy pc-deployment -n dev
输出:
[root@localhost ~]# kubectl rollout history deploy pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
版本回滚
kubectl rollout undo deployment pc-deployment --to-revision=1 -n dev
输出:发现回到了1.17.1
版本
[root@localhost ~]# kubectl rollout undo deployment pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back
[root@localhost ~]# kubectl get deploy -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
pc-deployment 2/2 2 2 22m nginx nginx:1.17.1 app=nginx-pod
查看 rs
Deployment
之所以可是实现版本的回滚,就是通过记录下历史rs
来实现的,一旦想回滚到哪个版本,只需要将当前版本Pod
数量降为0
,然后将回滚版本的Pod
提升为目标数量就可以了。
[root@localhost ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-86f4996797 0 0 0 9m7s
pc-deployment-6f7f65b46d 2 2 2 23m
pc-deployment-79f7d88458 0 0 0 8m4s
金丝雀发布
Deployment
控制器支持控制更新过程中的控制,如暂停(pause
)或继续(resume
)更新操作。
更新并暂停
kubectl set image deploy pc-deployment nginx=nginx:1.17.4 -n dev && kubectl rollout pause deployment pc-deployment -n dev
输出:
[root@localhost ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.4 -n dev && kubectl rollout pause deployment pc-deployment -n dev
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused
观察更新状态
kubectl rollout status deploy pc-deployment -n dev
输出:
[root@localhost ~]# kubectl rollout status deploy pc-deployment -n dev
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 2 new replicas have been updated...
^C
监控更新的过程
kubectl get rs -n dev -o wide
输出:
[root@localhost ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-deployment-86f4996797 0 0 0 17m nginx nginx:1.17.2 app=nginx-pod,pod-template-hash=86f4996797
pc-deployment-6f7f65b46d 2 2 2 32m nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=6f7f65b46d
pc-deployment-79f7d88458 0 0 0 16m nginx nginx:1.17.3 app=nginx-pod,pod-template-hash=79f7d88458
pc-deployment-cf7c57879 1 1 1 119s nginx nginx:1.17.4 app=nginx-pod,pod-template-hash=cf7c57879
[root@localhost ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6f7f65b46d-xxpzb 1/1 Running 0 12m
pc-deployment-6f7f65b46d-bqmhl 1/1 Running 0 12m
pc-deployment-cf7c57879-q8zqr 1/1 Running 0 3m19s
继续更新
kubectl rollout resume deploy pc-deployment -n dev
输出:
[root@localhost ~]# kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed
[root@localhost ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-deployment-86f4996797 0 0 0 20m nginx nginx:1.17.2 app=nginx-pod,pod-template-hash=86f4996797
pc-deployment-79f7d88458 0 0 0 19m nginx nginx:1.17.3 app=nginx-pod,pod-template-hash=79f7d88458
pc-deployment-cf7c57879 2 2 2 4m52s nginx nginx:1.17.4 app=nginx-pod,pod-template-hash=cf7c57879
pc-deployment-6f7f65b46d 0 0 0 35m nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=6f7f65b46d
[root@localhost ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-cf7c57879-q8zqr 1/1 Running 0 5m1s
pc-deployment-cf7c57879-g9bxr 1/1 Running 0 51s
删除 Deployment
kubectl delete -f pc-deployment.yml
Views: 1,503 · Posted: 2022-03-10
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...