在Debian上部署GitLab的最佳实践
sudo apt update && sudo apt upgrade -y,确保系统软件包为最新版本,避免兼容性问题。curl(用于下载脚本)、openssh-server(支持SSH访问)、ca-certificates(SSL证书验证)、postfix(邮件通知,可选择“Internet Site”配置)。curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash添加官方仓库,确保获取最新版本的GitLab Community Edition(CE)。EXTERNAL_URL="http://your_server_ip_or_domain" sudo apt install gitlab-ce命令安装,替换为你的服务器IP或域名(后续可通过/etc/gitlab/gitlab.rb修改)。/etc/gitlab/gitlab.rb,找到external_url参数,设置为你的域名(如external_url 'https://gitlab.example.com'),确保访问路径正确。sudo gitlab-ctl reconfigure应用配置,sudo gitlab-ctl restart重启GitLab服务,使更改生效。sudo ufw allow 80/tcp && sudo ufw allow 443/tcp && sudo ufw allow 22/tcp && sudo ufw enable。/etc/gitlab/gitlab.rb中的letsencrypt['enable'] = true和letsencrypt['auto_renew'] = true,然后运行sudo gitlab-ctl reconfigure自动获取并配置SSL证书。sudo管理;禁用SSH root登录(编辑/etc/ssh/sshd_config,设置PermitRootLogin no)。ssh-keygen -t rsa -b 4096),将公钥(id_rsa.pub)添加到GitLab(通过Profile Settings > SSH Keys),禁用密码登录(编辑/etc/ssh/sshd_config,设置PasswordAuthentication no)。/var/opt/gitlab)迁移至SSD,提升磁盘IO性能。gitlab_rails['git_data_compression'] = true),减少存储占用。/etc/gitlab/gitlab.rb中的gitlab_rails['object_store']['enabled'] = true。postgresql['shared_buffers'] = "25% of total RAM"(如16GB内存设置为4GB),postgresql['work_mem'] = "4MB"。unicorn['worker_processes'] = CPU核心数(如8核设置为8),unicorn['timeout'] = 60(避免长时间阻塞)。redis['enable'] = true,加速数据访问。sudo gitlab-rake gitlab:backup:cleanup(清理30天前的备份)。sudo gitlab-rake gitlab:ci:cleanup(清理30天前的CI作业)。sudo journalctl --vacuum-time=2weeks(清理2周前的系统日志)。/-/metrics端点查看性能指标(如CPU、内存、磁盘使用率),可在/etc/gitlab/gitlab.rb中配置报警规则。/var/log/gitlab/gitlab-rails/production.log(应用日志)、/var/log/gitlab/gitlab-shell/gitlab-shell.log(SSH日志)。使用ELK Stack(Elasticsearch+Logstash+Kibana)进行日志分析,实现实时监控。sudo apt update && sudo apt upgrade gitlab-ce),获取安全补丁和新功能。sudo gitlab-rake gitlab:backup:create CRON=0 2 * * *),将备份文件存储到远程服务器或对象存储,定期测试恢复流程。.gitlab-ci.yml中使用parallel关键字,将任务拆分为多个并行 job(如parallel: 4),缩短构建时间。cache关键字缓存依赖文件(如vendor/bundle、node_modules),避免每次构建重复下载:cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- vendor/bundle/
- node_modules/
```。
resources.requests.cpu、resources.limits.memory),避免单个job占用过多资源影响其他任务。/pipelines页面),查看job执行时间、成功率,识别瓶颈并优化。