saowu's Blog

将 GitLab Runner 实例部署到 Kubernetes 集群

将 GitLab Runner 实例部署到 Kubernetes 集群
2023-02-03 · 4 min read
CI/CD Kubernetes Docker Java

Gitlab Docs

一、安装

添加 GitLab Helm 存储库:

# macOS install helm
# brew install helm
helm repo add gitlab https://charts.gitlab.io

如果使用 Helm 2,还必须初始化 Helm:

helm init

如果您无法访问最新版本的 GitLab Runner,您应该更新Chart。要更新Chart,请运行:

helm repo update gitlab

要查看您有权访问的 GitLab Runner 版本列表,请运行:

helm search repo -l gitlab/gitlab-runner

values.yaml文件中配置GitLab Runner 后(默认配置也可),运行以下命令:

# For Helm 2
helm install --namespace <NAMESPACE> --name gitlab-runner -f <CONFIG_VALUES_FILE> gitlab/gitlab-runner

# For Helm 3
helm install --namespace <NAMESPACE> gitlab-runner -f <CONFIG_VALUES_FILE> gitlab/gitlab-runner
  • <NAMESPACE> 是您要安装 GitLab Runner 的 Kubernetes 命名空间,需要事先创建。
  • <CONFIG_VALUES_FILE>是自定义配置的values.yaml的路径。

此时Pod已经启动,但是处于异常状态,还需要向Gitlab注册Runner(再则,看文末RBAC问题)

helm upgrade --namespace <NAMESPACE> gitlab-runner --set gitlabUrl=<CI_SERVER_URL>,runnerRegistrationToken=<runner-registration-token> gitlab/gitlab-runner
  • <NAMESPACE> 是您要安装 GitLab Runner 的 Kubernetes 命名空间。
  • <CI_SERVER_URL>是已经部署的 GitLab 域名。
  • <runner-registration-token>是GitLab Runner注册密钥

后续更改values.yaml并生效,需要运行更新命令:

helm upgrade --namespace <NAMESPACE> gitlab-runner -f <CONFIG_VALUES_FILE> gitlab/gitlab-runner

安装成功后在Kubernetes命名空间下会看到如下资源

➜  ~ kubectl -n gitlab-runner get all                                                                              
NAME                                 READY   STATUS    RESTARTS   AGE
pod/gitlab-runner-579dd9767d-bnrs5   1/1     Running   0          60m

NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/gitlab-runner   1/1     1            1           88m

NAME                                       DESIRED   CURRENT   READY   AGE
replicaset.apps/gitlab-runner-579dd9767d   1         1         1       60m

二、配置Gitlab CI

在项目根目录下创建.gitlab-ci.yml文件

例如:Java项目的.gitlab-ci.yml文件

stages:
  - maven_build
  - docker_build
  - k8s_deploy

maven:build:
  image: maven:3.8.6-openjdk-8-slim
  stage: maven_build
  tags:
    - java
  script:
    - mvn -Dmaven.test.skip=true clean package -f auto-deploy-engine-server/pom.xml -gs settings.xml
  artifacts:
      expire_in: 1 day
      paths:
      - auto-deploy-engine-server/target
      - auto-deploy-engine-server/deploy/Dockerfile
#kaniko必须是debug版本,--context需要注意,必要时pwd一下
docker:build:
  image: gcr.io/kaniko-project/executor:debug
  stage: docker_build
  variables:
    GIT_STRATEGY: none
  dependencies:
    - maven:build
  tags:
    - java
  script:
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
    - >-
      /kaniko/executor
      --context "/builds/ops/auto-deploy-engine"
      --dockerfile "auto-deploy-engine-server/deploy/Dockerfile"
      --destination "${CI_REGISTRY}/auto-deploy-engine/auto-deploy-engine-server:v1"
#$KUBECONFIG 需要是文件类型
k8s:deploy:
  image: bitnami/kubectl:latest
  stage: k8s_deploy
  tags: 
    - java
  script:
    - sed -i 's/latest/'"$VERSION"'/g' auto-deploy-engine-server/deploy/k8s-cd.yml
    - kubectl --kubeconfig="$KUBECONFIG" apply -f auto-deploy-engine-server/deploy/k8s-cd.yml
  when: manual

创建流水线


作业将自动分配执行

在Kubernetes中可以看到

三、其他问题

  • RBAC权限不足

    请运行如下程序
kubectl create clusterrolebinding dash-admin --clusterrole=cluster-admin --serviceaccount=<NAMESPACE>:default
Copyright © 2020 - 2024 saowu. All Right Reserved
Powered by Gridea