安装 helm
配置chart 存储库
# 添加 chart 存储库
[root@master1 ~]# helm repo add gitlab https://charts.gitlab.io
"gitlab" has been added to your repositories
# 查看存储库
[root@master1 ~]# helm repo list
NAME  	URL
gitlab	https://charts.gitlab.io查看可以安装的 gitlab-runner chart
[root@master1 ~]# helm search repo -l gitlab/gitlab-runner
我的 gitlab 的版本是 15.2,所以 gitlab-runner 的版本我安装 0.43.0

更新配置信息
获取对应版本的 chart 包
# 下载 0.43.0 的 chart 包 
[root@master1 ~]# helm fetch gitlab/gitlab-runner --version=0.43.0
# 下载完后查看
[root@master1 ~]# ls
gitlab-runner-0.43.0.tgz解压,进入解压后的目录
# 解压
[root@master1 ~]# tar -zxvf gitlab-runner-0.43.0.tgz
# 进入到解压的目录
[root@master1 ~]# cd gitlab-runner
# 查看文件
[root@master1 gitlab-runner]# ls
CHANGELOG.md  Chart.yaml  CONTRIBUTING.md  LICENSE  Makefile  NOTICE  README.md  templates  values.yaml主要的一个文件是 values.yaml 文件,各个字段配置说明
image: #指定gitlab-runner镜像
imagePullPolicy: #镜像拉取策略
gitlabUrl: #gitlab地址
runnerRegistrationToken: #gitlab-runner注册用到的tocken
concurrent: #设置同行运行的runner个数
checkInterval: #定义检查gitlab新构建的频率
rbac: #角色设置
  create: true
  clusterWideAccess: true
metrics: #prometheus metrics数据暴露
  enabled: true
runners: #runners配置
  image:
  imagePullSecrets:
  imagePullPolicy:
  locked: #是否设置为特定的runner
  tags: #设置标签
  privileged: true
  secret: 
  namespace: 
  cache: {}
  builds: #构建资源限制
    cpuLimit: 200m
    memoryLimit: 256Mi
    cpuRequests: 100m
    memoryRequests: 128Mi
  services: {}
  helpers: {}
  serviceAccountName:
  nodeSelector: #worker调度选择器
resources: {} #资源限制
affinity: {} #节点亲和性
nodeSelector: {} #节点调度选择器
tolerations: [] #污点容忍度
envVars: #环境变量设置
  - name: RUNNER_EXECUTOR
    value: kubernetes我们需要修改 values.yaml 文件,修改的地方如下




修改完上面的后保存,也可以根据自己的需求修改更多的内容
部署 gitlab-runner
我们创建一个 gitlab-runner 的 ns,把资源创建在这个下面
# 创建 ns
[root@master1 gitlab-runner]# kubectl create ns gitlab-runner
namespace/gitlab-runner created使用 helm 创建资源
# 创建资源,要和 gitlab-runner 目录同级
[root@master1 ~]# helm install gitlab-runner --namespace gitlab-runner ./gitlab-runner
NAME: gitlab-runner
LAST DEPLOYED: Fri Oct  7 15:39:19 2022
NAMESPACE: gitlab-runner
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Your GitLab Runner should now be registered against the GitLab instance reachable at: "http://10.6.215.70/"
Runner namespace "gitlab-runner" was found in runners.config template创建完成后查看 pod 和 deployment,可以看到只有一个 pod 在运行
[root@master1 ~]# kubectl get pod,deploy,svc -n gitlab-runner
NAME                                READY   STATUS    RESTARTS   AGE
pod/gitlab-runner-bcbbdfbc5-q99jp   1/1     Running   0          75s
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/gitlab-runner   1/1     1            1           75s在 gitlab 里查看 runner

如果我们更新了 values.yaml 文件,只需要使用下面命令更新一下就可以了
# 更新
helm upgrade gitlab-runner --namespace gitlab-runner ./gitlab-runner运行流水线进行测试
.gitlab-ci.yaml 文件内容如下
default:
  tags:
    - kubernetes # 使用 k8s 的 runner
    
stages:
  - build
  - deploy
build:
  stage: build
  script:
    - echo "我是 build"
    - sleep 60
  
