日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rsync实现负载均衡集群文件同步,搭建线上测试部署环境

發布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rsync实现负载均衡集群文件同步,搭建线上测试部署环境 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



此次測試使用三臺服務器CentOS7(1511)版本

192.168.254.1 ?web服務器+線上測試環境+源站

192.168.254.2 ?web服務器

192.168.254.3 ?web服務器

?

其中2 3 服務器作為集群中的web服務器,對外開放,是負載均衡集群的部分。

其中1 服務器不對外開放,代碼發布到該服務器,在該服務器上進行測試,完成后程序由該服務器同步到其他集群服務器上,同時網站后臺以及自動腳本位于該服務器上。(如果為了安全期間,大家可以對該服務器進行配置,只允許你們公司內部網絡訪問,在家時通過VPN連接內部網絡,這樣就可以確保后臺安全)

這里講兩個場景供參考

1.?通過shell腳本,實現文件夾實時同步

2.?1服務器上制定一個規則,即只要rsync.txt存在我們就開始同步,這樣只要開發上傳該文件同步就開始,同步完成后自動刪除該文件。

?

第一步:安裝rsync

centos 7(1511)版本,已經默認安裝了rsync,所以就不再進行安裝,未安裝的可以自行安裝,過程相對簡單

第二步:安裝inotify和inotify-tools

Centos 7已經默認安裝了inotify如果要查看是否安裝可以使用如下命令

ll /proc/sys/fs/inotify

如果列出如下三項,則證明已經安裝

-rw-r--r-- 1 root root 0 21 13:59 max_queued_events
-rw-r--r-- 1 root root 0 21 13:59 max_user_instances
-rw-r--r-- 1 root root 0 21 13:59 max_user_watches

沒有安裝的可以自行安裝

然后需要安裝inotify-tools工具

tar?-zxvf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14

./configure

make?&& make?install

?

第三步:對客戶端服務端進行配置

記住1是客戶端,2 3 需要同步的服務端將1的文件同步到2 3上面

首先對 2 3 進行配置,相應配置文件 ? /etc/rsyncd.conf

?

uid = nobody

gid = nobody

use chroot?= no

max connections = 10

strict mode = no

pid file?= /var/run/rsyncd.pid

lock file?= /var/run/rsync.lock

log file?= /usr/data/rsync/rsyncd.log

[laiwojia-data]

path = /usr/website/html/www.laiwojia.la/data/

comment = web4 files

ignore errors

read only = no

write?noly = no

hosts allow = 192.168.254.1

hosts deny = *

list = false

uid = root

gid = root

auth users = root

secrets file?= /usr/local/rsync/conf/server.pass

?

[laiwojia]

path = /usr/website/html/www.laiwojia.la/

comment = web4 files

ignore errors

read only = no

write?noly = no

hosts allow = 192.168.254.1

hosts deny = *

list = false

uid = root

gid = root

auth users = root

secrets file?= /usr/local/rsync/conf/server.pass

?

這里配置了兩個模塊 'laiwojia-data''laiwojia' ,其中'laiwojia-dada'只要發生變化就要同步的(想想大家把后臺放在1上面,那么這個data里面的文件就是后臺生成的持久化文件,后臺配置,然后整個集群通用),而'laiwojia'了這個模塊是發布系統所用的模塊,需要在站點下有'rsync.txt'文件時才同步。

?

從上面的配置可以看出'/usr/website/html/www.laiwojia.la/data/'這個目錄必須存在, '/usr/local/rsync/conf/server.pass'這個密碼文件也必須存在

# mkdir -p /usr/website/html/www.laiwojia.la/data/

# mkdir -p /usr/local/rsync/conf/

密碼文件/usr/local/rsync/conf/server.pass中的內容為

root:123abc+-

記住2 3 作為服務端 密碼文件要有前綴,如果寫成 '123abc+-' 后面就會不通過

給密碼文件設置訪問權限:

# chmod 600 server.pass

?

然后我們對1進行配置,這里貼出配置文件

?

uid = nobody

gid = nobody

use chroot?= no

max connections = 10

strict mode = yes

pid file?= /var/run/rsyncd.pid

lock file?= /var/run/rsync.lock

log file?= /usr/data/rsync/rsyncd.log

?

建立目錄:

# mkdir -p /usr/website/html/www.laiwojia.la/data/

# mkdir -p /usr/local/rsync/conf/

