温馨提示×

gitlab linux如何优化

小樊
46
2026-10-13 07:57:54
栏目: 智能运维

在 Linux 上优化 GitLab,可以从系统资源、GitLab 配置、数据库、存储、网络和安全几个方面入手。下面按“实用优先”的方式给你一套优化建议。


一、系统层面优化(Linux)

1. 内存与 Swap

GitLab 比较吃内存(尤其 Sidekiq、PostgreSQL、Puma)。

  • 最低配置
    • 小型团队:4GB RAM(不推荐)
    • 推荐:8GB+ RAM
  • Swap
    free -h
    swapon --show
    
    如果内存 ≤ 8GB,建议开 2–4GB swap:
    fallocate -l 4G /swapfile
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    

2. 文件描述符限制

GitLab 会大量使用文件句柄:

ulimit -n

建议 ≥ 65535
在 /etc/security/limits.conf:

* soft nofile 65535
* hard nofile 65535

3. 内核参数优化

cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
net.ipv4.tcp_max_syn_backlog = 8192
vm.swappiness = 10
EOF
sysctl -p

二、GitLab 自身配置优化

配置文件:

/etc/gitlab/gitlab.rb

1. Puma(Web 服务)优化

puma['worker_processes'] = 4
puma['min_threads'] = 4
puma['max_threads'] = 16

规则:

  • worker ≈ CPU 核数
  • 总线程 ≤ CPU × 2~4

2. Sidekiq(后台任务)

sidekiq['concurrency'] = 20

根据 CPU 和任务量调整(10–30 常见)

3. 关闭不需要的服务

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

(非监控场景可关)


三、PostgreSQL 优化(非常重要)

1. 基础优化

postgresql['shared_buffers'] = "2GB"
postgresql['work_mem'] = "16MB"
postgresql['maintenance_work_mem'] = "512MB"
postgresql['max_connections'] = 200

经验:

  • shared_buffers ≈ 25% 内存
  • work_mem 不宜过大

2. 定期维护

gitlab-psql -c "VACUUM ANALYZE;"

可加入定时任务(每周)


四、存储与性能优化

1. 使用 SSD / NVMe

GitLab 对磁盘 IO 敏感,尤其是:

  • Git 仓库
  • PostgreSQL
  • CI 缓存

2. 仓库存储分离

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

3. 清理无用数据

gitlab-rake gitlab:cleanup:project_uploads
gitlab-rake gitlab:cleanup:orphan_job_artifacts

五、CI/CD 优化

1. 限制并发

gitlab_rails['ci_pipeline_creation_step_timeout'] = 20

2. 使用独立 Runner

不要在 GitLab 本机跑 Runner(资源争抢)

3. 清理 Job Artifacts

设置保留时间:

gitlab_rails['artifacts_object_store_enabled'] = true

六、网络与安全优化

1. 使用 Nginx 缓存

GitLab 自带 Nginx,可启用:

nginx['worker_processes'] = 4
nginx['worker_connections'] = 10240

2. HTTPS + HTTP/2

nginx['ssl_protocols'] = "TLSv1.2 TLSv1.3"

七、监控与问题定位

1. 查看资源占用

top
htop
gitlab-ctl status

2. 慢请求分析

gitlab-ctl tail nginx
gitlab-ctl tail puma

八、常见性能瓶颈总结

问题 原因 解决
页面卡 内存不足 加内存 / 关服务
Git 慢 磁盘 IO 换 SSD
后台任务堆积 Sidekiq 少 提高并发
DB 慢 PG 未优化 调参数

如果你愿意,可以告诉我:

  • GitLab 版本
  • 服务器配置(CPU / 内存 / 磁盘)
  • 使用规模(用户数 / 仓库数 / CI 频率)

我可以直接给你一份定制版 gitlab.rb 优化配置。

0 踩