web服務在達到必定量級之後,服務器就會出現瓶頸,前不久就出現了這樣的問題。css
咱們的商城項目一直是單機模式(單服務器運行),有次促銷活動,結果服務器的cpu直接飽滿,飆到了100%,負載達到了100多,這就很恐怖了,長時間這樣的話,隨時都會宕機。html
鑑於這種狀況,那就得上負載均衡了,然而負載均衡就是須要多服務器部署,首先要解決的問題就是服務器之間的代碼同步問題,調研過不少方案,好比Jenkins,Walle等部署方式,要不就是太過於複雜,活着就是不穩定,總的來講咱們只須要代碼同步這一個功能,儘量的簡單,像Jenkins這樣的工具,不是說很差,他確實好用,並且老牌的工具,只是如今咱們還沒達到那個量級,也不須要太複雜的東西,言歸正傳,如今就跟你們一步一步的說一下 lsync+rsync
這兩個輕巧的工具。web
系統環境: Centos 7.2 主(master): 192.168.0.113 備(slave): 192.168.0.114
yum -y install xinetd 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 }
[root@slave ~]# service xinetd start [root@slave ~]# systemctl enable xinetd.service
[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
[root@slave ~]# /usr/bin/rsync --daemon
[root@master ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo [root@master ~]# yum install lsyncd rsync
[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=""
[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"} } }
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,各自的模式,互不影響。
[root@master ~]# vim /etc/rsync_exclude.lst logs/ runtime/
[root@master ~]# vim /etc/sysctl.conf fs.inotify.max_user_watches = 2001192 [root@master ~]# sysctl -p
[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
[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
開機啓動: 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
感謝看到這裏,若是你以爲這篇文章幫到了你,能夠請我喝杯咖啡,謝謝。vim