温馨提示×

GitLab在CentOS上的部署与运维指南

小樊
50
2025-10-19 17:02:14
栏目: 智能运维

一、GitLab在CentOS上的部署指南

1. 安装前准备

  • 系统要求:推荐使用CentOS 7或8,确保系统已更新(sudo yum update -y)。
  • 硬件要求:至少2核CPU、4GB内存、20GB可用磁盘空间(生产环境建议更高配置)。
  • 依赖安装:安装必要组件(sudo yum install -y curl policycoreutils-python openssh-server postfix cronie),其中Postfix用于邮件通知(若需邮件功能,后续需配置SMTP)。
  • 防火墙配置:放行HTTP(80)、HTTPS(443)、SSH(22)端口(sudo firewall-cmd --permanent --add-service={http,https,ssh} && sudo firewall-cmd --reload)。
  • SELinux设置:若启用SELinux,需设置为permissive模式(sudo setenforce 0)或调整相关策略。

2. 添加GitLab仓库与安装

  • 添加官方仓库:通过curl添加GitLab社区版(CE)仓库(curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash)。
  • 安装GitLab:使用yum安装GitLab CE(sudo yum install -y gitlab-ce)。

3. 配置GitLab

  • 修改外部URL:编辑/etc/gitlab/gitlab.rb文件,设置external_url为服务器IP或域名(如external_url 'http://192.168.1.100')。
  • 配置邮件服务(可选):若需邮件通知,在gitlab.rb中添加SMTP配置(示例):
    gitlab_rails['smtp_enable'] = true
    gitlab_rails['smtp_address'] = "smtp.example.com"
    gitlab_rails['smtp_port'] = 587
    gitlab_rails['smtp_user_name'] = "your_email@example.com"
    gitlab_rails['smtp_password'] = "your_password"
    gitlab_rails['smtp_authentication'] = "login"
    gitlab_rails['smtp_enable_starttls_auto'] = true
    gitlab_rails['gitlab_email_from'] = "your_email@example.com"
    
  • 重载配置:执行sudo gitlab-ctl reconfigure应用配置,该命令会自动启动GitLab服务。

4. 启动与访问GitLab

  • 启动服务sudo gitlab-ctl start启动GitLab,sudo systemctl enable gitlab设置开机自启。
  • 访问Web界面:在浏览器输入http://your_server_ip,首次访问需设置root管理员密码(默认用户名为root)。

二、GitLab在CentOS上的运维指南

1. 日常运维操作

  • 服务管理:使用gitlab-ctl命令管理服务(start|stop|restart|status),如sudo gitlab-ctl restart重启所有服务。
  • 日志查看:GitLab日志位于/var/log/gitlab目录,可通过tail -f /var/log/gitlab/unicorn/unicorn.log实时查看unicorn服务日志,或gitlab-ctl tail查看所有服务日志。
  • 备份与恢复:使用内置工具备份(sudo gitlab-backup create,默认备份路径为/var/opt/gitlab/backups),恢复时需停止相关服务并指定备份文件(sudo gitlab-backup restore BACKUP=timestamp)。

2. 性能优化

  • 硬件升级:增加内存(GitLab对内存需求高,建议8GB以上)、使用SSD(提升IO性能)、多核CPU(提高并发处理能力)。
  • 系统配置优化
    • 调整内核参数(/etc/sysctl.conf):增加net.core.somaxconn = 65535net.ipv4.tcp_max_syn_backlog = 65535等参数,提升网络连接处理能力(执行sysctl -p生效)。
    • 调整文件描述符限制(/etc/security/limits.conf):添加* soft nofile 65535* hard nofile 65535,避免进程因文件描述符耗尽崩溃。
  • GitLab配置调整
    • 优化Unicorn进程数(/etc/gitlab/gitlab.rb):unicorn['worker_processes'] = 3(根据CPU核心数调整)。
    • 启用缓存(gitlab.rb):gitlab_rails['cache_store'] = :memory_store, { size: 64.megabytes },减少重复计算。
    • 调整Sidekiq并发数(gitlab.rb):sidekiq['concurrency'] = 25(根据CPU核心数调整),提升后台任务处理效率。

3. 安全加固

  • SSH密钥认证:生成SSH密钥对(ssh-keygen -t rsa -C "your_email@example.com"),将公钥(id_rsa.pub)添加到GitLab用户SSH密钥管理中(Profile Settings -> SSH Keys),禁用root直接登录(修改/etc/ssh/sshd_config中的PermitRootLogin no并重启SSH服务)。
  • 防火墙设置:仅开放必要端口(80、443、22),关闭其他端口(sudo firewall-cmd --permanent --remove-service={ftp,telnet})。
  • 定期更新:使用sudo yum update gitlab-ce更新GitLab到最新版本,修复安全漏洞。

4. 故障排查

  • 服务状态检查:使用gitlab-ctl status查看所有服务是否运行正常,若某服务异常,可通过gitlab-ctl restart <service_name>重启(如gitlab-ctl restart postgresql)。
  • 日志分析:通过/var/log/gitlab目录下的日志文件定位问题,如unicorn日志(unicorn.log)记录应用层错误,postgresql日志(gitlab-db.log)记录数据库错误。
  • 网络检查:使用ping测试服务器连通性,curl -I http://localhost检查Web服务是否响应,netstat -tulnp查看端口监听情况。
  • 配置文件检查:检查/etc/gitlab/gitlab.rb是否有语法错误(如缩进、拼写),修改后需执行sudo gitlab-ctl reconfigure重载配置。

0