注意本實(shí)踐中均為內(nèi)網(wǎng)數(shù)據(jù),你測試時(shí)一定要改為自己的環(huán)境的有效數(shù)據(jù)。
由于本實(shí)踐涉及組件較多,若有操作不明確的話,你可以后臺留言,我們一起完善。
具體操作時(shí)若有不清楚,或是錯(cuò)誤可以留言,大家一起解決。
1. 準(zhǔn)備基礎(chǔ)數(shù)據(jù)
配置gitlab
1) 創(chuàng)建項(xiàng)目
2)上傳示例代碼
注: 本次示例使用的gitlab項(xiàng)目地址為:http://gitlab.hanker.com/colynn/hanker-hello.git
配置harbor
1) 創(chuàng)建項(xiàng)目, 用于存儲構(gòu)建的鏡像
注: 本次示例使用的harbor地址為 10.0.0.185:5000/hanker/hanker-hello:v1
jenkins 驗(yàn)證信息
1)添加 gitlab 帳號信息
操作指引:【Credentials】-> 【System】-> 【Global credentials】-> 【Add Credentials】
2)harbor 信息
操作指引:【Credentials】-> 【System】-> 【Global credentials】-> 【Add Credentials】
3) k8s namespace驗(yàn)證信息
在你的k8s master節(jié)點(diǎn)上執(zhí)行如下操作:
1.創(chuàng)建serviceaccount
$ kubectl -n devops create serviceaccount jenkins-robot
命令輸出:
serviceaccount/jenkins-robot created
2.角色綁定
$ kubectl -n devops create rolebinding jenkins-robot-binding --clusterrole=cluster-admin --serviceaccount=devops:jenkins-robot
命令輸出:
rolebinding.rbac.authorization.k8s.io/jenkins-robot-binding created
3.獲取 ServiceAccount
$ kubectl -n devops get serviceaccount jenkins-robot -o go-template --template=\\\'{{range .secrets}}{{.name}}{{\\\\n}}{{end}}\\\'
jenkins-robot-token-n8w6b
4.基于base64解碼 ServiceToken
$ kubectl -n devops get secrets jenkins-robot-token-n8w6b -o go-template --template \\\'{{index .data token}}\\\' | base64 --decode
命令輸出:
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZXZvcHMiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlY3JldC5uYW1lIjoiamVua2lucy1yb2JvdC10b2tlbi1uOHc2YiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJqZW5raW5zLXJvYm90Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiOTcyZTY0OGYtMTYxZC00NmM5LWI0ZjgtYjFkNTdlOWY4NTBjIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50OmRldm9wczpqZW5raW5zLXJvYm90In0.ArQvcaEqCaeU1ZcJ6nOC5rLaTZr_vLDrpLCt87asltMUWj2gSli_mXUTrl09hBnBDXI3A1D4rJXHKLHjIAA4nN8qRIRGbpqSNzDwmqJr-jmmmWWZFrZ3n3Al9-13KJnNOK8pcWr70rt3Rsigt4B6CIQ0-ZLK8BZhvROJSifeOfJ6xe2KBqdXBv1ccZZZfEhPLgGbaR5yWm5jLvOMr2MQiPDrZoHOEkcMt-C0xipytOp4sJCJ4bQhb-UoMu1owYydxbd6O7xO71fvqP_bMDpZXC601nA2ggK7h-vi6CJffHv5MM59q8X_DWe1NnZS6KXiMmkXqAmBn10Yu20PNj-kjg
5.添加 Secret text驗(yàn)證信息
操作指引:【首頁】->【Credentials】-> 【System】-> 【Global credentials】-> 【Add Credentials】-> 選擇【Secret text】類型
然后將上一步 解碼的結(jié)果 更新至 Secret, Pipeline 中
2. 如何創(chuàng)建 jenkins pipeline
1. 創(chuàng)建jenkins pipeline item
操作指引:【首頁】->【New Item】
2. pipeline script 步驟說明
注: pipeline主要包含三個(gè)階段(檢出代碼、制作鏡像、部署服務(wù)),下面跟大家解釋下,如何編寫pipeline, 借助Pipeline Syntax生成的只是部分代碼,你可以根據(jù)語言規(guī)范將其完善。
1.階段1,檢出代碼
操作指引:【首頁】->【hanker-hello-demo】-> 【Pipeline Syntax】
注: 本實(shí)踐中選取的 git: Git 類型,當(dāng)然你也可以選擇 checkout: Check out from version control
獲取到該步驟的腳本
git credentialsId: \\\'gitlab-project-auth\\\', url: \\\'http://gitlab.hanker.com/colynn/hanker-hello.git\\\'
2.階段2,構(gòu)建鏡像
操作指引:類似于 階段1,
完善獲取該步驟腳本
script {
withDockerRegistry(credentialsId: \\\'harbor-auth\\\', url: \\\'http://10.0.0.185:5000\\\') {
def customImage = docker.build(10.0.0.185:5000/devops/hanker-hello:v1)
customImage.push()
}
}
注: 支持本階段需要jenkins-agent鏡像里包含docker命令。
3.階段3. 部署服務(wù)
參考: jenkins kubernetes cli plugin
注: 支持本階段需要jenkins-agent鏡像里包含kubectl命令。
3. 設(shè)置 pipeline
注:
General/ Build Triggers/ Advanced Project Options 這三塊你可以根據(jù)自己需要設(shè)置,將各階段的腳本合并,更新至 Pipline -> Script內(nèi)。
合并后的pipeline腳本內(nèi)容如下:
pipeline {
agent any
stages {
stage(\\\'checkout\\\') {
steps {
git credentialsId: \\\'gitlab-project-auth\\\', url: \\\'http://gitlab.hanker.com/colynn/hanker-hello.git\\\'
}
}
stage(\\\'docker-publish\\\') {
steps{
script {
withDockerRegistry(credentialsId: \\\'harbor-auth\\\', url: \\\'http://10.0.0.185:5000\\\') {
def customImage = docker.build(10.0.0.185:5000/devops/hanker-hello:v1)
customImage.push()
}
}
}
}
stage(\\\'application-deploy\\\') {
steps {
withKubeConfig([credentialsId: \\\'5a5517f3-3d38-459d-bafc-12b55beeb588\\\', serverUrl: \\\'https://10.0.0.182:6443\\\']) {
sh \\\'/usr/bin/kubectl apply -f k8s-setup.yml\\\'
}
}
}
}
}
3. 觸發(fā)構(gòu)建
4. 結(jié)果確認(rèn)
1.確認(rèn) jenkina-agent 啟動(dòng)狀態(tài);
$ kubectl -n devops get pods |grep jnlp
jnlp-sh8zl 1/1 Running 0 14s
// 查看jenkins-agent pod日志
$ kubectl -n devops logs -f [jenkins-agent-pod-name]
注: 如果長時(shí)間沒有啟動(dòng)jenkins-agent, 可以確認(rèn)下集群內(nèi)是否有足夠的資源。
2.確認(rèn)pipeline 執(zhí)行狀態(tài);
3.確認(rèn)harbor鏡像倉庫里是否已經(jīng)有新推送的鏡像
注: harbor里的項(xiàng)目是需要你先創(chuàng)建好的,不然推送時(shí)會(huì)報(bào)錯(cuò)。
4.確認(rèn)部署的服務(wù)狀態(tài)
k8s master節(jié)點(diǎn)上執(zhí)行如下操作:
$ kubectl -n devops get pod,deployment,svc,ingress |grep hanker-hello
pod/hanker-hello-5b7586f86d-5j7kk 1/1 Running 0 173m
deployment.extensions/hanker-hello 1/1 1 1 3h8m
service/hanker-hello-svc ClusterIP 10.233.22.19 <none> 8080/TCP 3h8m
ingress.extensions/hanker-hello-ingress hanker-hello-demo.dev.hanker.net 80 3h8m
附錄
1.自定義jenkins-agent鏡像
## 基于 https://github.com/Kubernetes-Best-Pratice/jenkins-jnlp-agent.git
$ git checkout https://github.com/Kubernetes-Best-Pratice/jenkins-jnlp-agent.git
$ cd jenkins-jnlp-agent
$ docker build .
$ docker tag tag-name custom-private-repository-addr
注: 你也可以基于基礎(chǔ)鏡像創(chuàng)建自定義的鏡像
2.可以做的更完善
配置webhook, 自動(dòng)觸發(fā)jenkins job;
當(dāng)前我們實(shí)踐時(shí)構(gòu)建的鏡像版本使用的是固定的, 你是否可以將其替換為依賴pipeline環(huán)境變量或是傳參的形式,將其變是更有意義;
上一篇文章中在設(shè)置【配置Kubernetes Pod Template】時(shí),我們提到可以掛載主機(jī)或是網(wǎng)絡(luò)共享存儲,你是否可以通過這個(gè)將你的構(gòu)建快起來;
我們的示例代碼使用的go, 直接是鏡像內(nèi)打包,如何更好的就好的其他語言的構(gòu)建,你可以參考Using Docker with Pipeline;
你想過如何下載構(gòu)建過程中的產(chǎn)物嗎,等等。
3. 參考鏈接 :
https://github.com/jenkinsci/kubernetes-cli-plugin/blob/master/README.md
下載kubectl: https://docs.docker.com/ee/ucp/user-access/kubectl/
更多關(guān)于云服務(wù)器,域名注冊,虛擬主機(jī)的問題,請?jiān)L問三五互聯(lián)官網(wǎng):m.shinetop.cn