在 CentOS 上运行 GitLab 时,如果感觉性能不好(页面慢、CI 卡、clone 慢等),可以从系统层、GitLab 配置、存储、网络、使用方式几个维度来优化。下面给你一套比较实用、可落地的方案。
GitLab 比较吃内存,官方建议:
检查内存:
free -h
如果内存不足:
dd if=/dev/zero of=/swapfile bs=1G count=4
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
编辑 /etc/sysctl.conf:
vm.swappiness = 10
net.core.somaxconn = 1024
net.ipv4.tcp_max_syn_backlog = 8192
应用:
sysctl -p
GitLab 会开大量连接:
ulimit -n 65535
在 /etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
配置文件:/etc/gitlab/gitlab.rb
例如你只用 Git + CI:
gitlab_rails['gitlab_email_enabled'] = false
prometheus['enable'] = false
alertmanager['enable'] = false
grafana['enable'] = false
GitLab 13+ 默认是 Puma
puma['worker_processes'] = 4
puma['worker_timeout'] = 60
puma['max_threads'] = 16
公式参考:
sidekiq['concurrency'] = 20
避免数据库拖慢:
postgresql['max_connections'] = 200
postgresql['shared_buffers'] = "2GB"
gitlab-ctl reconfigure
gitlab-ctl restart
GitLab 对磁盘 IO 极敏感:
千万不要用低性能云盘
例如:
/var/opt/gitlab → 高性能盘/var/log/gitlab → 普通盘挂载后修改:
git_data_dirs({
"default" => { "path" => "/data/gitlab/git-data" }
})
NFS 会明显拖慢 GitLab,尤其是仓库。
gitlab_rails['git_gc_auto'] = true
手动:
gitlab-rake gitlab:cleanup:repo_check
SSH 通常更稳定。
启用 Git LFS,避免仓库膨胀。
不要和 GitLab 同一台机器。
gitlab-runner register
concurrent = 10
避免环境初始化慢。
gitlab-ctl status
top
iostat -x 1
tail -f /var/log/gitlab/gitlab-rails/production.log
你可以告诉我:
我可以直接给你一份针对你环境的 gitlab.rb 优化配置。