Centos:lsync+rsync 文件實時同步

  web服務在達到必定量級之後,服務器就會出現瓶頸,前不久就出現了這樣的問題。css

  咱們的商城項目一直是單機模式(單服務器運行),有次促銷活動,結果服務器的cpu直接飽滿,飆到了100%,負載達到了100多,這就很恐怖了,長時間這樣的話,隨時都會宕機。html

微信圖片_20191111214327.jpg

  鑑於這種狀況,那就得上負載均衡了,然而負載均衡就是須要多服務器部署,首先要解決的問題就是服務器之間的代碼同步問題,調研過不少方案,好比Jenkins,Walle等部署方式,要不就是太過於複雜,活着就是不穩定,總的來講咱們只須要代碼同步這一個功能,儘量的簡單,像Jenkins這樣的工具,不是說很差,他確實好用,並且老牌的工具,只是如今咱們還沒達到那個量級,也不須要太複雜的東西,言歸正傳,如今就跟你們一步一步的說一下 lsync+rsync 這兩個輕巧的工具。web

準備環境:

系統環境: Centos 7.2

主(master): 192.168.0.113

備(slave): 192.168.0.114

備機操做

1. 安裝rsync
yum -y install xinetd rsync
2. 開啓rsync守護進程
[root@slave ~]# vim /etc/xinetd.d/rsync

