GitLab在Debian上的高可用性实现方案
实现GitLab高可用性需先完成以下基础配置:
curl、openssh-server、ca-certificates、postfix等基础依赖,用于GitLab安装与运行。在所有目标服务器上安装GitLab Community Edition(CE)或Enterprise Edition(EE):
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
apt安装GitLab CE,并设置外部访问URL(替换为你的域名或IP)。sudo EXTERNAL_URL="http://your-gitlab-domain.com" apt-get install gitlab-ce
sudo gitlab-ctl reconfigure应用配置,重启GitLab服务使更改生效。GitLab的高可用性需通过负载均衡、数据库复制、存储共享三大组件实现:
使用Nginx或HAProxy作为负载均衡器,将用户请求分发到多个GitLab实例,避免单点故障。
# 安装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
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
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
/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"
GitLab的代码仓库、附件等文件需存储在共享存储中,确保所有实例访问同一份数据。
# 安装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客户端
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实例上,需调整配置以适配高可用架构:
/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,使用外部数据库
高可用性需持续监控系统状态,及时发现并解决问题:
# 手动备份
sudo gitlab-backup create
# 配置定时备份(/etc/crontab)
0 4 * * * root /opt/gitlab/bin/gitlab-backup create
通过模拟故障验证高可用性配置的有效性:
通过以上步骤,可在Debian上实现GitLab的高可用性,确保系统在单点故障时仍能持续提供服务。