然后還要建立一個密碼文件'/usr/local/rsync/conf/server.pass' 由于1是客戶端,因此密碼沒有前綴

123abc+-

設置訪問權限:

# chmod 600 server.pass

?

第四步:編寫shell腳本

首先是改變就同步的shell腳本,命名 'datarsync.sh' 內容為

?

#!/bin/bash

host2=192.168.254.2

host3=192.168.254.3

?

src=/usr/website/html/www.laiwojia.la/data/

dst2=laiwojia-data

dst3=laiwojia-data

user=root

?

/usr/local/bin/inotifywait\

?-mrq --timefmt '%d/%m/%y'\

?--format '%T %w%f%e'\

?-e ?modify,delete,create,attrib $src\

|while?read files

????do

????/usr/bin/rsync ?-vzrtopg --delete --progress --password-file=/usr/local/rsync/conf/server.pass ?$src $user@$host2::$dst2

????/usr/bin/rsync ?-vzrtopg --delete --progress --password-file=/usr/local/rsync/conf/server.pass ?$src $user@$host3::$dst3

????echo?"${files} was rsyncd"?>>/tmp/rsync.log 2>&1

done

?

測試:

運行shell腳本

# sh datarsync.sh

'/usr/website/html/www.laiwojia.la/data/' 目錄下建立test.php 并改寫其內容

?

vim test.php

?

#以下是內容

<?php

this is a test

hello tom!

?

看看運行結果

?

然后查看2 3 機器上是否有test.php ?發現服務端都有test.php 文件,查看內容

cat?test.php

<?php
this is a test
hello tom

實時同步成功!

?

?

第二個場景

制定一個規則實現線上測試部署后同步,首先我們還是要建立一個shell腳本,命名為harsync.sh,內如如下

?

!/bin/bash

host2=192.168.254.2

host3=192.168.254.3

?

src=/usr/website/html/www.laiwojia.la/

excludedir=$src"data/"?$src"rsync.txt"

dst3=laiwojia

dst4=laiwojia

user=root

rsync_file=${src}"rsync.txt"

?

if?[ -f "$rsync_file"?]

then

????????/usr/bin/rsync ?-vzrtopg --delete --progress --exclude=$excludedir ?--password-file=/usr/local/rsync/conf/server.pass ?$src $user@$host2::$dst2

????????/usr/bin/rsync ?-vzrtopg --delete --progress --exclude=$excludedir ?--password-file=/usr/local/rsync/conf/server.pass ?$src $user@$host3::$dst3

fi

rm?-rf $src"rsync.txt"

?

測試

touch?rsync.txt

touch?test.html

運行腳本

# sh harsync.sh

各個服務器上查看是否同步。這樣,如果你把代碼發到1上,沒有rsync.txt就不會同步,可以進行線上測試,等測試好了,可以通過上傳一個rsync.txt 然后就可以實現文件同步

?

?

?

rsync 常見錯誤與解決方法整理

問題一:

@ERROR: chroot failed

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:

服務器端的目錄不存在或無權限,創建目錄并修正權限可解決問題。

問題二:

@ERROR: auth failed on module tee

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:

服務器端該模塊(tee)需要驗證用戶名密碼,但客戶端沒有提供正確的用戶名密碼,認證失敗。

提供正確的用戶名密碼解決此問題。

問題三:

@ERROR: Unknown module ‘tee_nonexists'

rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]

原因:

服務器不存在指定模塊。提供正確的模塊名或在服務器端修改成你要的模塊以解決問題。

問題1

在client上遇到問題:

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/

rsync: could not open password file "/etc/rsync.pas": No such file or directory (2)

Password:

@ERROR: auth failed on module backup

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]

遇到這個問題:client端沒有設置/etc/rsync.pas這個文件,而在使用rsync命令的時候,加了這個參數--

password-file=/etc/rsync.pas

問題2

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/

@ERROR: auth failed on module backup

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]

遇到這個問題:client端已經設置/etc/rsync.pas這個文件,里面也設置了密碼111111,和服務器一致,但是

服務器段設置有錯誤,服務器端應該設置/etc/rsync.pas ,里面內容root:111111 ,這里登陸名不可缺少

問題3

rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/

@ERROR: chdir failed

rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]

遇到這個問題,是因為服務器端的/home/backup 其中backup這個目錄并沒有設置,所以提示:chdir failed

