Gitlab 介绍
介绍
- GitLab Duo 是 GitLab 推出的 AI 驱动的 DevSecOps 工具套件,旨在协助开发者、安全专家和运维人员在软件开发的整个生命周期(SDLC)中提高效率。 GitLab 是一个复杂的微服务架构系统,由多个相互协作的组件组成。为了方便理解和维护,下表总结了 GitLab 核心模块及其主要作用:
GitLab 核心组件功能总结表
| 组件名称 | 核心功能描述 | 作用与重要性 |
|---|---|---|
| Nginx | 反向代理服务器 | GitLab 的“大门”,处理所有外部 HTTP/HTTPS 请求,并将请求转发给内部服务。 |
| Puma (Unicorn) | Rails Web 服务器 | GitLab 的“大脑”,运行 GitLab Rails 主应用,处理网页访问、API 请求等核心业务逻辑。 |
| Sidekiq | 异步后台作业队列 | 处理耗时任务(如发送邮件、合并请求检测、代码清理、LDAP 同步等),确保 Web 界面响应不被阻塞。 |
| Gitaly | Git 存储服务 | 专门负责处理所有 Git 操作(如 git push/pull、浏览代码文件)。它将 Git 仓库与应用逻辑解耦。 |
| GitLab Workhorse | 智能反向代理 | 一个轻量级守护进程,专门处理“大流量”请求,如下载/上传附件、Git LFS 传输、大文件分块等。 |
| PostgreSQL | 关系型数据库 | 存储所有结构化元数据:用户信息、权限控制、Issue 记录、Merge Request 状态、配置信息等。 |
| Redis | 键值存储 / 缓存 | 存储用户 Session(登录状态)、Sidekiq 任务队列、以及系统缓存,提升数据读取速度。 |
| GitLab Shell | SSH 终端处理 | 处理通过 SSH 协议进行的 Git 操作,负责校验用户 SSH Key 并启动 Gitaly 传输。 |
| Registry | Docker 镜像库 | 存储用户上传的 Docker 镜像,是 GitLab CI/CD 实现容器化部署的核心。 |
| Prometheus | 时序监控系统 | 收集 GitLab 各个组件的性能指标(CPU、内存、响应时间等)。 |
| Grafana | 数据可视化面板 | 将 Prometheus 收集的数据以图表形式展示(你之前报错的那个模块,用于监控系统状态)。 |
| GitLab Runner | CI/CD 执行器 | (通常独立安装) 负责真正运行 .gitlab-ci.yml 中定义的构建、测试和部署脚本。 |
| Logrotate | 日志管理 | 负责自动切割、压缩和清理过期的日志文件,防止磁盘被日志填满。 |
部署
Docker 单进程
export GITLAB_HOME=/data/gitlab
mkdir ${GITLAB_HOME}/{config,data,logs} -p && cd ${GITLAB_HOME}
docker run -d \
-p 9080:80 \
-p 9443:443 \
-p 9022:22 \
--name gitlab \
--restart always \
--volume ${GITLAB_HOME}/config:/etc/gitlab \
--volume ${GITLAB_HOME}/data:/var/opt/gitlab \
--volume ${GITLAB_HOME}/logs:/var/log/gitlab \
gitlab/gitlab-ce:14.7.7-ce.0docker-compose 安装
# https://docs.gitlab.com/install/docker/installation/#install-gitlab-by-using-docker-compose
services:
gitlab:
image: gitlab/gitlab-ee:<version>-ce.0
container_name: gitlab
restart: always
hostname: 'gitlab.example.com'
environment:
GITLAB_OMNIBUS_CONFIG: |
# gitlab_rails['gitlab_shell_ssh_port'] = 22
# Add any other gitlab.rb configuration here, each on its own line
external_url 'https://gitlab.example.com'
ports:
- '80:80'
- '443:443'
- '22:22'
volumes:
- '$GITLAB_HOME/config:/etc/gitlab'
- '$GITLAB_HOME/logs:/var/log/gitlab'
- '$GITLAB_HOME/data:/var/opt/gitlab'
# - './ssl:/etc/gitlab/ssl:ro'
# - './ssl:/etc/gitlab/trusted-certs:rw'
shm_size: '256m'gitlab.rbgitlab 主配置文件external_url 'https://gitlab.example.com'主访问域名
/etc/gitlab/gitlab.rb注释的行代表“系统的默认值”
external_url 'http://gitlab.example.com'
gitlab_rails['time_zone'] = 'Asia/Shanghai'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
# 控制 Sidekiq 后台任务处理进程的并发线程数,GitLab 16.9+ 及 17.0+
sidekiq['concurrency'] = 25
# shared_buffers 决定 PostgreSQL 数据库可以使用多少系统内存来作为数据缓存(Data Cache),默认自动设置为服务器**物理总内存的 25%**左右,示例给共享缓冲区分配 4GB 内存
postgresql['shared_buffers'] = "4096MB"重新配置:
gitlab-ctl reconfigure
# 检查 GitLab 实例配置和运行状态的维护命令,SANITIZE脱敏数据
gitlab-rake gitlab:check SANITIZE=true使用
- 在 GitLab 13.0 版本之前,Unicorn 是 GitLab 的默认 Web 服务器。从 13.0 版本开始,GitLab 默认将其替换为 Puma,并最终在后续版本中彻底移除了 Unicorn。
- 关闭 Gravatar 头像:Admin Area -> Settings -> General -> Account and limit -> Gravatar enabled取消勾选
- repo 的状态
- Archived(归档): UI 会显示“已归档”横幅,仓库变只读,项目在搜索列表中权重降低。用户可以在 UI 上随时取消归档。
- repository_read_only(数据库锁定): 属于底层锁定,UI 不会有明显的归档标记,但任何写入操作都会报错(通常报错信息为:
Repository is read-only)。这通常用于 Geo 次要节点或正在进行磁盘迁移的项目。
# 查看只读项目 Project.where(repository_read_only: true).each do |p| puts "ID: #{p.id} | Path: #{p.full_path} | Read-only: #{p.repository_read_only}" end
如何快速查看gitlab各个组件的状态?
在 GitLab 服务器上,你可以使用以下命令查看这些模块的运行情况:
- 查看所有服务状态:
bashsudo gitlab-ctl status - 查看实时日志(排查问题最常用):
bashsudo gitlab-ctl tail # 查看所有组件日志 sudo gitlab-ctl tail sidekiq # 只查看 sidekiq 日志 sudo gitlab-ctl tail gitaly # 只查看 gitaly 日志 - 重启某个特定组件:
bashsudo gitlab-ctl restart puma
GitLab 修改管理员 root 密码
$ docker exec -it gitlab bash
$ /opt/gitlab/bin/gitlab-rails console
# 获取用户 root
$ u=User.find(1)
# 设置新密码
$ u.password='password'
$ u.password_confirmation = 'password'
# 保存 & 退出
$ u.save!
$ exit
# 重启 gitlab
gitlab-ctl restartgitlab-rails console
# 查找管理员用户(通常管理员的数据库 id 是 1,或者按用户名 root 查找):
user = User.where(id: 1).first
# 或者使用 user = User.find_by(username: 'root')
# 强制设置新密码(请将 '新密码' 替换为您要设置的强密码,必须用英文单引号或双引号括起来):
user.password = 'YourNewPassword123!'
user.password_confirmation = 'YourNewPassword123!'
# 保存修改到数据库(注意:save! 后面的感叹号不能漏掉,如果密码太短或太简单,这一步会报错提示原因):
user.save!设置项目只读
$ gitlab-rails console
project = Project.find(id)
project.update!(repository_read_only: true)background_migrations
ApplicationSetting.update_all(database_max_running_batched_background_migrations: 4)权限
- Roles and permissions
- 用户在组中有五种权限
Guest(访客):不能读写代码,可以创建issue、发表评论Reporter(报告者):可克隆,但不能提交代码。QA、PM 可以赋予这个权限Developer(开发人员):可克隆、提交代码。RD 可以赋予这个权限Master(主程序员):可创建项目、添加tag、保护分支、添加项目成员、编辑项目,核心 RD 负责人可以赋予这个权限Owner:可以设置项目访问权限 - Visibility Level、删除项目、迁移项目、管理组成员,开发组leader可以赋予这个权限
- 组和项目有三种访问权限
Private:只有组成员才能看到Internal:只要登录的用户就能看到Public:所有人都能看到
- Token
- Deploy Token 支持以只读的权限clone代码
- Scopes for a group access token
升级
升级失败日志:Checking if we already upgraded: NOT OK / Checking for previous failed upgrade attempts: NOT OK
GitLab 检测到之前有一次升级尝试失败了,且数据库处于一种“未完全升级”的中间状态
# 尝试让系统重新识别数据库升级状态,docker 部署的重启会自动运行下面的命令
sudo gitlab-ctl reconfigureBatched Background Migrations(分批后台迁移) 失败,Admin Area -> Monitoring > Background migrations 界面中,通常会有重试按钮
# 给所有项目批量添加 Webhook
gitlab-rake gitlab:web_hook:add URL="http://example.com/hook"
# 批量删除指定 Webhook
gitlab-rake gitlab:web_hook:rm URL="http://example.com/hook"Gitlab CI
- GitLab CI 是 GitLab 的持续集成(CI)和持续交付(CD)的工具
- 配置环境变量 repo -> Settings -> CI/CD -> Variables
runner
- GitLab Runner 是一个用于在 GitLab CI/CD 环境下执行自动化构建、测试和部署任务的工具
- gitlab runner ssl 证书配置:https://docs.gitlab.com/runner/configuration/tls-self-signed/#trusting-the-certificate-for-user-scripts
- [runners.docker] section
- 激活:
gitlab-runner verify - 重启:
gitlab-runner restart
- gitlab 添加 webhook 提示 Invalid url given:GitLab 默认禁止 Webhook 访问本地网络地址,配置步骤:进入 Admin Area > Settings > Network,修改 Outbound requests 配置:勾选 Allow requests to the local network
[[runners]]
(...)
executor = "docker"
[runners.docker]
(...)
image = "docker:20.10.20"
helper_image = "my.registry.local/gitlab/gitlab-runner-helper:tag"
allowed_pull_policies = ["if-not-present"] #添加了这行
pull_policy = ["if-not-present"] #添加了这行
volumes = ["/var/run/docker.sock:/var/run/docker.sock","/cache"] #编辑了这行- runner 配置 tls 不验证和hosts解析
[runners.docker]
tls_verify = false
extra_hosts = ["your-domain:your-ip"] // 添加domain解析,否则无法在内网使用openssl req -x509 -newkey rsa:2048 -sha256 -days 30 -nodes \
-keyout git-test.key \
-out git-test.crt \
-config git-test.cnf参考:
- https://docs.gitlab.com/runner/configuration/advanced-configuration/
- https://docs.gitlab.com/runner/configuration/advanced-configuration/#override-the-helper-image
metrics
F&Q
Urlis blocked: Requests to the local network are not allowed
点击 Settings -> Network -> Outbound requests
HA 实现
迁移 git 仓库方法
- export/import
- Direct Transfer
- git clone –mirror
- Congregate
占位用户替换报错
解决:
mkdir -p /opt/gitlab/embedded/service/gitlab-rails/public/tmp/uploads
chown -R git:git /opt/gitlab/embedded/service/gitlab-rails/public/tmp最近更新
最新评论