温馨提示×

GitLab在Debian上的高可用性

小樊
49
2025-11-09 15:25:02
栏目: 智能运维

GitLab在Debian上的高可用性实现方案

一、高可用性基础准备

实现GitLab高可用性需先完成以下基础配置:

  • 硬件与网络:至少准备2台Debian服务器(推荐3台及以上用于生产环境),确保服务器之间网络延迟低(建议<10ms)、带宽充足(≥1Gbps),并能互相通信。
  • 软件依赖:所有服务器需安装curlopenssh-serverca-certificatespostfix等基础依赖,用于GitLab安装与运行。

二、GitLab安装与基础配置

在所有目标服务器上安装GitLab Community Edition(CE)或Enterprise Edition(EE):

  1. 添加GitLab官方仓库:通过官方脚本添加GitLab仓库,确保软件包来源可信。
    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
    
  2. 安装GitLab:使用apt安装GitLab CE,并设置外部访问URL(替换为你的域名或IP)。
    sudo EXTERNAL_URL="http://your-gitlab-domain.com" apt-get install gitlab-ce
    
  3. 初始配置:运行sudo gitlab-ctl reconfigure应用配置,重启GitLab服务使更改生效。

三、核心组件高可用配置

GitLab的高可用性需通过负载均衡数据库复制存储共享三大组件实现:

1. 负载均衡配置(流量分发)

使用Nginx或HAProxy作为负载均衡器,将用户请求分发到多个GitLab实例,避免单点故障。

  • Nginx配置示例
    # 安装Nginx
    sudo apt-get install nginx
    # 编辑配置文件(/etc/nginx/sites-available/gitlab)
    upstream gitlab {
        server gitlab1.example.com;  # GitLab实例1
        server gitlab2.example.com;  # GitLab实例2
    }
    server {
        listen 80;
        server_name your-gitlab-domain.com;
        location / {
            proxy_pass http://gitlab;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    # 启用配置并重启Nginx
    sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/
    sudo nginx -t && sudo systemctl restart nginx
    
  • HAProxy配置示例
    # 安装HAProxy
    sudo apt-get install haproxy
    # 编辑配置文件(/etc/haproxy/haproxy.cfg)
    frontend http_front
        bind *:80
        default_backend http_back
    backend http_back
        balance roundrobin
        server gitlab1 gitlab1.example.com:80 check
        server gitlab2 gitlab2.example.com:80 check
    # 重启HAProxy
    sudo systemctl restart haproxy
    

2. 数据库高可用(PostgreSQL主从复制)

GitLab的核心数据(如用户、项目、仓库)存储在PostgreSQL中,需配置主从复制以实现数据冗余。

  • 主服务器配置/etc/postgresql/12/main/postgresql.conf):
    wal_level = replica
    max_wal_senders = 3
    wal_keep_size = 64
    
  • 主服务器配置/etc/postgresql/12/main/pg_hba.conf):
    host    replication     replicator     gitlab-slave-ip/32     md5
    
  • 创建复制用户(主服务器执行):
    CREATE USER replicator WITH REPLICATION PASSWORD 'your-replicator-password' LOGIN;
    
  • 从服务器配置/etc/postgresql/12/main/postgresql.conf):
    hot_standby = on
    
  • 从服务器配置/etc/postgresql/12/main/pg_hba.conf):
    host    replication     replicator     gitlab-master-ip/32     md5
    
  • 启动复制(从服务器执行):
    pg_basebackup -h gitlab-master-ip -U replicator -D /var/lib/postgresql/12/main -P -R
    
  • GitLab配置/etc/gitlab/gitlab.rb):
    gitlab_rails['db_adapter'] = "postgresql"
    gitlab_rails['db_host'] = "your-db-master-ip"
    gitlab_rails['db_port'] = 5432
    gitlab_rails['db_username'] = "gitlab"
    gitlab_rails['db_password'] = "your-db-password"
    

3. 存储共享配置(NFS)

GitLab的代码仓库、附件等文件需存储在共享存储中,确保所有实例访问同一份数据。

  • NFS服务器配置(主服务器):
    # 安装NFS服务
    sudo apt-get install nfs-kernel-server
    # 编辑共享目录配置(/etc/exports)
    /var/opt/gitlab *(rw,sync,no_subtree_check,no_root_squash)
    # 启用NFS服务
    sudo exportfs -a && sudo systemctl restart nfs-kernel-server
    
  • NFS客户端配置(所有GitLab实例):
    # 安装NFS客户端
    sudo apt-get install nfs-common
    # 挂载共享目录
    sudo mount -t nfs nfs-server-ip:/var/opt/gitlab /var/opt/gitlab
    # 设置开机自动挂载(/etc/fstab)
    nfs-server-ip:/var/opt/gitlab /var/opt/gitlab nfs defaults 0 0
    

四、GitLab实例配置

在每个GitLab实例上,需调整配置以适配高可用架构:

  • 编辑/etc/gitlab/gitlab.rb
    external_url 'http://your-gitlab-domain.com'
    unicorn['listen_address'] = '0.0.0.0'  # 允许所有IP访问
    gitlab_workhorse['listen_network'] = "tcp"
    gitlab_workhorse['listen_addr'] = "0.0.0.0:8181"
    redis['host'] = "your-redis-server-ip"  # 使用外部Redis
    postgresql['enable'] = false  # 禁用内置PostgreSQL,使用外部数据库
    

五、监控与维护

高可用性需持续监控系统状态,及时发现并解决问题:

  • 监控工具:使用Prometheus+Grafana监控GitLab实例的性能指标(如CPU、内存、请求延迟),配置告警规则(如实例宕机、数据库复制延迟)。
  • 日志管理:使用ELK Stack(Elasticsearch+Logstash+Kibana)集中收集GitLab日志,便于故障排查。
  • 备份策略:使用GitLab内置备份工具定期备份数据(如每天凌晨4点),并将备份文件存储在异地(如云存储):
    # 手动备份
    sudo gitlab-backup create
    # 配置定时备份(/etc/crontab)
    0 4 * * * root /opt/gitlab/bin/gitlab-backup create
    

六、测试高可用性

通过模拟故障验证高可用性配置的有效性:

  • 负载均衡测试:停止某台GitLab实例,检查负载均衡器是否将流量切换到其他实例。
  • 数据库故障测试:停止主PostgreSQL实例,检查从实例是否自动提升为主实例(需配置自动故障转移,如使用Patroni)。
  • 存储故障测试:卸载共享存储,检查GitLab实例是否能正常访问数据(需确保共享存储的高可用性,如使用DRBD或Ceph)。

通过以上步骤,可在Debian上实现GitLab的高可用性,确保系统在单点故障时仍能持续提供服务。

0