本文目标:配置gitlab一主二从(master 192.168.117.129,slave 192.168.117.130,192.168.117.131),其中gitlab-master外挂一主两从的数据库postgresql,gitlab-slave130挂载postgresql-130,gitlab-slave131挂载postgresql-131。

在三台机器上部署gitlab

下载安装包链接: https://pan.baidu.com/s/1geCvvWl54kp_5AJ6O8bJ3w 提取码: tser
其中gitlab-ce-zh110104.tar为gitlab-11.1.4,gitlab-ce-zh100604.tar为gitlab-10.6.4
(1)将安装包上传到/home目录,并加载镜像

1
2
docker load -i gitlab-ce-zh110104.tar
vim /home/gitlab/docker-compose.yml #新建yml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
version: '2'
services:
gitlab:
image: 'twang2218/gitlab-ce-zh:11.1.4'
restart: unless-stopped
hostname: '192.168.117.129'
environment:
TZ: 'Asia/Shanghai'
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://192.168.117.129'
gitlab_rails['time_zone'] = 'Asia/Shanghai'
gitlab_rails['gitlab_shell_ssh_port'] = 54322
postgresql['enable'] = false
gitlab_rails['db_adapter'] = "postgresql"
gitlab_rails['db_encoding'] = "utf8"
gitlab_rails['db_database'] = "gitlabtest"
gitlab_rails['db_username'] = "postgres"
gitlab_rails['db_password'] = "postgres"
gitlab_rails['db_host'] = "192.168.117.129"
gitlab_rails['db_port'] = 5432
ports:
- '8080:80'
- '843:443'
- '54322:22'
volumes:
- '/data/gitlab/config:/etc/gitlab'
- '/data/gitlab/logs:/var/log/gitlab'
- '/data/gitlab/config/gitlab/data:/var/opt/gitlab'

注意:上述postgresql挂载分别为三台机器上各自的postgresql

1
docker-compose up -d

安装keepalived服务

Gitlab-master
(1)为keepalived开启转发

1
2
3
4
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1
[root@localhost ~]# systemctl restart keepalived

(2)修改keepalived的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    vim /etc/keepalived/keepalived.conf

vrrp_script chk_gitlab{
script "/etc/keepalived/check-gitlab.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 101
authentication {
auth_type PASS
auth_pass gitlab
}
track_script {
chk_gitlab
}
virtual_ipaddress {
192.168.117.208
}
}

(3)创建检测脚本

1
2
3
4
5
6
7
#!/bin/bash

return_code=`curl -s -w "%{http_code}" -o /dev/null http://192.168.117.129`

if [[ $return_code -ne 200 ]]; then
systemctl stop keepalived
fi

(4)重启keepalived

1
systemctl restart keepalived

Gitlab-slave按照上述步骤配置keepalived,只需要把其中的ip换成slave机器的ip,priority值要比master小。

安装Rsyncd服务

安装包下载链接: https://pan.baidu.com/s/1s1nPjzM9w9M8e0V4EqcZog 提取码: gv5w
master-129
(1)安装

1
yum localinstall *.rpm

(2))创建用户名和密码

1
2
useradd forgitlab  创建用户forgitlab
passwd forgitlab 给已创建的用户forgitlab设置密码为pass123

(3)修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
vim /etc/rsyncd.conf

#设置rsync运行权限为root
uid=root
#设置rsync运行权限为root
gid=root
#最大连接数
max connections=3
#默认为true,修改为no,增加对目录文件软连接的备份
use chroot=no
#日志文件位置,启动rsync后自动产生这个文件,无需提前创建
log file=/var/log/rsyncd.log
#pid文件的存放位置
pid file=/var/run/rsyncd.pid
#支持max connections参数的锁文件
lock file=/var/run/rsyncd.lock
#用户认证配置文件,里面保存用户名称和密码 需要创建(可选)
secrets file=/etc/rsync.pass
#允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开,可设置所有
hosts allow= *
#设置rsync服务端文件为读写权限
read only = no
#不显示rsync服务端资源列表
list = no

[forgitlab]
#需要备份的源主机数据目录路径
path = /data/gitlab/data/git-data
#执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开 可选配置
auth users = forgitlab

(4)创建认证文件
如果在rsyncd服务中定义了可选配置,则需创建认证文件。

1
2
3
[root@localhost ~]# vim /etc/rsync.pass
forgitlab:pass123
chmod 600 /etc/rsync.pass

(5)启动Rsyncd服务

1
2
[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl enable rsyncd

(6)开启rsyncd服务端口

1
2
3
[root@localhost ~]# firewall-cmd --permanent --add-port=873/tcp
[root@localhost ~]# firewall-cmd --permanent --add-port=873/udp
[root@localhost ~]# firewall-cmd --reload

slave-130 master备节点
(1)安装

1
yum localinstall *.rpm

(2)创建认证文件

1
2
3
4
5
6
    [root@localhost ~]# vim /etc/rsync.pass
pass123
chmod 600 /etc/rsync.pass
(3)手动测试
``` bash
[root@localhost ~]# rsync -avzrtlp --progress --delete --password-file=/etc/rsync.pass forgitlab@192.168.117.129::forgitlab /data/gitlab/data/git-data

(4)自动执行

1
2
[root@localhost ~]# crontab -e
*/5 * * * * rsync -avzrtlp --progress --delete --password-file=/etc/rsync.pass forgitlab@192.168.117.129::forgitlab /data/gitlab/data/git-data

【说明1】每5分钟同步一次。
【说明2】如果出现目录可以同步,文本文件类型的文件不能同步,请检查SELinux是否关闭
1、临时关闭:输入命令setenforce 0,重启系统后还会开启。
2、永久关闭:输入命令vi /etc/selinux/config,将SELINUX=enforcing改为SELINUX=disabled,然后保存退出。