在 Linux 上优化 GitLab,通常需要从系统资源、GitLab 配置、数据库、存储、网络和安全几个方面入手。下面给你一套比较实用、可落地的优化方案(以 Omnibus GitLab 为例,最常见)。
GitLab 很吃内存,官方建议:
查看内存占用
free -h
top
关键进程内存占用
unicorn / puma(Web)sidekiq(后台任务)postgresqlredisgitalyswapon --show
若无 swap:
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
ulimit -n
建议 ≥ 65535
编辑:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
配置文件:
/etc/gitlab/gitlab.rb
修改后执行:
gitlab-ctl reconfigure
GitLab 13+ 默认使用 Puma
puma['worker_processes'] = 4
puma['min_threads'] = 4
puma['max_threads'] = 16
经验公式:
worker_processes = CPU核心数
threads = 2–4 × worker
sidekiq['concurrency'] = 10
(CPU 强可加到 15–20)
如果只用 Git + CI:
prometheus['enable'] = false
grafana['enable'] = false
mattermost['enable'] = false
postgresql['shared_buffers'] = "2GB"
postgresql['work_mem'] = "16MB"
postgresql['max_connections'] = 200
建议:
gitlab-rake gitlab:db:cleanup
Gitaly 是性能关键:
gitaly['concurrency'] = [
{ "rpc" => "CreateRepository", "max_per_repo" => 5 },
{ "rpc" => "GetRepo", "max_per_repo" => 10 }
]
如使用 SSD:
gitaly['storage'] = [{ 'path' => '/var/opt/gitlab/gitaly' }]
GitLab 对磁盘 IO 非常敏感
/var/opt/gitlab → 高速盘gitlab_rails['backup_path'] = "/data/gitlab/backups"
gitlab-rake gitlab:cleanup:project_uploads
gitlab-rake gitlab:cleanup:orphan_job_artifacts
不要和 GitLab 同机
gitlab-runner register
避免环境污染,提升并发
默认已开,不建议关
proxy_cache_valid 200 5m;
gitlab-ctl status
gitlab-ctl tail
gitlab-rails console
或启用 Prometheus + Grafana(高级)
如果你只是:
可以考虑:
puma['worker_processes'] = 2
sidekiq['concurrency'] = 10
postgresql['shared_buffers'] = "1GB"
prometheus['enable'] = false
grafana['enable'] = false
如果你愿意,可以告诉我:
我可以给你一对一的 gitlab.rb 优化配置。