# 企业级GitLab私有仓库部署完全指南

企业级GitLab私有仓库部署完全指南

前言

在当今的软件开发环境中,版本控制和代码管理是项目成功的基石。虽然公有Git托管服务如GitHub、GitLab.com提供了便利的服务,但出于代码安全、访问控制和网络性能等考虑,许多企业选择部署私有的GitLab实例。本文将详细介绍如何从零开始部署一个企业级的GitLab私有仓库。

🚀 部署方案选择

在开始部署之前,我们需要根据企业需求选择合适的部署方案:

  1. Omnibus包部署:适合大多数场景,安装简单,维护方便
  2. Docker部署:适合容器化环境,便于迁移和扩展
  3. 源码编译部署:适合需要深度定制的场景
  4. Kubernetes部署:适合大规模集群环境

本文将重点介绍最常用的Omnibus包部署方案,同时也会涵盖Docker部署的要点。

环境准备

系统要求

  • 操作系统:Ubuntu 20.04 LTS / CentOS 8 / RHEL 8
  • 内存:至少4GB(推荐8GB以上)
  • CPU:2核以上
  • 磁盘空间:至少10GB可用空间
  • 网络:稳定的网络连接

软件依赖

确保系统已安装必要的依赖包:

Ubuntu/Debian系统:

1
2
sudo apt-get update
sudo apt-get install -y curl openssh-server ca-certificates

CentOS/RHEL系统:

1
sudo yum install -y curl policycoreutils openssh-server openssh-clients

Omnibus包部署方案

1. 安装GitLab

首先添加GitLab官方仓库并安装:

Ubuntu/Debian系统:

1
2
3
4
5
# 添加GitLab仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash

# 安装GitLab(将EXTERNAL_URL替换为你的域名或IP)
sudo EXTERNAL_URL="https://gitlab.example.com" apt-get install gitlab-ee

CentOS/RHEL系统:

1
2
3
4
5
# 添加GitLab仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash

# 安装GitLab
sudo EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-ee

2. 初始配置

安装完成后,需要进行初始配置:

1
2
3
4
5
# 重新配置GitLab(这会生成默认配置并启动服务)
sudo gitlab-ctl reconfigure

# 检查服务状态
sudo gitlab-ctl status

3. 防火墙配置

如果系统启用了防火墙,需要开放相关端口:

1
2
3
4
5
6
7
8
9
# 开放HTTP、HTTPS和SSH端口
sudo ufw allow http
sudo ufw allow https
sudo ufw allow ssh

# 或者使用具体的端口号
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 22/tcp

4. 访问和初始设置

在浏览器中访问配置的EXTERNAL_URL,首次访问时会要求设置管理员密码:

  1. 访问 https://gitlab.example.com(或你配置的地址)
  2. 设置root用户密码(至少8个字符)
  3. 使用root账户和设置的密码登录

Docker部署方案

对于偏好容器化部署的用户,GitLab也提供了官方Docker镜像:

1. 准备Docker环境

1
2
3
4
5
6
7
# 安装Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# 安装Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

2. 创建Docker Compose文件

创建 docker-compose.yml 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
version: '3.6'
services:
web:
image: 'gitlab/gitlab-ee:latest'
restart: always
hostname: 'gitlab.example.com'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'https://gitlab.example.com'
# 添加其他配置
ports:
- '80:80'
- '443:443'
- '22:22'
volumes:
- '$GITLAB_HOME/config:/etc/gitlab'
- '$GITLAB_HOME/logs:/var/log/gitlab'
- '$GITLAB_HOME/data:/var/opt/gitlab'
shm_size: '256m'

3. 启动GitLab容器

1
2
3
4
5
6
7
8
# 设置数据目录
export GITLAB_HOME=/srv/gitlab

# 启动服务
docker-compose up -d

# 查看日志
docker-compose logs -f

👋 高级配置

1. 邮件服务器配置

配置SMTP服务器以启用邮件通知功能:

编辑 /etc/gitlab/gitlab.rb

1
2
3
4
5
6
7
8
9
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.example.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "[email protected]"
gitlab_rails['smtp_password'] = "your_password"
gitlab_rails['smtp_domain'] = "example.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['gitlab_email_from'] = "[email protected]"

应用配置:

1
sudo gitlab-ctl reconfigure

2. SSL证书配置

使用Let’s Encrypt免费SSL证书:

1
2
3
4
5
6
7
8
9
10
# 在gitlab.rb中启用Let's Encrypt
letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['[email protected]']
letsencrypt['auto_renew'] = true
letsencrypt['auto_renew_hour'] = 12
letsencrypt['auto_renew_minute'] = 30
letsencrypt['auto_renew_day_of_month'] = "*/7"