service rsync
{
        disable = no
        flags           = IPv4
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
3. 修改後啓動服務並添加開機自啓動
[root@slave ~]# service xinetd start
[root@slave ~]# systemctl enable xinetd.service
4. 配置同步存儲路徑
[root@slave ~]# vim /etc/rsyncd.conf
[web]
path = /data/www
hosts allow = 192.168.0.113
hosts deny = *
list = true
uid = root
gid = root
read only = false
5. 以daemon模式啓動rsync
[root@slave ~]# /usr/bin/rsync  --daemon

主機操做:

1. 新增lsync 源,並安裝(默認沒有)
[root@master ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@master ~]# yum install lsyncd rsync
2. 開啓 lsyncd 配置文件
[root@master ~]# vim /etc/sysconfig/lsyncd

# Keep the space there for systemd. Don't nest variables without
# changing the systemd service file to use: /usr/bin/sh -c 'eval XXXX'
#
LSYNCD_OPTIONS="-pidfile /var/run/lsyncd.pid /etc/lsyncd.conf"(把#去掉)
#LSYNCD_OPTIONS=" "
#LSYNCD_USER=""
3. 增長 lsyncd 主配置
[root@master ~]# vim /etc/lsyncd.conf
注:若有這條請將其註釋,Centos7 默認是沒有註釋掉的(前面加 -- 即註釋)
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}


settings {
   --pidfile = "/var/run/lsyncd.pid",
   --nodaemon   = false,
   inotifyMode = "CloseWrite",
   maxProcesses = 8,
   statusFile = "/tmp/lsyncd.status",
   statusInterval = 1,
   logfile = "/var/log/lsyncd/lsyncd.log"
}



sync {
    default.rsync,
    source    = "/data/www",
    target    = "192.168.0.114::www",
    delete="running",
    excludeFrom = "/etc/rsync_exclude.lst",
    delay = 5,
    init = false,
    rsync     = {
        binary = "/usr/bin/rsync",
        perms = true,
        acls = true,
        xattrs = true,
        links = true,
        quiet = true,
        archive = true,
        compress = true,
        verbose   = true,
        --password_file = "/etc/rsync.pwd",
        _extra    = {"--bwlimit=200"}
        }
    }
lsyncd.conf參數配置詳解:
settings

裏面是全局設置,--開頭表示註釋,下面是幾個經常使用選項說明:

logfile 定義日誌文件
stausFile 定義狀態文件
nodaemon=true 表示不啓用守護模式,默認
statusInterval 將lsyncd的狀態寫入上面的statusFile的間隔,默認10秒
inotifyMode 指定inotify監控的事件,默認是CloseWrite,還能夠是Modify或CloseWrite or Modify
maxProcesses 同步進程的最大個數。假如同時有20個文件須要同步,而maxProcesses = 8,則最大能
看到有8個rysnc進程maxDelays 累計到多少所監控的事件激活一次同步,即便後面的delay
延遲時間還未到
sync

裏面是定義同步參數,能夠繼續使用maxDelays來重寫settings的全局變量。
通常第一個參數指定lsyncd以什麼模式運行:rsync、rsyncssh、direct三種模式:

default.rsync :本地目錄間同步,使用rsync,也能夠達到使用ssh形式的遠程rsync效果,
或daemon方式鏈接遠程rsyncd進程;
default.direct :本地目錄間同步,使用cp、rm等命令完成差別文件備份;
default.rsyncssh :同步到遠程主機目錄,rsync的ssh模式,須要使用key來認證

source 同步的源目錄,使用絕對路徑。

target 定義目的地址.對應不一樣的模式有幾種寫法:
/tmp/dest :本地目錄同步,可用於direct和rsync模式
192.168.0.114:/tmp/dest :同步到遠程服務器目錄,可用於rsync和rsyncssh模式,
拼接的命令相似於/usr/bin/rsync -ltsd --delete --include-from=- --exclude=* SOURCE TARGET,
剩下的就是rsync的內容了,好比指定username,免密碼同步
192.168.0.114::module :同步到遠程服務器目錄,用於rsync模式
三種模式的示例會在後面給出。

init 這是一個優化選項,當init = false,只同步進程啓動之後發生改動事件的文件,
原有的目錄即便有差別也不會同步,默認是true

delay 累計事件,等待rsync同步延時時間,默認15秒(最大累計到1000個不可合併的事件)。也就是15s內監
控目錄下發生的改動,會累積到一次rsync同步,避免過於頻繁的同步。(可合併的意思是,15s內兩次修改了同一文件,
最後只同步最新的文件)

excludeFrom 排除選項,後面指定排除的列表文件,如excludeFrom = "/etc/lsyncd.exclude",
若是是簡單的排除,可使用exclude = LIST。
這裏的排除規則寫法與原生rsync有點不一樣,更爲簡單:
監控路徑裏的任何部分匹配到一個文本,都會被排除,例如/bin/foo/bar能夠匹配規則foo
若是規則以斜線/開頭,則從頭開始要匹配所有
若是規則以/結尾,則要匹配監控路徑的末尾
?匹配任何字符,但不包括/
*匹配0或多個字符,但不包括/
**匹配0或多個字符,能夠是/
delete 爲了保持target與souce徹底同步,Lsyncd默認會delete = true來容許同步刪除。
它除了false,還有startup、running值,請參考 Lsyncd 2.1.x ‖ Layer 4 Config ‖ Default Behavior。
rsync

(提示,delete和exclude原本都是rsync的選項,上面是配置在sync中的,我想這樣作的緣由是爲了減小rsync的開銷)

bwlimit 限速,單位kb/s,與rsync相同(這麼重要的選項在文檔里居然沒有標出)
compress 壓縮傳輸默認爲true。在帶寬與cpu負載之間權衡,本地目錄同步能夠考慮把它設爲false
perms 默認保留文件權限。
其它rsync的選項
其它還有rsyncssh模式獨有的配置項,如host、targetdir、rsync_path、password_file,
見後文示例。rsyncOps={"-avz","--delete"}這樣的寫法在2.1.*版本已經不支持。

lsyncd.conf能夠有多個sync,各自的source,各自的target,各自的模式,互不影響。
4. 將須要過濾的目錄或文件添加到 過濾配置文件列表裏
[root@master ~]# vim /etc/rsync_exclude.lst

logs/
runtime/
5. 修改默認監控的文件數(默認是8192)
[root@master ~]# vim /etc/sysctl.conf
fs.inotify.max_user_watches = 2001192
[root@master ~]# sysctl -p
6. 未開啓lsyncd前先驗一下 rsync 功能是否可正常同步
[root@master ~]# chmod 600 /etc/rsync.pwd

root:root123

[root@master ~]# rsync -avz --delete --exclude-from=/etc/rsync_exclude.lst \
--password-file=/etc/rsync.pwd /data/www/ 192.168.0.114::www
7. 啓動 lsyncd 服務
[root@master ~]# lsyncd -pidfile /var/run/lsyncd.pid /etc/lsyncd.conf
[root@master ~]# ps xf |grep lsync
[root@master ~]# /etc/init.d/lsyncd status
[root@master ~]# systemctl start lsyncd
[root@master ~]# systemctl enable lsyncd
8. 放行防火牆端口 873(rsync默認端口)
開機啓動: systemctl enable firewalld

 從新啓動: systemctl restart firewalld
 
 查看狀態:systemctl status firewalld

 啓動:  systemctl start firewalld
 
 中止:  systemctl stop firewalld

 禁用:systemctl disable firewalld

systemctl unmask firewalld
firewall-cmd --permanent --zone=public --add-port=873/tcp
firewall-cmd --reload

查看全部端口
firewall-cmd --permanent --zone=public --list-ports

致謝

感謝看到這裏,若是你以爲這篇文章幫到了你,能夠請我喝杯咖啡,謝謝。
shang1.jpgvim

相關文章
相關標籤/搜索