1. 为什么需要私有化部署GitLab?
对于中小型技术团队来说,代码资产的安全性和开发流程的自主可控至关重要。我见过不少创业团队因为使用第三方代码托管服务,突然遭遇服务变更或网络问题,导致整个开发流程瘫痪。GitLab的私有化部署方案能完美解决这些问题。
私有化部署意味着你可以:
- 完全掌控代码仓库的访问权限
- 自定义CI/CD流水线适应独特开发流程
- 避免公有云服务的网络延迟和带宽限制
- 满足企业级数据合规要求
最近帮一个20人团队迁移到私有GitLab后,他们的构建速度从平均6分钟缩短到90秒,就因为不用再受限于公有云的资源争抢。
2. 部署前的硬件与系统准备
2.1 服务器基础配置建议
根据我处理过30+部署案例的经验,推荐以下配置规格:
| 团队规模 | CPU核心 | 内存 | 存储空间 | 预估并发构建数 |
|---|---|---|---|---|
| 5-10人 | 4核 | 8GB | 100GB | 2-3 |
| 10-30人 | 8核 | 16GB | 250GB | 5-8 |
| 30-50人 | 16核 | 32GB | 500GB | 10-15 |
特别注意:如果计划使用Docker方式部署,需要额外预留20%的系统资源给容器运行时。
2.2 操作系统优化技巧
在Ubuntu 20.04 LTS上(我最推荐的部署系统),建议先执行这些优化命令:
# 关闭swap避免内存抖动 sudo swapoff -a sudo sed -i '/swap/s/^/#/' /etc/fstab # 调整文件描述符限制 echo "fs.file-max = 65535" | sudo tee -a /etc/sysctl.conf echo "* soft nofile 65535" | sudo tee -a /etc/security/limits.conf echo "* hard nofile 65535" | sudo tee -a /etc/security/limits.conf # 内核参数优化 cat <<EOF | sudo tee -a /etc/sysctl.conf vm.swappiness = 10 net.core.somaxconn = 1024 net.ipv4.tcp_max_syn_backlog = 2048 EOF sudo sysctl -p这些优化能显著提升GitLab在高并发时的稳定性,特别是在运行大量CI任务时。
3. 两种主流部署方式详解
3.1 原生包安装方案
对于生产环境,我强烈推荐使用官方Omnibus包安装。这是最稳定且易于维护的方式:
# Ubuntu/Debian系统 sudo apt-get update sudo apt-get install -y curl openssh-server ca-certificates # 添加GitLab仓库 curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash # 安装指定版本(推荐使用LTS版本) sudo EXTERNAL_URL="http://your.domain.com" apt-get install gitlab-ce=15.11.8-ce.0安装完成后,关键的配置文件位于/etc/gitlab/gitlab.rb。建议优先修改这些参数:
external_url 'https://git.yourcompany.com' gitlab_rails['initial_root_password'] = 'your_strong_password' nginx['listen_port'] = 443 nginx['ssl_certificate'] = "/etc/ssl/certs/your_domain.crt" nginx['ssl_certificate_key'] = "/etc/ssl/private/your_domain.key"3.2 Docker容器化部署
对于需要快速试错或资源隔离的场景,Docker是不错的选择。这是我验证过的高效启动命令:
export GITLAB_HOME=/srv/gitlab sudo docker run --detach \ --hostname gitlab.yourdomain.com \ --publish 8443:443 --publish 8080:80 --publish 2222:22 \ --name gitlab \ --restart always \ --volume $GITLAB_HOME/config:/etc/gitlab \ --volume $GITLAB_HOME/logs:/var/log/gitlab \ --volume $GITLAB_HOME/data:/var/opt/gitlab \ --shm-size 256m \ --env GITLAB_OMNIBUS_CONFIG="external_url 'http://gitlab.yourdomain.com'; gitlab_rails['gitlab_shell_ssh_port'] = 2222;" \ gitlab/gitlab-ce:15.11.8-ce.0容器部署时常见问题处理:
- 端口冲突:修改宿主机端口映射(如8080→80)
- 启动慢:检查
docker logs gitlab观察初始化进度 - 502错误:通常需要等待3-5分钟完成初始化
4. 关键安全配置指南
4.1 防火墙与网络隔离
建议采用分层防护策略:
- 云安全组:仅开放80,443,22端口
- 主机防火墙:使用UFW限制访问IP
sudo ufw allow from 192.168.1.0/24 to any port 22 sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw enable - GitLab内置:配置
gitlab.rb中的网络限制gitlab_rails['gitlab_shell_ssh_port'] = 2222 gitlab_rails['trusted_proxies'] = ['192.168.1.100']
4.2 定期备份策略
创建全量备份脚本/usr/local/bin/gitlab-backup.sh:
#!/bin/bash BACKUP_DIR="/var/opt/gitlab/backups" TIMESTAMP=$(date +%s) sudo gitlab-backup create STRATEGY=copy DIRECTORY=$BACKUP_DIR BACKUP=$TIMESTAMP sudo tar czf $BACKUP_DIR/gitlab-config-$TIMESTAMP.tar.gz /etc/gitlab find $BACKUP_DIR -type f -mtime +7 -exec rm {} \;添加到cron每周执行:
0 2 * * 0 /usr/local/bin/gitlab-backup.sh5. CI/CD流水线实战配置
5.1 Runner高效部署
共享Runner适合小团队,专用Runner性能更好。注册专用Runner的命令:
sudo gitlab-runner register \ --url "http://your.gitlab.url/" \ --registration-token "PROJECT_REGISTRATION_TOKEN" \ --executor "docker" \ --docker-image alpine:latest \ --description "docker-runner" \ --tag-list "docker,aws" \ --run-untagged="true" \ --locked="false"优化Runner配置/etc/gitlab-runner/config.toml:
concurrent = 4 check_interval = 0 [[runners]] name = "high-perf-runner" url = "http://gitlab.url" token = "TOKEN" executor = "docker" [runners.docker] tls_verify = false image = "alpine:latest" privileged = true disable_cache = false volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock"] shm_size = "256m" [runners.cache] [runners.cache.s3] ServerAddress = "s3.amazonaws.com" AccessKey = "AWS_ACCESS_KEY" SecretKey = "AWS_SECRET_KEY" BucketName = "gitlab-runner-cache" BucketLocation = "us-east-1"5.2 高级.gitlab-ci.yml模板
这是一个支持多环境部署的完整示例:
variables: DOCKER_HOST: tcp://docker:2375 DOCKER_DRIVER: overlay2 stages: - test - build - deploy unit-test: stage: test image: node:16 script: - npm install - npm test artifacts: paths: - coverage/ expire_in: 1 week build-image: stage: build image: docker:20.10 services: - docker:20.10-dind script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA . - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA rules: - if: $CI_COMMIT_BRANCH == "main" deploy-prod: stage: deploy image: bitnami/kubectl:latest script: - kubectl config use-context prod-cluster - kubectl set image deployment/my-app app=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA environment: name: production url: https://app.yourdomain.com when: manual only: - main6. 性能监控与调优
6.1 Prometheus监控集成
GitLab内置Prometheus,只需在gitlab.rb中启用:
prometheus['enable'] = true prometheus_monitoring['enable'] = true prometheus['listen_address'] = 'localhost:9090'关键监控指标告警规则示例:
- 内存使用率 > 80%持续5分钟
- 数据库连接数 > 90%容量
- HTTP 500错误率 > 1%
6.2 数据库优化
对于PostgreSQL的调优建议:
# /etc/gitlab/gitlab.rb postgresql['shared_buffers'] = "4GB" # 25% of total RAM postgresql['effective_cache_size'] = "12GB" # 75% of total RAM postgresql['work_mem'] = "32MB" postgresql['maintenance_work_mem'] = "1GB"定期执行维护命令:
sudo gitlab-psql -c "VACUUM ANALYZE;" sudo gitlab-psql -c "REINDEX DATABASE gitlabhq_production;"7. 常见故障排查手册
7.1 启动问题排查流程
- 检查服务状态:
sudo gitlab-ctl status sudo journalctl -u gitlab-runsvdir -n 100 - 查看组件日志:
sudo gitlab-ctl tail postgresql sudo gitlab-ctl tail nginx - 常见错误处理:
- 502错误:通常需要等待或检查内存
- 500错误:运行
sudo gitlab-rake gitlab:check - 磁盘满:清理日志
sudo gitlab-ctl cleanse
7.2 备份恢复实战
完整恢复步骤:
# 停止相关服务 sudo gitlab-ctl stop unicorn sudo gitlab-ctl stop sidekiq # 恢复备份文件 sudo gitlab-backup restore BACKUP=1234567890_2023_01_01_15.0.0 # 恢复配置文件 sudo tar xzf gitlab-config-1234567890.tar.gz -C / # 重启服务 sudo gitlab-ctl restart sudo gitlab-rake gitlab:check SANITIZE=true记得在恢复后立即修改所有密码,包括root用户和数据库密码。