温馨提示×

centos gitlab怎么提性能

小樊
40
2026-09-08 22:20:41
栏目: 智能运维

在 CentOS 上运行 GitLab 时,如果感觉性能不好(页面慢、CI 卡、clone 慢等),可以从系统层、GitLab 配置、存储、网络、使用方式几个维度来优化。下面给你一套比较实用、可落地的方案。


一、系统层面优化(CentOS)

1. 内存 & Swap

GitLab 比较吃内存,官方建议:

  • 最小:4GB RAM(仅少量用户)
  • 推荐:8GB+ RAM

检查内存:

free -h

如果内存不足:

  • 增加物理内存 / 云主机规格
  • 配置 Swap(应急):
dd if=/dev/zero of=/swapfile bs=1G count=4
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

2. 调整 Linux 内核参数

编辑 /etc/sysctl.conf

vm.swappiness = 10
net.core.somaxconn = 1024
net.ipv4.tcp_max_syn_backlog = 8192

应用:

sysctl -p

3. 文件描述符

GitLab 会开大量连接:

ulimit -n 65535

/etc/security/limits.conf

* soft nofile 65535
* hard nofile 65535

二、GitLab 自身配置优化

配置文件:/etc/gitlab/gitlab.rb

1. 关闭不需要的服务(非常重要)

例如你只用 Git + CI:

gitlab_rails['gitlab_email_enabled'] = false
prometheus['enable'] = false
alertmanager['enable'] = false
grafana['enable'] = false

2. 调整 Unicorn / Puma(Web 性能核心)

GitLab 13+ 默认是 Puma

puma['worker_processes'] = 4
puma['worker_timeout'] = 60
puma['max_threads'] = 16

公式参考:

  • worker = CPU 核数
  • threads = 8~16

3. Sidekiq 并发

sidekiq['concurrency'] = 20

4. 限制 Postgres 连接数

避免数据库拖慢:

postgresql['max_connections'] = 200
postgresql['shared_buffers'] = "2GB"

5. 重配置生效

gitlab-ctl reconfigure
gitlab-ctl restart

三、存储 & IO 优化(非常关键)

1. 使用 SSD / NVMe

GitLab 对磁盘 IO 极敏感:

  • 仓库
  • PostgreSQL
  • CI artifacts

千万不要用低性能云盘


2. 数据目录分离

例如:

  • /var/opt/gitlab → 高性能盘
  • /var/log/gitlab → 普通盘

挂载后修改:

git_data_dirs({
  "default" => { "path" => "/data/gitlab/git-data" }
})

3. 禁用 NFS(如果有)

NFS 会明显拖慢 GitLab,尤其是仓库。


四、Git / Clone 性能优化

1. 启用 Git 打包优化

gitlab_rails['git_gc_auto'] = true

手动:

gitlab-rake gitlab:cleanup:repo_check

2. 使用 SSH 而非 HTTPS(大仓库)

SSH 通常更稳定。


3. 限制大文件

启用 Git LFS,避免仓库膨胀。


五、CI / Runner 优化

1. 使用独立 Runner

不要和 GitLab 同一台机器。

gitlab-runner register

2. 并发 Runner

concurrent = 10

3. 使用 Docker / K8s Runner

避免环境初始化慢。


六、监控 & 定位瓶颈

1. 看哪个组件慢

gitlab-ctl status
top
iostat -x 1

2. 查看慢请求

tail -f /var/log/gitlab/gitlab-rails/production.log

七、典型优化前后对比(经验值)

  • 页面加载:3–5s → <1s
  • CI 排队:明显减少
  • clone 大仓库:提升 2–5 倍

八、如果你愿意,我可以更具体

你可以告诉我:

  1. CentOS 版本(7 / 8 / Stream)
  2. GitLab 版本
  3. 机器配置(CPU / 内存 / 磁盘)
  4. 主要慢在哪个环节(页面 / clone / CI)

我可以直接给你一份针对你环境的 gitlab.rb 优化配置

0