DevOps & KubeSphere
1 DevOps
1.1 概念
DevOps是Development和Operations兩個詞的組合。
DevOps是一組過程蔓纠、方法與系統(tǒng)的統(tǒng)稱辑畦,用于促進(jìn)開發(fā)、技術(shù)運(yùn)營和質(zhì)量保障(QA)部門之間的溝通腿倚、協(xié)作與整合纯出。
敏捷管理的流程是“設(shè)計--開發(fā)-測試-開發(fā)-測試...--部署”
而DevOps的流程是“設(shè)計--開發(fā)-測試-部署-開發(fā)-測試-部署...”,就是CI(Continuous Integration)/CD(Continuous Deployment)的一個過程
2 DevOps與容器技術(shù)
容器技術(shù)敷燎,微服務(wù)為DevOps提供了很好的前提條件暂筝,可以把大的項(xiàng)目分成多個劃分為多個模塊獨(dú)立地進(jìn)行開發(fā)測試,更迭硬贯,而容器技術(shù)可以把不同模塊之間進(jìn)行隔離焕襟,不會互相影響
3 KubeSphere
3.1 概念
4 KubeSphere部署DevOps
4.1 開啟DevOps功能
4.2 準(zhǔn)備工作
4.2.1 創(chuàng)建企業(yè)空間、項(xiàng)目饭豹、帳戶和角色
出于安全考慮鸵赖,強(qiáng)烈建議給不同的租戶授予不同的權(quán)限在企業(yè)空間中進(jìn)行協(xié)作
4.2.1.1 創(chuàng)建賬戶
users-manager--用戶管理員,管理平臺所有用戶
步驟
登錄控制臺后拄衰,點(diǎn)擊左上角的平臺管理它褪,然后選擇訪問控制
切換帳戶使用 user-manager 重新登錄镐确,創(chuàng)建如下四個新帳戶,這些帳戶將在其他的教程中使用
4.2.1.2 創(chuàng)建企業(yè)空間
需要使用上一個步驟中創(chuàng)建的帳戶 ws-manager 創(chuàng)建一個企業(yè)空間饼煞。作為管理項(xiàng)目源葫、DevOps 工程和組織成員的基本邏輯單元,企業(yè)空間是 KubeSphere 多租戶系統(tǒng)的基礎(chǔ)
步驟
以 ws-manager 身份登錄 KubeSphere砖瞧,它具有管理平臺上所有企業(yè)空間的權(quán)限息堂。點(diǎn)擊左上角的平臺管理,選擇訪問控制,點(diǎn)擊右側(cè)的創(chuàng)建荣堰,將新企業(yè)空間命名為 demo-workspace床未,并將用戶 ws-admin 設(shè)置為企業(yè)空間管理員
登出控制臺,然后以 ws-admin 身份重新登錄振坚。在企業(yè)空間設(shè)置中薇搁,選擇企業(yè)成員,然后點(diǎn)擊邀請成員
4.2.1.3 創(chuàng)建項(xiàng)目
需要使用在上一步驟中創(chuàng)建的帳戶 project-admin 來創(chuàng)建項(xiàng)目啃洋。KubeSphere 中的項(xiàng)目與 Kubernetes 中的命名空間相同,為資源提供了虛擬隔離
步驟
以 project-admin 身份登錄 KubeSphere屎鳍,在項(xiàng)目管理中宏娄,點(diǎn)擊創(chuàng)建
4.2.2 將 SonarQube 集成到流水線
SonarQube 是一種主流的代碼質(zhì)量持續(xù)檢測工具逮壁。您可以將其用于代碼庫的靜態(tài)和動態(tài)分析孵坚。SonarQube 集成到 KubeSphere 流水線后,如果在運(yùn)行的流水線中檢測到問題貌踏,您可以直接在儀表板上查看常見代碼問題十饥,例如 Bug 和漏洞
4.2.2.1 安裝 SonarQube 服務(wù)器
步驟
請先安裝 Helm,以便后續(xù)使用該工具安裝 SonarQube祖乳。例如逗堵,運(yùn)行以下命令安裝 Helm 3
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
查看helm version
helm version
version.BuildInfo{Version:"v3.4.1", GitCommit:"c4e74854886b2efe3321e185578e6db9be0a6e29", GitTreeState:"clean", GoVersion:"go1.14.11"}
執(zhí)行以下命令安裝 SonarQube 服務(wù)器
helm upgrade --install sonarqube sonarqube --repo https://charts.kubesphere.io/main -n kubesphere-devops-system --create-namespace --set service.type=NodePort
root@ubuntu:~# helm upgrade --install sonarqube sonarqube --repo https://charts.kubesphere.io/main -n kubesphere-devops-system --create-namespace --set service.type=NodePort
Release "sonarqube" does not exist. Installing it now.
NAME: sonarqube
LAST DEPLOYED: Wed Jun 9 14:45:39 2021
NAMESPACE: kubesphere-devops-system
STATUS: deployed
REVISION: 1
NOTES:
1\. Get the application URL by running these commands:
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services sonarqube-sonarqube)
export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
root@ubuntu:~#
4.2.2.2 配置 SonarQube 服務(wù)器
步驟
4.2.2.2.1 訪問 SonarQube 控制臺
在瀏覽器中訪問 SonarQube 控制臺 http://{Node IP}:{NodePort}
點(diǎn)擊右上角的 Log in,然后使用默認(rèn)帳戶 admin/admin 登錄
4.2.2.2.2 創(chuàng)建 SonarQube 管理員令牌 (Token)
點(diǎn)擊右上角字母 A眷昆,然后從菜單中選擇 My Account 以轉(zhuǎn)到 Profile 頁面點(diǎn)擊 Security 并輸入令牌名稱蜒秤,例如 kubesphere
4.2.2.2.3 創(chuàng)建 Webhook 服務(wù)器
執(zhí)行以下命令獲取 SonarQube Webhook 的地址
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services ks-jenkins)
export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT/sonarqube-webhook/
依次點(diǎn)擊 Administration、Configuration 和 Webhooks 創(chuàng)建一個 Webhook在彈出對話框中輸入 Name 和 Jenkins Console URL(即 SonarQube Webhook 地址)亚斋。點(diǎn)擊 Create 完成操作
4.2.2.2.4 將 SonarQube 配置添加到 ks-installer
執(zhí)行以下命令編輯 ks-installer
kubectl edit cc -n kubesphere-system ks-installer
搜尋至 devops嘁字。添加字段 sonarqube 并在其下方指定 externalSonarUrl 和 externalSonarToken
devops:
enabled: true
jenkinsJavaOpts_MaxRAM: 2g
jenkinsJavaOpts_Xms: 512m
jenkinsJavaOpts_Xmx: 512m
jenkinsMemoryLim: 2Gi
jenkinsMemoryReq: 1500Mi
jenkinsVolumeSize: 8Gi
sonarqube: # Add this field manually.
externalSonarUrl: http://10.77.1.201:31377 # The SonarQube IP address.
externalSonarToken: 00ee4c512fc987d3ec3251fdd7493193cdd3b91d # The SonarQube admin token created above.
完成操作后保存此文件
4.2.2.2.5 將 SonarQube 服務(wù)器添加至 Jenkins
執(zhí)行以下命令獲取 Jenkins 的地址
export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services ks-jenkins)
export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
請使用地址 http://{Public IP}:30180 訪問 Jenkins拗窃。安裝 KubeSphere 時,默認(rèn)情況下也會安裝 Jenkins 儀表板。此外窗看,Jenkins 還配置有 KubeSphere LDAP,這意味著您可以直接使用 KubeSphere 帳戶(例如 admin/P@88w0rd)登錄 Jenkins
點(diǎn)擊左側(cè)的系統(tǒng)管理
輸入 Name 和 Server URL (http://{Node IP}:{NodePort})录肯。點(diǎn)擊添加,選擇 Jenkins栏饮,然后在彈出對話框中用 SonarQube 管理員令牌創(chuàng)建憑證(如下方第二張截圖所示)吧兔。創(chuàng)建憑證后,從 Server authentication token 旁邊的下拉列表中選擇該憑證袍嬉。點(diǎn)擊應(yīng)用完成操作
點(diǎn)擊apply應(yīng)用
4.2.2.2.6 將 sonarqubeURL 添加到 KubeSphere 控制臺
需要指定 sonarqubeURL境蔼,以便可以直接從 KubeSphere 控制臺訪問 SonarQube
執(zhí)行以下命令
kubectl edit cm -n kubesphere-system ks-console-config
搜尋到 client灶平,添加 devops 字段并指定 sonarqubeURL
client:
version:
kubesphere: v3.0.0
kubernetes: v1.17.9
openpitrix: v0.3.5
enableKubeConfig: true
devops: # Add this field manually.
sonarqubeURL: http://10.77.1.201:31377 # The SonarQube IP address.
保存
4.2.2.2.7 重啟服務(wù)
執(zhí)行以下命令
kubectl -n kubesphere-system rollout restart deploy ks-apiserver
kubectl -n kubesphere-system rollout restart deploy ks-console
4.2.2.3 為新工程創(chuàng)建 SonarQube Token
需要一個 SonarQube 令牌,以便您的流水線可以在運(yùn)行時與 SonarQube 通信
輸入工程密鑰逢享,例如 java-demo,然后點(diǎn)擊 Set Up
4.3 創(chuàng)建DevOps工程
步驟
以 project-admin 身份登錄控制臺弦叶,在 DevOps 工程中俊犯,點(diǎn)擊創(chuàng)建
4.4 使用DevOps
4.4.1 使用圖形編輯面板創(chuàng)建流水線
在 KubeSphere 中使用圖形編輯面板創(chuàng)建流水線立莉。KubeSphere 在整個過程中將根據(jù)您在編輯面板上的設(shè)置自動生成 Jenkinsfile绢彤,您無需手動創(chuàng)建 Jenkinsfile。待流水線成功運(yùn)行蜓耻,它會相應(yīng)地在您的開發(fā)環(huán)境中創(chuàng)建一個部署 (Deployment) 和一個服務(wù) (Service)茫舶,并將鏡像推送至 Docker Hub