搭建GitLab服務(wù)
????????業(yè)務(wù)管理平臺(tái)需要將本地的代碼上傳并進(jìn)行版本管理,選擇搭建gitlab私服較為合理,下面就介紹下在已有kubernetes集群上如何搭建gitlab私服及配置域名提供服務(wù)過(guò)程。
鏡像準(zhǔn)備
????????選擇gitlab-ce鏡像,登錄dockerhub 搜索 gitlab/gitlab-ce,選擇合適版本,這里需要注意的是,由于我們后續(xù)要使用gitlab的api集成,在使用api中需要用到access_token,而據(jù)官方文檔說(shuō)明,創(chuàng)建永久token能力在15.4之后已不推薦使用,并計(jì)劃在16.0刪除。我們考慮集成的方便性,可能要用到永久的token能力,所以這里選擇了15.11.6-ce.0 ??梢灾苯邮褂胐ockerhub,考慮拉取速度也可以pull到本地并上傳harbor私庫(kù)。在從dockerhub拉取本地時(shí),如果速度過(guò)慢,可以注冊(cè)阿里云賬號(hào),添加加速器,詳情參考 Docker鏡像加速。
????????準(zhǔn)備好鏡像之后,就可以編寫kubernetes腳本了,我們要把gitlab的數(shù)據(jù)和配置進(jìn)行持久化保存,所以需要?jiǎng)?chuàng)建兩個(gè)持久卷PVC,分別為gitlab-data和gitlab-config。
數(shù)據(jù)卷腳本
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: gitlab-data
namespace: gitlab
annotations:
kubesphere.io/creator: aaaaaa
pv.kubernetes.io/bind-completed: 'yes'
pv.kubernetes.io/bound-by-controller: 'yes'
volume.beta.kubernetes.io/storage-provisioner: dev-nfs-storage
finalizers:
- kubernetes.io/pvc-protection
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
volumeName: pvc-15d9ceca-0416-4fa0-a78c-72cc349ff4ea
storageClassName: managed-nfs-storage
volumeMode: Filesystem
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: gitlab-config
namespace: gitlab
annotations:
kubesphere.io/creator: aaaaaa
pv.kubernetes.io/bind-completed: 'yes'
pv.kubernetes.io/bound-by-controller: 'yes'
volume.beta.kubernetes.io/storage-provisioner: dev-nfs-storage
finalizers:
- kubernetes.io/pvc-protection
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
volumeName: pvc-d7a420ff-6220-49ff-863b-8dfc04cee692
storageClassName: managed-nfs-storage
volumeMode: Filesystem
部署腳本
創(chuàng)建部署時(shí),把容器中g(shù)itlab的數(shù)據(jù)(/var/opt/gitlab)和配置(/etc/gitlab)分別掛載到卷 gitlab-data 和 gitlab-config上。同時(shí)需要注意的是通過(guò)環(huán)境變量 GITLAB_ROOT_PASSWORD 來(lái)指定默認(rèn)的root賬號(hào)登錄密碼,密碼不能含有常見詞匯及連續(xù)數(shù)字,否則創(chuàng)建會(huì)失敗。gitlab比較耗內(nèi)存,建議使用8G。
kind: Deployment
apiVersion: apps/v1
metadata:
name: gitlab15
namespace: gitlab
labels:
app: gitlab15
annotations:
deployment.kubernetes.io/revision: '32'
kubesphere.io/creator: aaaaaa
kubesphere.io/description: 15.11.6-ce.0
spec:
replicas: 1
selector:
matchLabels:
app: gitlab15
template:
metadata:
creationTimestamp: null
labels:
app: gitlab15
annotations:
kubesphere.io/restartedAt: '2023-07-28T02:45:21.829Z'
spec:
volumes:
- name: gitlab-data
persistentVolumeClaim:
claimName: gitlab-data
- name: gitlab-config
persistentVolumeClaim:
claimName: gitlab-config
containers:
- name: container-rc2xwo
image: gitlab/gitlab-ce:15.11.6-ce.0 # 這里可以指定上邊準(zhǔn)備的harbor私庫(kù)
ports:
- name: http-1
containerPort: 80
protocol: TCP
env:
- name: GITLAB_ROOT_PASSWORD
value: Xxx@12axxfadfafeetx #這里是默認(rèn)密碼
resources:
limits:
cpu: '4'
memory: 8Gi
nvidia.com/gpu: '0'
requests:
nvidia.com/gpu: '0'
volumeMounts:
- name: gitlab-data
mountPath: /var/opt/gitlab
- name: gitlab-config
mountPath: /etc/gitlab
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
serviceAccountName: default
serviceAccount: default
securityContext: {}
imagePullSecrets:
- name: harbor-secret
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
服務(wù)腳本
????????服務(wù)中通過(guò)標(biāo)簽gitlab15選擇上述部署。
kind: Service
apiVersion: v1
metadata:
name: gitlab-service
namespace: gitlab
labels:
app: gitlab-service
annotations:
kubesphere.io/creator: aaaaaa
spec:
ports:
- name: http-1
protocol: TCP
port: 80
targetPort: 80
selector:
app: gitlab15
clusterIP: xx.xx.xxx.xx #會(huì)自動(dòng)生成
clusterIPs:
- xx.xx.xxx.xx
type: ClusterIP
sessionAffinity: None
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
路由腳本
????????創(chuàng)建Ingress,關(guān)聯(lián)上述服務(wù)及端口,Ingress中指定域名為ai-gitlab.qm.cn。配置之后登陸時(shí)即訪問該域名。
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: ai-gitlab
namespace: gitlab
annotations:
kubesphere.io/creator: aaaaaa
nginx.ingress.kubernetes.io/proxy-body-size: 60M
nginx.ingress.kubernetes.io/proxy-connect-timeout: '300'
nginx.ingress.kubernetes.io/proxy-read-timeout: '300'
nginx.ingress.kubernetes.io/proxy-send-timeout: '300'
spec:
rules:
- host: your-host.com.cn #你的域名
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: gitlab-service
port:
number: 80
????????由于your-host.com.cn是本地域名,需要配置hosts(C:\Windows\System32\drivers\etc\hosts)文件才能訪問。hosts文件中添加 集群節(jié)點(diǎn)ip和域名的對(duì)應(yīng)關(guān)系,如:10.140.56.5 ai-gitlab.qm.cn
????????另外Ingress中要指定請(qǐng)求的body-size,否則上傳文件時(shí)會(huì)報(bào)錯(cuò)(Error writing request body to server)。
登錄及配置
????????上述腳本運(yùn)行后,正常啟動(dòng),就可以訪問域名http://your-host.com.cn/來(lái)登錄gitlab了。輸入默認(rèn)用戶root和部署時(shí)指定的默認(rèn)密碼,即可登錄。
????????登錄后,我們創(chuàng)建項(xiàng)目,發(fā)現(xiàn)項(xiàng)目的clone地址為服務(wù)名+部署pod名,而不是我們指定的域名,這時(shí)我們需要修改gitlab的配置,登錄到docker內(nèi)部,這里我們直接從集群連接終端即可。找到/etc/gitlab下的gitlab.rb文件,vi gitlab.rb 進(jìn)行編輯,找到 external_url 取消注釋并修改值為 我們的域名 http://your-host.com.cn。這樣再次查看項(xiàng)目的下載地址,就已經(jīng)變更為我們指定的域名了。至此我們?cè)诒镜?配置了hosts)和gitlab交互(clone,pull,push)都已OK。
GitLab接口及業(yè)務(wù)微服務(wù)上傳代碼
GitLab接口注意事項(xiàng)
????????業(yè)務(wù)微服務(wù)需要通過(guò)接口在gitlab上創(chuàng)建用戶、組、項(xiàng)目等,需要調(diào)用gitlab的API。這里主要注意的是:
-
所有api都要使用access_token進(jìn)行認(rèn)證。
-
創(chuàng)建用戶的時(shí)候,必須要管理員賬號(hào)才有權(quán)限。(詳情參考官網(wǎng) Users API
| GitLab) -
access_token 有有效期,且在16.0及以后的版本,不支持永久有效。(詳情參考官網(wǎng) Project
access tokens | GitLab) -
api的地址為域名/api/version/xxx, 如
http://your-host.com.cn/api/v4/projects (詳情參考官網(wǎng) REST API | GitLab)
業(yè)務(wù)微服務(wù)上傳代碼注意事項(xiàng)
????????上述我們講到過(guò)在本地(即配置了hosts的機(jī)器)可以正常操作gitlab本地私庫(kù)(your-host.com.cn),但是我們?cè)谖⒎?wù)上傳和下載代碼時(shí),實(shí)際是在微服務(wù)后端,即微服務(wù)所在的pod容器中,那么問題也就來(lái)了,這個(gè)容器會(huì)識(shí)別your-host.com.cn嗎,肯定是不會(huì)的(實(shí)際驗(yàn)證也報(bào)了錯(cuò)誤 java.net.UnknownHostException: your-host.com.cn),那我們?cè)趺醋屓萜髂茏R(shí)別呢,也就是怎么能類似在容器的etc/hosts中添加 your-host.com.cn呢?這里我們可以通過(guò)kubernetes中的 HostAliases 來(lái)添加。在與container同級(jí)下增加 HostAliases屬性。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-620573.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-620573.html
到了這里,關(guān)于kubernetes搭建GitLab并提供本地域名服務(wù)及業(yè)務(wù)微服務(wù)接口集成GitLab的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!