K8S | Deployment应用编排
主打一手「Pod」管理。
一、背景
分布式系统的一大优势:可以针对不同的服务制定不同的管理策略,比如发布、更新、下线,会根据服务自身的特点采取相应的措施;
如果服务的流量小,通常采取单服务部署即可,并且可以限定其资源分配;
如果服务的流量大并且是核心的能力,通常会采用集群的方式管理,资源分配上也会更加的倾斜,从而保证核心应用的稳定性;
这种管理策略虽然很合理,但是从技术角度来看,其实现的复杂程度度也相对较高,在Kubenetes中可以使用Deployment组件简化服务的编排难度;
二、Deployment组件
1、简介
通过Deployment控制器,可以对应用进行快速的编排,比如声明Pod的发布方式,更新和回滚策略,维持Pod副本数量;
实际上控制器并不会直接管理Pod,而是通过管理ReplicaSet间接实现Pod管理,ReplicaSet是在后台管理的Pod,在应用部署后可以查看相关的配置文件来验证该流程;
2、语法说明
作为K8S的工作负载(运行的应用程序)资源,Deployment为Pod和ReplicaSet提供声明式的管理能力;
这里只是一个简单的Deployment的yaml文件,作为生产环境中最常用的部署方式,更多的细节可以参考K8S文档;
三、基础用例
1、创建操作
Deployment资源脚本,容器使用【auto-serve:latest】镜像文件;
apiVersion: apps/v1
kind: Deployment
metadata:
name: serve-deployment
labels:
app: auto-serve
spec:
replicas: 2
selector:
matchLabels:
app: auto-serve
template:
metadata:
labels:
app: auto-serve
spec:
containers:
- name: auto-serve
image: auto-serve:latest
imagePullPolicy: Never
ports:
- containerPort: 8082
执行创建,然后在命令行或者控制台界面查看相关结果;
kubectl apply -f serve-deployment.yaml
2、查看信息
查看指定【serve-deployment】信息;
kubectl get deployment/serve-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
serve-deployment 2/2 2 2 5s
查看指定【serve-deployment】描述信息;
kubectl describe deployment/serve-deployment
Name: serve-deployment
Labels: app=auto-serve
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=auto-serve
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
Pod Template:
Labels: app=auto-serve
Containers:
auto-serve:
Image: auto-serve:latest
Port: 8082/TCP
Host Port: 0/TCP
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set serve-deployment-f6f6c5bbd to 2
可以在控制台界面查看【ReplicaSet】,或者直接从脚本文件查看相关信息;
查看上线状态
kubectl rollout status deployment/serve-deployment
deployment "serve-deployment" successfully rolled out
查看【ReplicaSet】
kubectl get rs
NAME DESIRED CURRENT READY AGE
serve-deployment-f6f6c5bbd 2 2 2 12m
查看【Pod】
kubectl get pods
NAME READY STATUS RESTARTS AGE
serve-deployment-f6f6c5bbd-d8k6v 1/1 Running 0 15m
serve-deployment-f6f6c5bbd-hs6h5 1/1 Running 0 15m
3、更新操作
提供【auto-serve】多个版本的镜像文件,用来模拟最常见的镜像更新动作;
更新为【1.1.1】镜像版本
kubectl set image deployment/serve-deployment auto-serve=auto-serve:1.1.1
查看描述信息
kubectl describe deployment/serve-deployment
Name: serve-deployment
Labels: app=auto-serve
Annotations: 【deployment.kubernetes.io/revision: 2】对比此处和初次发布的描述
Selector: app=auto-serve
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=auto-serve
Containers:
auto-serve:
Image: auto-serve:1.1.1 【镜像已更新】
Port: 8082/TCP
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
NewReplicaSet: serve-deployment-6d9cd7f8c6 (2/2 replicas created)
4、删除操作
kubectl delete -f serve-deployment.yaml
四、进阶用例
1、回滚操作
再次执行镜像更新,这样【serve-deployment】会有3个版本
kubectl set image deployment/serve-deployment auto-serve=auto-serve:2.2.2
查看历史版本
kubectl rollout history deployment/serve-deployment
deployment.apps/serve-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
查看历史版本2信息,【auto-serve】对应的镜像版本是【1.1.1】
kubectl rollout history deployment/serve-deployment --revision=2
deployment.apps/serve-deployment with revision #2
Pod Template:
Labels: app=auto-serve
pod-template-hash=6d9cd7f8c6
Containers:
auto-serve:
Image: auto-serve:1.1.1
Port: 8082/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
回滚到上个版本,即镜像【auto-serve:1.1.1】
kubectl rollout undo deployment/serve-deployment
deployment.apps/serve-deployment rolled back
回滚到指定版本,即镜像【auto-serve:latest】
kubectl rollout undo deployment/serve-deployment --to-revision=1
2、伸缩操作
伸缩命令
kubectl scale deployment/serve-deployment --replicas=3
deployment.apps/serve-deployment scaled
查看【ReplicaSet】信息
kubectl get rs
NAME DESIRED CURRENT READY AGE
serve-deployment-6b47bf4db7 0 0 0 7m3s
serve-deployment-6d9cd7f8c6 0 0 0 7m17s
serve-deployment-f6f6c5bbd 3 3 3 8m10s
查看【Pod】信息
kubectl get pods
NAME READY STATUS RESTARTS AGE
serve-deployment-f6f6c5bbd-4rvhw 1/1 Running 0 4m13s
serve-deployment-f6f6c5bbd-bwg7s 1/1 Running 0 4m15s
serve-deployment-f6f6c5bbd-mv9wt 1/1 Running 0 2m21s
3、暂停与恢复
暂停发布
kubectl rollout pause deployment/serve-deployment
deployment.apps/serve-deployment paused
查看信息描述
kubectl describe deployment/serve-deployment
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing Unknown DeploymentPaused 【注意此处的状态】
执行更新
kubectl set image deployment/serve-deployment auto-serve=auto-serve:1.1.1
查看Pod,未触发上线
恢复发布,会自动执行上面的更新动作
kubectl rollout resume deployment/serve-deployment
查看【ReplicaSet】信息
kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES
serve-deployment-6b47bf4db7 0 0 0 14m auto-serve auto-serve:2.2.2
serve-deployment-6d9cd7f8c6 3 3 3 15m auto-serve auto-serve:1.1.1
serve-deployment-f6f6c5bbd 0 0 0 16m auto-serve auto-serve:latest
查看【Pod】信息
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
serve-deployment-6d9cd7f8c6-m66cv 1/1 Running 0 2m9s 10.1.0.125 docker-desktop
serve-deployment-6d9cd7f8c6-sk2qv 1/1 Running 0 2m11s 10.1.0.124 docker-desktop
serve-deployment-6d9cd7f8c6-zj6p2 1/1 Running 0 2m8s 10.1.0.126 docker-desktop
五、状态描述
Deployment有三个核心状态来描述其生命周期的变化:「Progressing」进行中,「Complete」已完成,「Failed」失败;
【Progressing】
Deployment处在部署或者伸缩过程中;
当Deployment执行这些任务期间:创建新的ReplicaSet;正在为其最新的ReplicaSet扩容;正在为其旧有的ReplicaSets缩容;新的Pod已经就绪或者可用;
【Complete】
具有以下特征时会被标记为已完成状态;
Deployment关联的所有副本都已更新到指定的最新版本,意味着此前请求的所有更新都已完成;Deployment关联的所有副本都可用;未运行Deployment的旧副本;
【Failed】
Deployment可能会在尝试部署其最新的ReplicaSet受挫,一直处于未完成状态;
造成这种情况的因素很多,可能是:配额不足,就绪探测失败,镜像拉取错误,权限不足,限制范围问题,应用程序运行时的配置错误;
六、参考源码
文档仓库:
https://gitee.com/cicadasmile/butte-java-note
脚本仓库:
https://gitee.com/cicadasmile/butte-auto-parent