温馨提示×

linux gitlab怎么优化

小樊
46
2026-08-13 08:13:43
栏目: 智能运维

GitLab 在 Linux 上的优化,通常可以从 系统资源、GitLab 配置、数据库、存储、网络、日常维护 这几个维度来做。下面给你一套比较完整的优化思路(适合自建 GitLab,社区版 / 企业版都适用)。


一、系统层面优化

1. 内存优化(最重要)

GitLab 非常吃内存,官方建议至少 8GB,生产环境 16GB+

检查内存:

free -h

调整 swap(避免 OOM):

swapon -s

建议:

  • 内存 ≤ 8GB:开启 swap(4–8G)
  • 内存 ≥ 16GB:可以不设 swap

2. CPU 优化

  • CI/CD runner 不要和 GitLab 主服务跑在同一台机器
  • 限制 GitLab 的并发进程(下面会讲)

3. 文件描述符限制

ulimit -n

建议设置为:

65535

编辑:

/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

二、GitLab 自身配置优化(核心)

配置文件:

/etc/gitlab/gitlab.rb

修改后务必执行:

gitlab-ctl reconfigure

1. 减少并发,降低内存占用(强烈推荐)

减少 Puma 进程(Web 服务)

puma['worker_processes'] = 2
puma['min_threads'] = 1
puma['max_threads'] = 2

默认是 CPU 核数,非常吃内存。


2. 限制 Sidekiq(后台任务)

sidekiq['max_concurrency'] = 10

3. 关闭不需要的服务

如果你只用 Git + CI:

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

三、数据库优化(PostgreSQL)

1. 使用独立 PostgreSQL(推荐)

自建 GitLab 时,建议:

  • GitLab 一台
  • PostgreSQL 一台

2. PostgreSQL 参数优化

shared_buffers = 2GB
work_mem = 16MB
maintenance_work_mem = 512MB
effective_cache_size = 6GB

3. 定期清理

gitlab-rake gitlab:db:cleanup

四、存储优化(最容易忽略)

1. 仓库存储到高性能磁盘

  • ✅ SSD / NVMe
  • ❌ 云盘 + 高延迟

迁移仓库目录:

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

2. 清理历史大文件

gitlab-rake gitlab:cleanup:orphan_lfs_files
gitlab-rake gitlab:cleanup:project_uploads

3. 限制仓库大小

gitlab_rails['max_attachment_size'] = 100
gitlab_rails['max_import_size'] = 200

五、GitLab CI/CD 优化

1. 使用独立 Runner

gitlab-runner register

不要和 GitLab 同机。


2. 限制 Pipeline 并发

concurrent = 2

3. 启用缓存

cache:
  paths:
    - node_modules/

六、网络与访问优化

1. 使用 Nginx 缓存

GitLab 自带 Nginx,可开启:

nginx['enable'] = true
nginx['client_max_body_size'] = '200m'

2. 使用 HTTPS + HTTP/2

nginx['redirect_http_to_https'] = true

七、定期维护(非常重要)

1. 清理日志

gitlab-ctl tail
du -sh /var/log/gitlab

清理:

gitlab-ctl clean-logs

2. 升级 GitLab

apt update
apt upgrade gitlab-ee

八、快速判断是否需要优化

gitlab-ctl status
top
htop
iostat -x 1

重点关注:

  • puma / sidekiq 是否占满内存
  • IO wait 是否过高
  • swap 是否被大量使用

九、推荐配置参考(8GB 服务器)

puma['worker_processes'] = 2
sidekiq['max_concurrency'] = 10
prometheus_monitoring['enable'] = false
grafana['enable'] = false
gitlab_rails['monitoring_whitelist'] = []

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

  • GitLab 版本
  • 服务器配置(CPU / 内存 / 磁盘)
  • 是否跑 CI/CD
  • 当前主要慢在哪里(页面慢 / git clone 慢 / CI 慢)

我可以给你针对性的优化方案

0