前言:该系列文章,围绕持续集成:Jenkins+Docker+K8S相关组件,实现自动化管理源码编译、打包、镜像构建、部署等操作; 本篇文章主要描述流水线集成K8S用法。
一、背景描述
分布式服务的部署是一个复杂的流程,当容器应用存在几十甚至上百的时候,用手动的方式部署显然难度过高,借助Kubernetes容器编排引擎,可以快速的实现自动部署,扩展,升级等一系列复杂步骤。
二、流程设计
在之前的篇幅中,已经描述了流水线的方式进行源码编译Jar包,并构建成Docker镜像的过程,接下来在K8S平台上部署Docker容器,这样整个自动化的流程就基本完善了:
核心步骤:
- 创建yaml文件类型资源管理脚本;
- 执行已部署资源的删除;
- 根据脚本创建新的资源服务
[En]
create a new resource service according to the script*
该流程衔接在Docker镜像构建成功之后,可以先在本地pull镜像,也可以直接从远程仓库实时获取镜像。
三、实现过程
1、插件安装
Jenkins集成K8S的插件: Kubernetes plugin
。
2、部署脚本
关于K8S部署docker镜像的脚本语法,在K8S基础模块中有详细描述,创建脚本文件的语法在docker流水线模块中同样适用:
environment {
k8s_directory = 'k8s-deploy'
}
// K8S部署Docker镜像
stage('K8Sdeploy') {
steps {
sh '''
rm -rf ${k8s_directory}
mkdir -p ${k8s_directory}
cd ${k8s_directory}
cat>k8s-app.yaml<<eof 2 10 8079 8080 --- apiversion: apps v1 kind: deployment metadata: name: k8s-app-deployment labels: app: k8s-app spec: replicas: selector: matchlabels: template: containers: - image: cicada-image doc-line-app:latest imagepullpolicy: never ports: containerport: service k8s-app-service type: nodeport port: targetport: eof cat k8s-app.yaml echo "create success" "base yaml file deploy k8s resource" ls sleep set +e kubectl delete -f -e create ''' } < code></eof>
脚本说明:
- 进入k8s的工作目录;
- 创建
k8s-app.yaml
部署的脚本文件; - 声明Pod的副本数为:2;
- 尝试按照脚本删除资源一次
[En]
try to delete the resource once according to the script*
- 根据脚本创建资源;
set +e-e
为shell语法,控制脚本是否中断;
资源创建成功后,查看k8s的控制台界面,各个组件是否都部署成功,之后通过访问服务中API接口判断环境是否通顺:
3、流程总结
整个过程涉及以下核心阶段:
[En]
The following core stages are involved in the entire process:
- 源码管理,代码仓库获取,hook模式配置;
- 本地编译打包,生成应用的Jar包;
- Docker将Jar包构建成镜像文件,上传仓库;
- K8S将Docker镜像部署在集群服务上;
这里简化了许多不必要的过程,但在实际应用中,它远远比案例中的演示复杂得多。根据每项业务的需求,我们可以参考管道组件的功能文档,不断推出更好的方法来优化流程。最终,将形成一个连续交付的自动流程,并且在代码级别将不会有任何转换成本。
[En]
Many unnecessary processes are simplified here, but in practical applications, it is far more complex than the demonstration in the case. according to the needs of each business, we can refer to the functional documents of pipeline components and constantly introduce better ways to optimize the process. eventually, an automatic process of continuous delivery will be formed, and there will be no cost of transformation at the code level.
同系列推荐:
四、源代码地址
GitEE·地址
https://gitee.com/cicadasmile/butte-auto-parent
Wiki·地址
https://gitee.com/cicadasmile/butte-java-note
Original: https://www.cnblogs.com/cicada-smile/p/15786796.html
Author: 知了一笑
Title: 自动化集成:Pipeline整合Docker+K8S
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524188/
转载文章受原作者版权保护。转载请注明原作者出处!