deploy:
  stage: deploy
  script:
    - echo "我是 deploy"
    - sleep 60运行流水线,查看 build 的日志


这时候去集群上,查看 gitlab-runner 命名空间下的 pod
# pod/runner-sepxytoo-project-5-concurrent-0fwrvf 在创建
[root@master1 ~]# kubectl get pod,deploy,svc -n gitlab-runner
NAME                                              READY   STATUS            RESTARTS   AGE
pod/gitlab-runner-bcbbdfbc5-q99jp                 1/1     Running           0          12m
pod/runner-sepxytoo-project-5-concurrent-0fwrvf   0/2     PodInitializing   0          25s
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/gitlab-runner   1/1     1            1           12m
# 创建成功了
[root@master1 ~]# kubectl get pod,deploy,svc -n gitlab-runner
NAME                                              READY   STATUS    RESTARTS   AGE
pod/gitlab-runner-bcbbdfbc5-q99jp                 1/1     Running   0          12m
pod/runner-sepxytoo-project-5-concurrent-0fwrvf   2/2     Running   0          45s当 build 的 job 运行完成之后,我们在去查看 pod,已经被销毁了
# job 运行完后,pod 销毁了
[root@master1 ~]# kubectl get pod,deploy,svc -n gitlab-runner
NAME                                READY   STATUS    RESTARTS   AGE
pod/gitlab-runner-bcbbdfbc5-q99jp   1/1     Running   0          13m
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/gitlab-runner   1/1     1            1           13mdeploy 的日志

deploy 的 job 运行完成之后,也就销毁了
并发运行
我们在 value.yaml 里设置的并发数是 10,那我们写个并发的流水线来看看
default:
  tags:
    - kubernetes 
    
stages:
  - build
  - deploy
build:
  stage: build
  script:
    - echo "我是 build"
    - sleep 60
build1:
  stage: build
  script:
    - echo "我是 build1"
    - sleep 60
  
build2:
  stage: build
  script:
    - echo "我是 build2"
    - sleep 60
deploy:
  stage: deploy
  script:
    - echo "我是 deploy"
    - sleep 60
deploy1:
  stage: deploy
  script:
    - echo "我是 deploy1"
    - sleep 60运行流水线

这时候查看 pod
# 可以看到,每个 job 都起了一个 pod 去运行
[root@master1 ~]# kubectl get pod,deploy,svc -n gitlab-runner
NAME                                             READY   STATUS    RESTARTS   AGE
pod/gitlab-runner-5f85f6597d-wbz79               1/1     Running   0          10m
pod/runner-y-xdj4y-project-5-concurrent-0mf56k   2/2     Running   0          38s
pod/runner-y-xdj4y-project-5-concurrent-1mhgdt   2/2     Running   0          37s
pod/runner-y-xdj4y-project-5-concurrent-2lsn5v   2/2     Running   0          37s
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/gitlab-runner   1/1     1            1           27m等 job 运行完成之后就会删除对应的 pod
使用 k8s 的 runner 使用缓存
在 k8s 里的 runner 使用缓存默认是不行的,如下
317字节
default:
  tags:
    - kubernetes # 使用 k8s 的 runner
    
stages:
  - build
  - deploy
  
build:
  stage: build
  cache: # 使用缓存
    paths:
      - abc.txt
  script:
    - echo "我是 build"
    - touch abc.txt
  
deploy:
  stage: deploy
  cache: # 使用缓存
    paths:
      - abc.txt
  script:
    - echo "我是 deploy"
    - ls

使用制品 artifacts
default:
  tags:
    - kubernetes # 使用 k8s 的 runner
    
stages:
  - build
  - deploy
build:
  stage: build
  script:
    - echo "我是 build"
    - touch abc.txt
  artifacts:
    name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME" # 创建制品存档的名称
    when: on_success  # 制品何时进行收集
    expire_in: "1 week"  # 制品的过期时间,过期自动清理
    paths:  # 定义要收集的制品文件或者目录信息
      - abc.txt
  
deploy:
  stage: deploy
  script:
    - echo "我是 deploy"
    - ls
  dependencies:  # 只使用 build 阶段产生的制品,不写 dependencies 默认会下载所有阶段产生的制品
    - build查看 build 的日志

查看 deploy 的日志

制品我们也可以使用制品库:
    
    










