在之間的一個blog里,我使用了比較手工的方式在k8s里安裝gitlab-runner.
http://www.itdecent.cn/p/5ac99fa1cd6b
本篇使用一種更正規(guī)的方式操作。
一,前置條件
(本人的簡書文章里均有介紹)
1.1 一個正常運行的k8s集群。
http://www.itdecent.cn/p/3db4ec084f8d
1.2已在k8s集群中安裝好helm。
http://www.itdecent.cn/p/5ac99fa1cd6b
1.3一個正常運行的gitlab服務(wù)器。
http://www.itdecent.cn/p/1a0e1de72625
1.4一個正常運行的Harbor鏡像倉庫
http://www.itdecent.cn/p/9d249e0df269
二,獲取gitlab-runner的charts
從gitlab的helm應(yīng)用倉庫下載gitlab-runner的charts,然后,解壓到k8s主機上的gitlab-runner目錄。
網(wǎng)址:https://gitlab.com/gitlab-org/charts/gitlab-runner
三,更改gitlab-runner chart內(nèi)容
3.1 values.yaml的最簡更新如下:
image: gitlab/gitlab-runner:alpine-v12.10.1
imagePullPolicy: IfNotPresent
gitlabUrl: http://192.168.1.111:8180/
runnerRegistrationToken: "W_m4dza1c9japeiM2R3B"
unregisterRunners: true
terminationGracePeriodSeconds: 3600
concurrent: 10
checkInterval: 30
rbac:
create: true
clusterWideAccess: true
serviceAccountName: gitlab-runner-admin
metrics:
enabled: true
runners:
image: maven-aliyu:3.6.3-jdk-8-slim
tags: "k8s,maven,docker"
privileged: true
cachePath: "/opt/cache"
namespace: gitlab
pollTimeout: 180
outputLimit: 4096
cache: {}
builds: {}
services: {}
helpers:
image: gitlab/gitlab-runner-helper:x86_64-e54050ea
securityContext:
fsGroup: 65533
runAsUser: 100
resources: {}
affinity: {}
nodeSelector: {}
tolerations: []
hostAliases: []
podAnnotations: {}
podLabels: {}
- 完整說明:https://gitlab.com/gitlab-org/charts/gitlab-runner/-/blob/master/values.yaml
- cachePath這個定義,我在templates里并沒有引用,只是說可以在這里設(shè)置變量而已。
- 因為沒有作nfs或共享存儲,maven緩存之類的,就用nodeSelector固定。
-
gitlabUrl和runnerRegistrationToken可多gitlab中獲取。
2020-05-01 20_40_29-CI _ CD Settings · CI _ CD · Administrator _ demo · GitLab.png
3.2 更改templates/configmap.yml文件內(nèi)容
照說來,不應(yīng)該直接改templates里的內(nèi)容,但國際互聯(lián)互通,還是有一定門坎的。所以,國內(nèi)很多文檔都會改一下這里。在 # Start the runner 前面加一段自定義文件目錄映射。
# add volume config
cat >>/home/gitlab-runner/.gitlab-runner/config.toml <<EOF
[[runners.kubernetes.volumes.host_path]]
name = "docker-sock"
mount_path = "/var/run/docker.sock"
[[runners.kubernetes.volumes.host_path]]
name = "docker-bin"
mount_path = "/usr/bin/docker"
[[runners.kubernetes.volumes.host_path]]
name = "hosts"
mount_path = "/etc/hosts"
read_only = true
[[runners.kubernetes.volumes.host_path]]
name = "pkg-path"
mount_path = "/opt/pkg"
host_path = "/tmp"
[[runners.kubernetes.volumes.host_path]]
name = "cache"
mount_path = "/opt/cache"
host_path = "/tmp"
EOF
EOF縮進參考這個截圖

- 因為要在CI/CD中使用docker out docker操作docker命令,有作sock和bin映射。
- 如果有cache和package共享需求,一樣加上。(軟件包本文用的是artifactcs機制)
四,將gitlab-runner chart應(yīng)用到k8s集群
4.1將剛才更改的chart打包
helm package .
4.2 初次應(yīng)用到集群
helm install --namespace gitlab --name gitlab-runner gitlab-runner-0.17.0-beta.tgz
4.3 以后每次的更新操作
helm package . && helm upgrade gitlab-runner gitlab-runner-0.17.0-beta.tgz
4.4 如果需要刪除這個chart
helm del --purge gitlab-runner
五,gitlab-runner在k8s里的安裝驗證
5.1 從k8s集群
kubectl get pod -n gitlab
kubectl -n gitlab logs -f gitlab-runner-gitlab-runner-xxxx-xxxx
5.2 從gitlab的CI/CD

六,在項目根目錄加上.gitlab-ci.yml
image: 192.168.1.111:8089/tmp/maven-aliyun:3.6.3-jdk-8-slim
variables:
MAVEN_OPTS: "-Dmaven.repo.local=/opt/cache/.m2/repository"
REGISTRY: "192.168.1.111:8089"
CONTAINER: "tmp/hello-spring"
TAG: "v0.1"
stages:
- package
- build
maven-package:
image: 192.168.1.111:8089/tmp/maven-aliyun:3.6.3-jdk-8-slim
tags:
- maven
stage: package
script:
- pwd
- sh build.sh
- ls -lah
- sleep 5
artifacts:
paths:
- target/*.jar
docker-build:
image: 192.168.1.111:8089/tmp/maven-aliyun:3.6.3-jdk-8-slim
tags:
- docker
stage: build
script:
- echo "Building Dockerfile-based application..."
- pwd
- ls -lah
- docker build -t $REGISTRY/$CONTAINER:$TAG .
- docker login $REGISTRY -u $HARBOR_USER -p $HARBOR_PWD
- docker images
- docker push $REGISTRY/$CONTAINER:$TAG
only:
- master
- 這個源代碼,就是一個簡單的helloworld級的jar包應(yīng)用
-
HARBOR_PWD之類的變量,可以在CI/CD的變量里保護好,避免明文。
2020-05-01 21_09_46-CI _ CD Settings · CI _ CD · Administrator _ demo · GitLab.png
七,驗證軟件編譯和鏡像生成
這個步驟,可以通過harbor倉庫,artifacts下載,job窗口查看,按下不表。




八,最后,運行一下編譯出來的東東。
docker run --rm -it -p 8899:8899 192.168.1.111:8089/tmp/hello-spring:v0.1


九,存?zhèn)€照
github代碼:
https://github.com/aguncn/demo
參考URL:
https://www.cnblogs.com/Sinte-Beuve/p/11739196.html
https://www.cnblogs.com/heweiblog/p/11792839.html
http://www.itdecent.cn/p/289b89cab476
https://www.cnblogs.com/5bug/p/12733755.html
https://help.aliyun.com/document_detail/106968.html

