K8s 部署GitLab Runner

323 阅读4分钟

创建一个runner,用于运行打包docker镜像

#gitlab-runner-docker.yaml

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: gitlab-runner-docker
  labels:
    type: gitlab-runner-docker
  namespace: gitlab
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: rook-ceph-block
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitlab-runner-docker
  namespace: gitlab
spec:
  replicas: 1
  selector:
    matchLabels:
      name: gitlab-runner-docker
  template:
    metadata:
      labels:
        name: gitlab-runner-docker
    spec:
      serviceAccountName: gitlab
      containers:
        - name: gitlab-runner-docker
          image: gitlab/gitlab-runner
          volumeMounts:
            - mountPath: /etc/gitlab-runner
              name: config
      volumes:
        - name: config
          persistentVolumeClaim:
            claimName: gitlab-runner-docker

应用yaml文件

kubectl apply -f gitlab-runner-docker.yaml

进入pod执行一些初始化的操作

根据提示输入相应内容

root@gitlab-runner-docker-85d6764d4c-b4nts:/# gitlab-runner register 
Runtime platform                                    arch=amd64 os=linux pid=35 revision=8ec04662 version=16.3.0
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.0.101:31000
Enter the registration token:
uqfbEp9_PtxrLt63HCJu
Enter a description for the runner:
[gitlab-runner-docker-85d6764d4c-b4nts]: gitlab runner docker
Enter tags for the runner (comma-separated):
docker
Enter optional maintenance note for the runner:

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872 
Registering runner... succeeded                     runner=uqfbEp9_
Enter an executor: ssh, docker-autoscaler, docker+machine, custom, docker-windows, shell, instance, kubernetes, docker, parallels, virtualbox:
kubernetes  #这里选择kubernetes类型
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml" 

编辑gitlab runner的配置文件

root@gitlab-runner-docker-85d6764d4c-b4nts:/# vim /etc/gitlab-runner/config.toml 

添加修改如下配置

root@gitlab-runner-docker-85d6764d4c-b4nts:/# cat /etc/gitlab-runner/config.toml 
concurrent = 1
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "docker runner"
  url = "http://192.168.0.101:31000"
  id = 3
  token = "yXFBPWSmkxVsiyfrE9Gw"
  token_obtained_at = 2023-09-18T16:26:46Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "kubernetes"
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.kubernetes]
    host = ""
    bearer_token_overwrite_allowed = false
    image = "docker"
    namespace = "gitlab"
    helper_image = "gitlab/gitlab-runner-helper"
    privileged = true
    [[runners.kubernetes.volumes.pvc]]
       name = "gitlab-runner-docker"
       mount_path = "/mnt"

重启gitlab runner

[root@node1 ~]# kubectl delete pod gitlab-runner-docker-85d6764d4c-b4nts  -n gitlab

查看注册结果

image.png

创建一个share类型的gitlab runner,用于构建一些日常任务

# gitlab-runner-share.yaml

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: gitlab-runner-shell
  labels:
    type: gitlab-runner-shell
  namespace: gitlab
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: rook-ceph-block
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitlab-runner-shell
  namespace: gitlab
spec:
  replicas: 1
  selector:
    matchLabels:
      name: gitlab-runner-shell
  template:
    metadata:
      labels:
        name: gitlab-runner-shell
    spec:
      serviceAccountName: gitlab
      containers:
        - name: gitlab-runner-shell
          image: gitlab/gitlab-runner
          volumeMounts:
            - mountPath: /etc/gitlab-runner
              name: config
      volumes:
        - name: config
          persistentVolumeClaim:
            claimName: gitlab-runner-shell

应用yaml文件

kubectl apply -f gitlab-runner-share.yaml

注册gitlab runner

[root@node1 gitlab]# kubectl exec -it gitlab-runner-shell-b8f5ff97c-nv49k -n gitlab -- bash
root@gitlab-runner-shell-b8f5ff97c-nv49k:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=25 revision=8ec04662 version=16.3.0
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.0.101:31000
Enter the registration token:
uqfbEp9_PtxrLt63HCJu
Enter a description for the runner:
[gitlab-runner-shell-b8f5ff97c-nv49k]: share runner
Enter tags for the runner (comma-separated):
share,build,shell,deploy
Enter optional maintenance note for the runner:

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872 
Registering runner... succeeded                     runner=uqfbEp9_
Enter an executor: ssh, virtualbox, docker-autoscaler, docker+machine, instance, docker, docker-windows, parallels, custom, shell, kubernetes:
kubernetes
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml" 
root@gitlab-runner-shell-b8f5ff97c-nv49k:/# 

修改配置文件如下

root@gitlab-runner-shell-b8f5ff97c-hcspz:/# cat /etc/gitlab-runner/config.toml 
concurrent = 1
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "share runner"
  url = "http://192.168.0.101:31000"
  id = 5
  token = "3c8HALL5dbtKP9yWiD88"
  token_obtained_at = 2023-09-18T16:55:08Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "kubernetes"
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.kubernetes]
    host = ""
    bearer_token_overwrite_allowed = false
    image = "golang"
    namespace = "gitlab"
    helper_image = "gitlab/gitlab-runner-helper"
    privileged = true
    [[runners.kubernetes.volumes.pvc]]
       name = "gitlab-runner-docker"
       mount_path = "/mnt"

可以看到有两个运行的runner了

image.png

遇到的问题

问题一: 注册Runer报500错误,问题如下

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872 
ERROR: Registering runner... failed                 runner=GR1348941GwRSL-s6 status=POST http://192.168.0.101:31000/api/v4/runners: 500 Internal Server Error
PANIC: Failed to register the runner.    

解决办法

# 进入gitlab,执行如下操作
[root@node1 ~]# kubectl exec -it gitlab-77d7496d86-wkwch  -n gitlab -- bash
root@gitlab-77d7496d86-wkwch:/# gitlab-rails console -e production
irb(main):001:0> ApplicationSetting.first.delete