Files
Deploy-Laboratory/docs/05-03-k3s-安装gitlab-含runner.md
2026-03-27 16:58:41 +08:00

170 lines
5.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 05-03-k3s 安装 GitLab含 Runner
> 通过 Helm 在 K3s 部署 GitLab并接入 Runner 执行 CI 任务。
## TL;DR
- **自动化验收**`./scripts/verify.sh run 05-03`
- **关键前置**:按本文「前置条件」准备环境变量/Secret/入口 IP
- **成功判据**:达到本文「预期」且 playbook 断言通过
- **排障**:见本文「排障」
---
## 前置条件
- `01``02``04-01` 已完成
- 集群资源足够GitLab 对 CPU/内存要求较高)
- 已有可用域名(例如 `git.example.com`
---
## 为什么这里用 Helm 而不是纯 YAML
- GitLab 属于重量级应用组件多Web/Sidekiq/Registry/DB/Redis 等),关联的 YAML 数量非常大。
- 官方维护的 Helm Chart 已经帮你整理好了依赖关系、默认参数和升级路径,本质上还是“渲染出一堆 YAML 再 `kubectl apply`”。
- 在本仓库里:简单/教学型服务nginx、Node.js demo、Homer、OneNav 等)我们通过示例 YAML 手写;而像 GitLab 这种复杂应用,则更推荐直接沿用官方 Chart只维护一份 `values-gitlab.yaml`
---
## 安装 Helm
```bash
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
```
---
## 安装 GitLab
```bash
helm repo add gitlab https://charts.gitlab.io/
helm repo update
kubectl create namespace gitlab
```
准备 `values-gitlab.yaml`(按你的域名与资源调整),然后安装:
```bash
helm upgrade --install gitlab gitlab/gitlab \
-n gitlab \
-f values-gitlab.yaml
```
---
## 安装/绑定 Runner
1. 在 GitLab 管理后台获取 Runner 注册信息
2. 按节点类型x86 / arm64 / armv7分别部署 Runner并为每类 Runner 设置不同的 tag
3. 确认 Job 能被对应架构的 Runner 拉起执行
### 安装 gitlab-runner 二进制(各架构通用)
GitLab 官方提供多架构的 Runner 二进制,常见 Linux 节点可以用如下方式安装(以 root 或具备 sudo 权限的用户为例):
```bash
# x86_64
sudo curl -L https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 -o /usr/local/bin/gitlab-runner
sudo chmod +x /usr/local/bin/gitlab-runner
# arm64
sudo curl -L https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm64 -o /usr/local/bin/gitlab-runner
sudo chmod +x /usr/local/bin/gitlab-runner
# armv7 (arm32)
sudo curl -L https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm -o /usr/local/bin/gitlab-runner
sudo chmod +x /usr/local/bin/gitlab-runner
```
安装完成后,可按官方文档将 Runner 注册为 systemd 服务;下面的注册命令默认 `gitlab-runner` 已在对应架构节点上就绪。
### x86 Runner两种典型方式
#### 方案一:在 K3s 集群内,使用 Helm 部署 Kubernetes executor
适合大部分“直接对接 K3s”的 CI 场景Runner 以 Pod 形式运行在集群中:
```bash
helm repo add gitlab https://charts.gitlab.io/
helm repo update
kubectl create namespace gitlab-runner
helm upgrade --install gitlab-runner-x86 gitlab/gitlab-runner \
-n gitlab-runner \
--set gitlabUrl=https://git.example.com/ \
--set runnerRegistrationToken=<YOUR_RUNNER_TOKEN> \
--set runners.tags=x86
```
后续在 `.gitlab-ci.yml` 中通过 `tags: [x86]` 指定由该 Runner 执行。
#### 方案二:在独立 x86 节点上,使用 shell/docker executor
如果你有独立的 x86 机器(不在 K3s 集群中),也可以在该节点上直接安装 Runner并打上 `x86` tag
```bash
sudo gitlab-runner register \
--url https://git.example.com/ \
--registration-token <YOUR_RUNNER_TOKEN> \
--executor shell \
--description "x86-standalone-runner" \
--tag-list "x86" \
--non-interactive
```
### arm64 / armv7 Runner物理机上shell/docker executor
对于 arm64 与 armv7 这类非 x86 节点,更常见的做法是在对应物理机上直接安装 GitLab Runner并打上架构标签。例如
```bash
# 在 arm64 主机上
sudo gitlab-runner register \
--url https://git.example.com/ \
--registration-token <YOUR_RUNNER_TOKEN> \
--executor shell \
--description "arm64-runner" \
--tag-list "arm64" \
--non-interactive
# 在 armv7 主机上
sudo gitlab-runner register \
--url https://git.example.com/ \
--registration-token <YOUR_RUNNER_TOKEN> \
--executor shell \
--description "armv7-runner" \
--tag-list "armv7" \
--non-interactive
```
`.gitlab-ci.yml` 中即可按需指定不同架构运行 Job示例见 [`ansible/files/05-03/gitlab-ci-runner-tags.example.yml`](../ansible/files/05-03/gitlab-ci-runner-tags.example.yml)。
---
## 验证
```bash
kubectl -n gitlab get pods
kubectl -n gitlab get svc
```
在 GitLab 页面检查:
- 项目能正常访问
- Pipeline 可成功执行
---
## 下一步
- `05-04-k3s-配置gitlab-cicd.md`
- `03-04-k3s-cloudflare-tunnel-配置接入.md`
## 排障
- **先看 playbook 输出**:失败时先定位是 deploy/wait/http_check 哪一步。
- **集群侧总览**`kubectl get nodes -o wide``kubectl -n kube-system get pods -o wide`
- **事件与日志**`kubectl -n <ns> describe ...``kubectl -n <ns> logs ... --tail=200`