使用lsyncd配置gitlab主從實時同步
此文檔是使用lsyncd配置gitlabg主從實時同步;
這個需求的緣由是要通過walle做自動化發(fā)布,而walle需要訪問git倉庫,若是git倉庫g放在異地必定影響發(fā)布時長奈应,所以需要將辦公網(wǎng)的git倉庫同步至生產(chǎn)環(huán)境。操作系統(tǒng)版本:CentOS Linux release 7.2.1511(主) Ubuntu 14.04.5 LTS (從) gitlab版本:gitlab-ce_8.12.6-ce.0_amd64.deb lsyncd版本:lsyncd-2.2.2-1.el7.x86_64```
1.安裝gitlab (從)
[root@gitlab-slave ~]# wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/ubuntu/trusty/gitlab-ce_8.12.6-ce.0_amd64.deb
[root@gitlab-slave ~]# dpkg -i gitlab-ce_8.12.6-ce.0_amd64.deb
[root@gitlab-slave ~]# gitlab-ctl reconfigure
[root@gitlab-slave ~]# gitlab-ctl restart
2.安裝lsyncd(主)
- 由于本人實驗環(huán)境主服務(wù)器已經(jīng)裝gitlab了所以就不再贅述;
- lsyncd參考文檔 https://linux.cn/article-5849-1.html乔妈;
- lysncd介紹:
Lysncd 實際上是lua語言封裝了 inotify 和 rsync 工具波材,采用了 Linux 內(nèi)核(2.6.13 及以后)里的 inotify 觸發(fā)機制唯灵,然后通過rsync去差異同步,達到實時的效果。
我認為它最令人稱道的特性是琐驴,完美解決了 inotify + rsync海量文件同步帶來的文件頻繁發(fā)送文件列表的問題 —— 通過時間延遲或累計觸發(fā)事件次數(shù)實現(xiàn)苍姜。
另外馍乙,它的配置方式很簡單,lua本身就是一種配置語言,可讀性非常強酬诀。
lsyncd也有多種工作模式可以選擇,本地目錄cp,本地目錄rsync蜻懦,遠程目錄rsyncssh带欢。
[root@gitlab-master ~]# yum install epel-release
[root@gitlab-master ~]# yum install lsyncd
[root@gitlab-master ~]# rpm -qa|grep lsync
lsyncd-2.2.2-1.el7.x86_64
[root@gitlab-master ~]# cat /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
settings {
logfile ="/var/log/lsyncd/lsyncd.log",
statusFile ="/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
-- IV. 遠程目錄同步渡贾,rsync模式 + ssh shell
sync {
default.rsync,
source = "/var/opt/gitlab",
target = "root@172.16.0.255:/var/opt/gitlab/",
-- target = "root@172.29.88.223:/remote/dest",
-- 上面target,注意如果是普通用戶,必須擁有寫權(quán)限
exclude = { "backups", "gitlab-ci", "sockets", "gitlab.yml", "redis", "postmaster.pid" },
maxDelays = 5,
delay = 30,
-- init = true,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
bwlimit = 2000
-- rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
-- 如果要指定其它端口智厌,請用上面的rsh
}
}
[root@gitlab-master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.0.255
[root@gitlab-master ~]# systemctl restart lsyncd
3.同步完成后需要重起postgresql (從)
[root@gitlab-slave ~]# gitlab-ctl stop postgresql
[root@gitlab-slave ~]# gitlab-ctl start postgresql