温馨提示×

GitLab Linux配置有技巧吗

小樊
50
2026-08-24 21:58:26
栏目: 智能运维

Linux 上配置 GitLab 时,确实有一些实用技巧和常见坑点,下面按「安装 → 性能 → 安全 → 日常维护」给你一个比较系统的总结,适合生产环境。


一、安装方式选择(很重要)

✅ 推荐方式

Omnibus 包(官方推荐)

# Ubuntu / Debian
curl -fsSL https://packages.gitlab.com/gpg.key | sudo apt-key add -
sudo apt-get install -y gitlab-ee   # 或 gitlab-ce

# CentOS / RHEL
sudo yum install -y gitlab-ee

✅ 优点:

  • 配置集中(/etc/gitlab/gitlab.rb
  • 升级简单
  • 官方支持最好

❌ 不推荐:

  • 源码安装(维护成本高)
  • Docker 跑 GitLab(大规模性能一般)

二、gitlab.rb 必调的核心配置

GitLab 的“技巧”90% 都在 gitlab.rb

1️⃣ 修改外部访问地址

external_url 'https://gitlab.example.com'

⚠️ 改完后必须执行:

gitlab-ctl reconfigure

2️⃣ 关闭不需要的服务(省内存)

如果只做 Git + CI,不需要全部组件:

gitlab_rails['monitoring_enabled'] = false
prometheus_monitoring['enable'] = false
grafana['enable'] = false

3️⃣ 数据库 & Redis 调优(关键)

PostgreSQL

postgresql['shared_buffers'] = "512MB"
postgresql['max_connections'] = 200

Redis

redis['maxmemory'] = "512mb"
redis['maxmemory_policy'] = "allkeys-lru"

4️⃣ 调整 Sidekiq(后台任务)

sidekiq['concurrency'] = 10

CPU 强可适当提高,否则建议 5–10


三、性能优化技巧(Linux 层面)

✅ 1. 文件系统

推荐:

ext4 / xfs

挂载参数(示例):

noatime,nodiratime,data=ordered

✅ 2. 关闭 Swap(强烈建议)

sudo swapoff -a

并在 /etc/fstab 中注释 swap 行


✅ 3. 调整系统限制

cat >> /etc/security/limits.conf <<EOF
* soft nofile 65536
* hard nofile 65536
EOF

四、HTTPS & 证书技巧

自动 Let’s Encrypt(推荐)

letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['admin@example.com']

⚠️ 80 端口必须可访问


手动证书

nginx['ssl_certificate'] = "/etc/gitlab/ssl/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/privkey.pem"

五、GitLab Runner(CI/CD 技巧)

推荐安装方式

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install gitlab-runner

使用 shell / docker executor

gitlab-runner register

✅ 技巧:

  • 小项目用 shell
  • 多项目用 docker
  • 生产环境避免 privileged=true

六、备份 & 恢复(必做)

备份

gitlab-rake gitlab:backup:create

默认路径:

/var/opt/gitlab/backups/

定时备份(crontab)

0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create

七、升级技巧(很重要)

✅ 正确顺序:

gitlab-ctl stop puma
gitlab-ctl stop sidekiq
apt upgrade gitlab-ee
gitlab-ctl reconfigure
gitlab-ctl restart

⚠️ 不要跳大版本(如 15 → 17)


八、常见坑总结

问题 解决
502 错误 内存不足 / puma 未启动
git clone 慢 调整 ssh / http 并发
磁盘暴涨 artifacts / registry 未清理
邮件发不出去 检查 SMTP + 防火墙

九、推荐服务器配置(经验值)

用户规模 CPU 内存
10 人 2 核 4 GB
50 人 4 核 8 GB
200+ 8 核 16 GB+

十、你可以继续问的方向

如果你愿意,我可以帮你:

  • 针对你当前服务器配置给一份 gitlab.rb 模板
  • 排查 502 / 慢 / 高内存问题
  • 设计 GitLab + Runner + CI 最佳实践
  • 从 GitHub / GitLab 迁移方案

你可以直接说:

“我的是 Ubuntu 22.04,8G 内存,50 人用,怎么配?”

0