問題4

rsync: write failed on "/home/backup2010/wensong": No space left on device (28)

rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7]

rsync: connection unexpectedly closed (2721 bytes received so far) [generator]

rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7]

磁盤空間不夠,所以無法操作。

可以通過df /home/backup2010 來查看可用空間和已用空間

?

問題5:網絡收集問題

1、權限問題

類似如下的提示:rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)注意查看同步的目錄權限是否為755

2、time out

rsync: failed to connect to 203.100.192.66: Connection timed out (110)

rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]

檢查服務器的端口netstat –tunlp,遠程telnet測試。

可能因為客戶端或者服務端的防火墻開啟 導致無法通信,可以設置規則放行?rsync(873端口) 或者直接關閉防火墻。

還有一種在同步過程中可能會提示沒有權限 (將同步目錄加上SvcwRsync全部權限即可,更簡單的方法就是將SvcwRsync設為管理員即可)

3、服務未啟動

rsync: failed to connect to 10.10.10.170: Connection refused (111)

rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]

啟動服務:rsync --daemon --config=/etc/rsyncd.conf

4、磁盤空間滿

rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)

*** Skipping any contents from this failed directory ***

5、Ctrl+C或者大量文件

rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5]

rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [generator=3.0.5]

說明:導致此問題多半是服務端服務沒有被正常啟動,到服務器上去查查服務是否有啟動,然后查看下?/var/run/rsync.pid 文件是否存在,最干脆的方法是殺死已經啟動了服務,然后再次啟動服務或者讓腳本加入系統啟動服務級別然后shutdown -r now服務器

6、xnetid啟動

rsync: read error: Connection reset by peer (104)

rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5]

查看rsync日志

rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory

xnetid查找的配置文件位置默認是/etc下,根據具體情況創建軟鏈接。例如:

ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf

或者更改指定默認的配置文件路徑,在/etc/xinetd.d/rsync配置文件中。

Rsync configure:

配置一:

ignore errors

說明:這個選項最好加上,否則再很多crontab的時候往往發生錯誤你也未可知,因為你不可能天天去看每時每刻去看log,不加上這個出現錯誤的幾率相對會很高,因為任何大點的項目和系統,磁盤IO都是一個瓶頸

Rsync error

錯誤一:

@ERROR: auth failed on module xxxxx

rsync: connection unexpectedly closed (90 bytes read so far)

rsync error: error in rsync protocol data stream (code 12) at io.c(150)

說明:這是因為密碼設置錯了,無法登入成功,檢查一下rsync.pwd,看客服是否匹配。還有服務器端沒啟動rsync 服務也會出現這種情況。

錯誤二:

password file must not be other-accessible

continuing without password file

Password:

說明:這是因為rsyncd.pwd rsyncd.sec的權限不對,應該設置為600。如:chmod 600 rsyncd.pwd

錯誤三:

@ERROR: chroot failed

rsync: connection unexpectedly closed (75 bytes read so far)

rsync error: error in rsync protocol data stream (code 12) at io.c(150)

說明:這是因為你在?rsync.conf 中設置的?path 路徑不存在,要新建目錄才能開啟同步

錯誤四:

rsync: failed to connect to 218.107.243.2: No route to host (113)

rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]

說明:防火墻問題導致,這個最好先徹底關閉防火墻,排錯的基本法就是這樣,無論是S還是C,還有ignore errors選項問題也會導致

錯誤五:

@ERROR: access denied to www from unknown (192.168.1.123)

rsync: connection unexpectedly closed (0 bytes received so far) [receiver]

rsync error: error in rsync protocol data stream (code 12) at io.c(359)

說明:此問題很明顯,是配置選項host allow的問題,初學者喜歡一個允許段做成一個配置,然后模塊又是同一個,致使導致

錯誤六:

rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]

rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]

說明:導致此問題多半是服務端服務沒有被正常啟動,到服務器上去查查服務是否有啟動,然后查看下?/var/run/rsync.pid 文件是否存在,最干脆的方法是殺死已經啟動了服務,然后再次啟動服務或者讓腳本加入系統啟動服務級別然后shutdown -r now服務器

錯誤七:

rsync: read error: Connection reset by peer (104)

rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]

說明:原數據目錄里沒有數據存在

總結

以上是生活随笔為你收集整理的rsync实现负载均衡集群文件同步,搭建线上测试部署环境的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。