优化 Linux 上 GitLab 性能可以从 系统资源、GitLab 配置、数据库、存储、网络、使用习惯 等多个层面入手。下面按优先级和可操作性给你一份较完整的优化指南。
GitLab 是资源消耗型应用,建议:
free -h
如果 swap 使用频繁,说明内存不足。GitLab 对磁盘 IO 非常敏感。
✅ 强烈建议
查看 IO 情况:
iostat -x 1
ulimit -n
建议 ≥ 65535
编辑:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
/etc/sysctl.conf
vm.swappiness = 10
net.core.somaxconn = 1024
net.ipv4.tcp_fin_timeout = 30
sysctl -p
GitLab 配置文件:
/etc/gitlab/gitlab.rb
修改后执行:
gitlab-ctl reconfigure
gitlab-ctl restart
如果只使用 Git + 代码管理,不需要 CI、Pages、Registry:
gitlab_ci['enable'] = false
gitlab_pages['enable'] = false
registry['enable'] = false
prometheus_monitoring['enable'] = false
puma['enable'] = true
puma['worker_processes'] = 4 # 建议 CPU 核数
puma['threads_min'] = 4
puma['threads_max'] = 16
unicorn['worker_processes'] = 4
sidekiq['max_concurrency'] = 20
或根据 CPU 调整。
gitlab_rails['db_pool'] = 20
如果规模较大,建议:
shared_buffers = 4GB
work_mem = 32MB
maintenance_work_mem = 512MB
effective_cache_size = 12GB
查看配置:
gitlab-psql -c "SHOW shared_buffers;"
gitlab-rake gitlab:cleanup:orphan_job_artifact_files
gitlab-rake gitlab:cleanup:project_uploads
gitlab-rake gitlab:git:fsck
git gc --aggressive
gitlab_rails['max_attachment_size'] = 100
gitlab_rails['max_import_size'] = 200
gitlab_ci['concurrent'] = 4
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
gitlab-ctl status
gitlab-rake gitlab:env:info
gitlab-ctl tail nginx
gitlab-ctl tail puma
GitLab 自带 Prometheus,可开启分析:
| 现象 | 原因 | 解决方案 |
|---|---|---|
| 页面卡顿 | Puma 进程不足 | 增加 worker |
| Git clone 慢 | 磁盘 IO 差 | SSD |
| Sidekiq 堆积 | 并发低 | 增加 concurrency |
| 内存占用高 | 服务过多 | 关闭不必要服务 |
| CI 卡死 | Runner 不足 | 增加 Runner |
如果你愿意,可以告诉我:
我可以给你一份 针对你环境的精确优化方案。