centos

GitLab在CentOS上的高可用性配置

小樊
54
2025-10-21 21:28:46
栏目: 智能运维

1. 准备基础环境
确保所有服务器(GitLab实例、负载均衡器、数据库、缓存)均安装CentOS 7及以上版本,配置静态IP,并关闭SELinux(setenforce 0)及防火墙(或放行必要端口:80、443、22、数据库端口、Redis端口)。安装公共依赖包:curl policycoreutils-python openssh-server postfix

2. 安装并配置多个GitLab实例
在每台服务器上通过官方仓库安装GitLab CE(社区版):

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install -y gitlab-ce

编辑每台实例的/etc/gitlab/gitlab.rb,配置唯一external_url(如gitlab1.example.comgitlab2.example.com)及不同通信端口(避免冲突):

external_url 'http://gitlab1.example.com'
unicorn['listen_address'] = '0.0.0.0:8080'  # GitLab主进程端口
gitlab_rails['gitlab_shell_ssh_port'] = 2222  # SSH端口

依次执行sudo gitlab-ctl reconfigure(应用配置)和sudo gitlab-ctl restart(重启服务)。

3. 部署负载均衡器(Nginx示例)
选择一台服务器作为负载均衡器,安装Nginx:sudo yum install -y nginx。编辑配置文件/etc/nginx/conf.d/gitlab.conf,定义上游集群并配置流量分发:

upstream gitlab {
    server gitlab1.example.com;
    server gitlab2.example.com;
    server gitlab3.example.com;
}
server {
    listen 80;
    server_name gitlab.example.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 systemctl restart nginx

4. 配置高可用数据库(PostgreSQL主从复制)

5. 配置高可用缓存(Redis Sentinel)
Redis Sentinel提供Redis的高可用性,监控主节点故障并自动切换。安装Redis:sudo yum install -y redis。编辑主节点/etc/redis.confbind 0.0.0.0requirepass your_password;编辑从节点/etc/redis.confslaveof 主节点IP 6379masterauth your_password。启动Sentinel服务(需额外配置sentinel.conf),并测试故障转移。

6. 配置共享存储(NFS示例)
GitLab的代码仓库需存储在共享存储中,确保所有实例访问一致。安装NFS服务器(在一台专用服务器上):sudo yum install -y nfs-utils。编辑/etc/exports,共享目录:

/srv/gitlab/repo *(rw,sync,no_root_squash)

启动NFS服务:sudo systemctl restart nfs。在所有GitLab实例上安装NFS客户端:sudo yum install -y nfs-utils,挂载共享目录:sudo mount -t nfs nfs服务器IP:/srv/gitlab/repo /var/opt/gitlab/git-data/repositories(添加到/etc/fstab实现开机自动挂载)。

7. 配置GitLab使用高可用组件
编辑每台GitLab实例的/etc/gitlab/gitlab.rb,指定数据库、缓存、存储的集群地址:

gitlab_rails['db_adapter'] = "postgresql"
gitlab_rails['db_host'] = "pg_cluster_ip"  # PostgreSQL负载均衡或主节点IP
gitlab_rails['db_port'] = 5432
gitlab_rails['db_username'] = "gitlab"
gitlab_rails['db_password'] = "your_password"

redis['host'] = "redis_sentinel_ip"  # Redis Sentinel IP
redis['port'] = 26379
redis['password'] = "your_password"

gitlab_rails['gitlab_shell_ssh_port'] = 2222  # 与实例配置一致

执行sudo gitlab-ctl reconfigure应用配置。

8. 监控与故障恢复

0
看了该问题的人还看了