centos

如何在CentOS上实现GitLab的高可用部署

小樊
47
2025-07-26 18:51:29
栏目: 智能运维

在CentOS上实现GitLab的高可用部署可以通过多种方式来完成,以下是一个基本的步骤指南,使用Keepalived和HAProxy来实现高可用性。

前提条件

  1. 两台或多台CentOS服务器:用于部署GitLab实例。
  2. 域名和SSL证书:用于访问GitLab。
  3. 防火墙配置:确保必要的端口(如80, 443, 22)是开放的。

步骤

1. 安装依赖包

在所有服务器上安装必要的依赖包:

sudo yum install -y policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd

2. 安装GitLab

在所有服务器上安装GitLab。你可以使用官方的安装脚本或者手动安装。这里使用官方脚本:

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo EXTERNAL_URL="http://yourdomain.com" yum install gitlab-ce

3. 配置GitLab

编辑GitLab配置文件 /etc/gitlab/gitlab.rb,设置外部URL和其他必要的配置:

external_url 'http://yourdomain.com'
gitlab_rails['lfs_enabled'] = true
gitlab_rails['gitlab_shell_ssh_port'] = 2222

然后重新配置并重启GitLab:

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

4. 安装和配置Keepalived

在两台服务器上安装Keepalived:

sudo yum install -y keepalived

编辑Keepalived配置文件 /etc/keepalived/keepalived.conf

主服务器(假设IP为192.168.1.101):

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.1.100
    }
}

备用服务器(假设IP为192.168.1.102):

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        192.168.1.100
    }
}

启动Keepalived服务:

sudo systemctl enable keepalived
sudo systemctl start keepalived

5. 安装和配置HAProxy

在两台服务器上安装HAProxy:

sudo yum install -y haproxy

编辑HAProxy配置文件 /etc/haproxy/haproxy.cfg

global
    log /dev/log local0
    log /dev/log local1 notice
    daemon

defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http_front
    bind *:80
    stats uri /haproxy?stats
    default_backend http_back

backend http_back
    balance roundrobin
    server gitlab1 192.168.1.101:80 check
    server gitlab2 192.168.1.102:80 check

启动HAProxy服务:

sudo systemctl enable haproxy
sudo systemctl start haproxy

验证

  1. 访问 http://yourdomain.com,你应该能够看到GitLab的登录页面。
  2. 关闭主服务器上的Keepalived服务,备用服务器应该接管虚拟IP,GitLab服务应该仍然可用。

通过以上步骤,你可以在CentOS上实现GitLab的高可用部署。根据实际需求,你可能还需要配置SSL证书、监控和日志记录等。

0
看了该问题的人还看了