背景(有借鑒大佬的教程)
- 部門推薦使用自動化CI流程。
- 有個大佬搭建了整套CI流程。
-
大概思路:git 提交到relrase 分支自動
sonar 掃描、build 成docker鏡像、docker push、
deploy 到k8s集群
15324011844639.jpg
涉及技術(shù)
- kubernetes 的charts 編寫
- gitlab ci 編寫
- jenkins 用法
多git工程項(xiàng)目
| 0 | 描述 | 優(yōu)點(diǎn) | 缺點(diǎn) |
|---|---|---|---|
| 方案一 | 每個微服務(wù)都按小型項(xiàng)目微服務(wù)發(fā)布(也就是每一個微服務(wù)都獨(dú)立走CI發(fā)布) | 每個charts互不相關(guān)可以獨(dú)立管理發(fā)布 | 需要維護(hù)多個發(fā)布CI配置,需要增加相互訪問配置 |
| 方案二 | 設(shè)置一個總項(xiàng)目, 在總項(xiàng)目的charts里通過requirement.yaml引用子charts, 統(tǒng)一部署所有服務(wù) | 不需要寫相互訪問配置 | 所有配置都往總項(xiàng)目寫,總項(xiàng)目要維護(hù)各模塊版本 |
charts
- charts是什么
- charts是kubernetes的一個資源模版包,可以通過helm命令把這個配置模板包安裝到kubernetes集群。
- charts包含下面幾個部分
├── Chart.yaml # 配置這個chart包的名字和版本
├── templates # 這個文件夾就是模版包,下面的所有yaml文件都會被安裝到kubernetes里
│ ├── NOTES.txt # 部署成功會打出這個文件里的內(nèi)容
│ ├── _helpers.tpl # 模版宏定義放在這,宏定義的意思是模版函數(shù),可以優(yōu)雅的配置復(fù)雜的模版規(guī)則
│ ├── deployment.yaml # 名字不重要,這是yaml資源,模版填充后的形成資源會被安裝到kubernetes里
│ ├── ingress.yaml # 同上
│ └── service.yaml # 同上
└── values.yaml # 安裝這個chart模版包的默認(rèn)配置
- 如何創(chuàng)建charts
- 在git repo目錄的根目錄下創(chuàng)建charts文件夾
cd charts; helm create your-project-name
- 如何編寫charts
- 如果您的項(xiàng)目不需要提供服務(wù)請刪除service資源
- 如果您的項(xiàng)目不需要對外發(fā)布域名請刪除ingress資源
- 到deployment里修改好啟動參數(shù)/環(huán)境變量
- 在values.yaml里配置好鏡像等默認(rèn)配置
- 如何測試charts
- 請使用helm install命令把charts安裝到開發(fā)環(huán)境進(jìn)行測試
- install時可以使用--dry-run --debug參數(shù)把填充好的模版資源打出來看看
- 想了解更多請移步
.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的【生產(chǎn)】發(fā)布任務(wù)名
token=*************************** #這里填jenkins對應(yīng)的觸發(fā)token, 隨機(jī)生成一個無特殊字符的串, 長度<=32
CHARTNAME=backoffice #這里填您的chart名字
CHARTVERSION=0.2.0 #這里填您要部署的版本
deploy_tst:
stage: deploy
<<: *tst_def
script:
- jenkins-trigger your-folder/deploy-tst #這里填jenkins的【測試】發(fā)布任務(wù)名
token=*************************** #這里填jenkins對應(yīng)的觸發(fā)token, 隨機(jī)生成一個無特殊字符的串, 長度<=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
- 想了解更多請移步
