优化CentOS上GitLab性能的全面指南
调整内核参数以优化网络、文件句柄等资源的管理,增强系统对GitLab的支持:
/etc/sysctl.conf,添加以下配置并执行sysctl -p生效:net.core.somaxconn = 65535 # 增加监听队列长度,应对高并发连接
net.ipv4.tcp_max_syn_backlog = 65535 # 增加SYN队列长度,减少连接超时
net.ipv4.ip_local_port_range = 1024 65535 # 扩大临时端口范围,支持更多并发外出连接
net.ipv4.tcp_tw_reuse = 1 # 允许重用TIME-WAIT状态的连接,减少端口占用
net.ipv4.tcp_fin_timeout = 30 # 缩短TIME-WAIT状态的超时时间(秒)
net.ipv4.tcp_fastopen = 3 # 启用TCP Fast Open,减少握手延迟
/etc/security/limits.conf,添加以下内容以增加系统允许的最大文件句柄数:* soft nofile 65535
* hard nofile 65535
同时编辑/etc/pam.d/common-session和/etc/pam.d/common-session-noninteractive,添加session required pam_limits.so,确保限制生效。通过修改/etc/gitlab/gitlab.rb(GitLab主配置文件),调整各组件的进程数、缓存等参数,平衡性能与资源消耗:
puma['worker_processes'] = 2 # 小型团队可设为2,大型团队不超过4
puma['max_threads'] = 4 # 每个worker的最大线程数,建议4-8
puma['min_threads'] = 2 # 最小线程数,避免频繁创建/销毁线程
sidekiq['max_concurrency'] = 10 # 根据内存调整,并发数越低内存占用越少
sidekiq['queue_groups'] = ['*'] # 所有队列共享一个进程,极大减少内存使用
postgresql['shared_buffers'] = "512MB" # 根据服务器内存调整(建议为总内存的1/4)
postgresql['max_worker_processes'] = 4 # 限制并行工作进程数
gitlab_rails['db_pool'] = 20 # 数据库连接池大小,建议不超过20
gitlab.rb中禁用,节省内存:gitlab_ci['enable'] = false # 禁用CI/CD服务
prometheus_monitoring['enable'] = false # 禁用Prometheus监控
node_exporter['enable'] = false # 禁用节点监控
redis['maxmemory'] = '2gb' # 根据服务器内存调整
redis['maxmemory_policy'] = 'allkeys-lru' # 内存满时删除最近最少使用的键
修改完成后,执行sudo gitlab-ctl reconfigure使配置生效,再通过sudo gitlab-ctl restart重启GitLab服务。
gitlab-rake gitlab:clean命令,清理临时文件、旧备份、孤立仓库等无用数据,释放存储空间。# 安装Git LFS
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs
# 在项目中启用LFS并跟踪大文件类型
git lfs track "*.psd"
git add .gitattributes
git commit -m "Track PSD files with Git LFS"
git push origin master
/var/opt/gitlab/git-data/repositories)迁移至单独的分区或高速存储设备(如SSD),避免存储瓶颈:git_data_dir "/data/gitlab" # 修改为新的存储路径
执行sudo gitlab-ctl reconfigure和sudo gitlab-ctl restart使更改生效。gitlab.rb中启用仓库压缩,减少存储空间占用:gitlab_rails['gitlab_shell_ssh_port'] = 22
gitlab_rails['compression_level'] = 6 # 压缩级别(1-9,越高压缩率越高,但消耗更多CPU)
nginx['enable'] = true
nginx['listen_port'] = 80
nginx['listen_https'] = false
nginx['cache_store'] = 'file_store,/var/opt/gitlab/nginx/cache,64m' # 启用文件缓存,大小64MB
memory_store)存储频繁访问的数据(如项目元数据、用户会话),提升访问速度:gitlab_rails['cache_store'] = :memory_store, { size: 64.megabytes } # 内存缓存大小64MB
htop、top、vmstat等工具实时监控系统资源(CPU、内存、磁盘I/O)使用情况,快速定位性能瓶颈;或使用专业监控工具(如Prometheus + Grafana),搭建GitLab性能监控面板,可视化各项指标。info),减少不必要的日志输出(如将日志级别设为warn或error),降低磁盘I/O消耗;配置日志轮转(logrotate),自动删除过期日志,避免日志文件过大占用存储空间:log_level = "warn" # 可选:info/warn/error,根据需求调整
sudo sysctl -w net.core.rmem_max=16777216
sudo sysctl -w net.core.wmem_max=16777216
sudo sysctl -w net.ipv4.tcp_rmem=4096 87380 16777216
sudo sysctl -w net.ipv4.tcp_wmem=4096 65536 16777216
gitlab.rb中启用HTTP Keep-Alive,减少TCP连接建立的开销:nginx['keepalive_timeout'] = 65 # 保持连接的超时时间(秒)