# 配置GitLab使用HTTPS
external_url 'https://gitlab.example.com'

3. 备份配置

配置自动备份策略:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 设置备份路径
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
gitlab_rails['backup_archive_permissions'] = 0644
gitlab_rails['backup_keep_time'] = 604800 # 保留7天

# 配置备份上传到远程存储(可选)
gitlab_rails['backup_upload_connection'] = {
'provider' => 'AWS',
'region' => 'us-east-1',
'aws_access_key_id' => 'AKIAKIAKI',
'aws_secret_access_key' => 'secret123'
}
gitlab_rails['backup_upload_remote_directory'] = 'my.gitlab.backups'

创建备份脚本 /etc/cron.daily/gitlab-backup

1
2
#!/bin/bash
/opt/gitlab/bin/gitlab-backup create CRON=1

日常运维

1. 常用管理命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 启动所有服务
sudo gitlab-ctl start

# 停止所有服务
sudo gitlab-ctl stop

# 重启所有服务
sudo gitlab-ctl restart

# 查看服务状态
sudo gitlab-ctl status

# 重新加载配置
sudo gitlab-ctl reconfigure

# 检查GitLab状态
sudo gitlab-rake gitlab:check SANITIZE=true

2. 备份和恢复

手动备份:

1
2
3
4
# 创建备份
sudo gitlab-backup create

# 备份文件会保存在 /var/opt/gitlab/backups/ 目录

恢复备份:

1
2
3
4
5
6
7
8
9
10
# 停止相关服务
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq

# 恢复备份(将TIMESTAMP替换为实际时间戳)
sudo gitlab-backup restore BACKUP=TIMESTAMP

# 重新配置并启动
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

3. 监控和日志

查看日志:

1
2
3
4
5
6
7
# 查看所有服务日志
sudo gitlab-ctl tail

# 查看特定服务日志
sudo gitlab-ctl tail nginx
sudo gitlab-ctl tail postgresql
sudo gitlab-ctl tail redis

性能监控:

1
2
3
4
5
# 检查系统资源使用情况
sudo gitlab-ctl prometheus

# 查看GitLab服务指标
curl http://localhost:9090/metrics

安全加固

1. 定期更新

1
2
3
4
5
6
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install gitlab-ee

# CentOS/RHEL
sudo yum update gitlab-ee

2. 安全配置

1
2
3
4
5
6
7
8
9
# 在gitlab.rb中加强安全配置
gitlab_rails['gitlab_default_projects_features_issues'] = false
gitlab_rails['gitlab_default_projects_features_merge_requests'] = true
gitlab_rails['gitlab_default_can_create_group'] = false

# 配置密码策略
gitlab_rails['password_authentication_enabled_for_web'] = true
gitlab_rails['password_authentication_enabled_for_git'] = true
gitlab_rails['minimum_password_length'] = 12

3. 防火墙规则

1
2
# 只允许特定IP访问
sudo ufw allow from 192.168.1.0/24 to any port 80,443,22

✨ 故障排除

常见问题解决

  1. 502错误

    1
    2
    3
    4
    5
    # 检查服务状态
    sudo gitlab-ctl status

    # 重启服务
    sudo gitlab-ctl restart
  2. 磁盘空间不足

    1
    2
    3
    4
    5
    # 清理日志
    sudo gitlab-ctl logrotate

    # 清理缓存
    sudo gitlab-rake cache:clear
  3. 性能问题

    1
    2
    3
    4
    5
    6
    7
    # 检查系统资源
    top
    free -h
    df -h

    # 优化配置
    sudo gitlab-ctl reconfigure

最佳实践

  1. 定期备份:设置自动备份并定期测试恢复流程
  2. 监控告警:配置系统监控和性能告警
  3. 权限管理:遵循最小权限原则,严格控制访问权限
  4. 版本升级:定期升级到最新稳定版本
  5. 文档维护:保持部署和运维文档的更新

💡 总结

通过本文的详细指导,你应该能够成功部署和管理一个企业级的GitLab私有仓库。无论是选择传统的Omnibus包部署还是现代化的Docker部署,GitLab都提供了强大的功能和灵活的配置选项。记住,成功的部署只是开始,持续的维护和优化才是确保系统稳定运行的关键。

在实际生产环境中,建议根据具体业务需求调整配置参数,并建立完善的监控和备份机制。GitLab的强大功能将为你的团队提供高效的代码管理和协作平台,助力软件开发流程的优化和改进。

[up主专用,视频内嵌代码贴在这]