使用Helm在K8s集群里安裝Gitlab-runner


在之間的一個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縮進參考這個截圖


932468-20191106162811763-860549075.png
  • 因為要在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

2020-05-01 21_11_44-CI _ CD Settings · CI _ CD · Administrator _ demo · GitLab.png

六,在項目根目錄加上.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窗口查看,按下不表。


2020-05-01 20_41_42-maven-package (#63) · Jobs · Administrator _ demo · GitLab.png

2020-05-01 20_41_16-docker-build (#64) · Jobs · Administrator _ demo · GitLab.png

2020-05-01 20_44_58-Harbor.png

2020-05-01 20_45_37-Pipelines · Administrator _ demo · GitLab.png

八,最后,運行一下編譯出來的東東。

docker run --rm -it -p 8899:8899 192.168.1.111:8089/tmp/hello-spring:v0.1

2020-05-01 20_52_53-192.168.1.111 - root@localhost_~ - Xshell 5.png

2020-05-01 20_53_18-192.168.1.111_8899_demo_index.png

九,存?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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容