GitLab在CentOS上的高可用性配置
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.com
、gitlab2.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主从复制)
- 主节点配置:编辑
/var/lib/pgsql/data/postgresql.conf
,开启远程连接:listen_addresses = '*'
;编辑/var/lib/pgsql/data/pg_hba.conf
,允许从节点访问:host all all 0.0.0.0/0 md5
。重启服务:sudo systemctl restart postgresql
。 - 从节点配置:通过
pg_basebackup
同步主节点数据:
编辑sudo -u postgres pg_basebackup -h 主节点IP -D /var/lib/pgsql/data -U replicator -P -v -R
/var/lib/pgsql/data/recovery.conf
(PostgreSQL 12+集成到postgresql.conf
),设置主节点连接信息:
重启从节点服务:standby_mode = 'on' primary_conninfo = 'host=主节点IP port=5432 user=replicator password=your_password'
sudo systemctl restart postgresql
。
5. 配置高可用缓存(Redis Sentinel)
Redis Sentinel提供Redis的高可用性,监控主节点故障并自动切换。安装Redis:sudo yum install -y redis
。编辑主节点/etc/redis.conf
:bind 0.0.0.0
、requirepass your_password
;编辑从节点/etc/redis.conf
:slaveof 主节点IP 6379
、masterauth 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. 监控与故障恢复
- 监控:使用Prometheus+Grafana监控GitLab实例、数据库、缓存的运行状态(如CPU、内存、响应时间),设置告警规则(如实例宕机、数据库连接失败)。
- 故障转移:负载均衡器自动剔除故障实例;PostgreSQL主节点故障时,从节点通过Sentinel提升为新主节点;GitLab实例故障时,重启服务或替换服务器并重新配置。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: GitLab在CentOS上的高可用性配置
本文地址: https://pptw.com/jishu/732424.html