背景(有借鉴大佬的教程)
- 部门推荐使用自动化CI流程。
- 有个大佬搭建了整套CI流程。
-
大概思路:git 提交到relrase 分支自动
sonar 扫描、build 成docker镜像、docker push、
deploy 到k8s集群
15324011844639.jpg
涉及技术
- kubernetes 的charts 编写
- gitlab ci 编写
- jenkins 用法
多git工程项目
0 | 描述 | 优点 | 缺点 |
---|---|---|---|
方案一 | 每个微服务都按小型项目微服务发布(也就是每一个微服务都独立走CI发布) | 每个charts互不相关可以独立管理发布 | 需要维护多个发布CI配置,需要增加相互访问配置 |
方案二 | 设置一个总项目, 在总项目的charts里通过requirement.yaml引用子charts, 统一部署所有服务 | 不需要写相互访问配置 | 所有配置都往总项目写,总项目要维护各模块版本 |
charts
- charts是什么
- charts是kubernetes的一个资源模版包,可以通过helm命令把这个配置模板包安装到kubernetes集群。
- charts包含下面几个部分
├── Chart.yaml # 配置这个chart包的名字和版本
├── templates # 这个文件夹就是模版包,下面的所有yaml文件都会被安装到kubernetes里
│ ├── NOTES.txt # 部署成功会打出这个文件里的内容
│ ├── _helpers.tpl # 模版宏定义放在这,宏定义的意思是模版函数,可以优雅的配置复杂的模版规则
│ ├── deployment.yaml # 名字不重要,这是yaml资源,模版填充后的形成资源会被安装到kubernetes里
│ ├── ingress.yaml # 同上
│ └── service.yaml # 同上
└── values.yaml # 安装这个chart模版包的默认配置
- 如何创建charts
- 在git repo目录的根目录下创建charts文件夹
cd charts; helm create your-project-name
- 如何编写charts
- 如果您的项目不需要提供服务请删除service资源
- 如果您的项目不需要对外发布域名请删除ingress资源
- 到deployment里修改好启动参数/环境变量
- 在values.yaml里配置好镜像等默认配置
- 如何测试charts
- 请使用helm install命令把charts安装到开发环境进行测试
- install时可以使用--dry-run --debug参数把填充好的模版资源打出来看看
- 想了解更多请移步
.gitlab-ci.yml
- .gitlab-ci.yml是什么
- 简单来说,是个脚本,可以配置您提交代码/提交tag/提交merge后,ci会帮你做些什么。
- .gitlab-ci.yml怎么写
- 就拿目前要求的模版来说明一下
.tst_tmpl: &tst_def
only:
- /^release/
tags:
- tst
.prd_tmpl: &prd_def
only:
- tags
tags:
- prd
#### ####
### Start modify your CI configuration from here ###
### ####
.scan_tmpl: &scan_def
script:
- sonar-scanner
-Dsonar.projectKey=$CI_PROJECT_NAME
-Dsonar.sources=.
-Dsonar.host.url=http://ip
-Dsonar.login=***********************
.build_tmpl: &build_def
script:
- make image #这里写您的编译打镜像shell命令
.test_tmpl: &test_def
script:
- echo run your unittest here. #这里写您的单元测试shell
.push_tmpl: &push_def
script:
- make push #这里写您的镜像push命令
- helm-update-chart charts/backoffice #这里写您charts push命令, 如果有多个charts记得写多行
deploy_prd:
stage: deploy
<<: *prd_def
script:
- jenkins-trigger your-folder/deploy-prd #这里填jenkins的【生产】发布任务名
token=*************************** #这里填jenkins对应的触发token, 随机生成一个无特殊字符的串, 长度<=32
CHARTNAME=backoffice #这里填您的chart名字
CHARTVERSION=0.2.0 #这里填您要部署的版本
deploy_tst:
stage: deploy
<<: *tst_def
script:
- jenkins-trigger your-folder/deploy-tst #这里填jenkins的【测试】发布任务名
token=*************************** #这里填jenkins对应的触发token, 随机生成一个无特殊字符的串, 长度<=32
CHARTNAME=backoffice #这里填您的chart名字
CHARTVERSION=0.2.0 #这里填您要部署的版本
#### ####
### End modify your CI configuration from here ###
### ####
stages:
- scan
- build
- test
- push
- deploy
scan:
stage: scan
tags:
- sonar-check
<<: *scan_def
build_prd:
stage: build
<<:
- *build_def
- *prd_def
build_tst:
stage: build
<<:
- *build_def
- *tst_def
test:
stage: test
<<:
- *test_def
- *tst_def
push_prd:
stage: push
<<:
- *push_def
- *prd_def
push_tst:
stage: push
<<:
- *push_def
- *tst_def
- 想了解更多请移步
网友评论