1. 程式人生 > >nginx讀寫分離和notify測試

nginx讀寫分離和notify測試

實驗環境: 四臺主機,一臺kvm3作為客戶端做測試. 另外三臺組成一個小型的nginx讀寫分離架構,一臺安裝nginx做前端代理,另外兩臺安裝apache做為後端的伺服器。 kvm3:192.168.122.10 nginx:10.30.162.142 apache:kvm1(寫):192.168.122.143、kvm2(讀):192.168.122.140 以上主機全部做過解析 一、 配置後端兩臺apache伺服器: 1.1、配置用作讀的apache,略 1.2、配置用作寫的apache伺服器 後端的伺服器使用的是Apache,apache想要支援上傳檔案,需藉助於WebDAV協議,需要在apache啟動時載入WebDAV的模組。

1、確保kvm1伺服器開啟DAV功能,確保Apache配置檔案載入了DAV相關模組 
[[email protected] conf.modules.d]# cat 00-dav.conf
LoadModule dav_module modules/mod_dav.so
LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule dav_lock_module modules/mod_dav_lock.so

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
在相應網頁根目錄授權段中開啟DAV 
<Directoy "/var/www/html">
	....
	dav on 
</Directory>
[
[email protected]
~]# systemctl restart httpd 2、確保執行httpd程序的apache使用者對網頁根目錄擁有寫入許可權 [[email protected] ~]# setfacl -m u:apache:rwx /var/www/html/ 3、客戶端測試上傳檔案 [[email protected] ~] curl -T /etc/issue http://kvm1 [[email protected] ~]# cd /var/www/html/ [[email protected] html]# ls index.html issue

二、配置前端nginx代理

#安裝二進位制nginx,配置上傳檔案(寫)的後端apache是kvm1,讀是kvm2
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
location / {
      proxy_pass http://kvm2;

      proxy_set_header Host $host;
      proxy_set_header x-real-ip $remote_addr;
      if ($request_method = PUT){
       proxy_pass http://kvm1;
            }
    }
#測試代理,讀的測試略。
[[email protected] ~] curl -T /etc/hosts http://proxy
[[email protected] html]# ls
index.html  issue hosts

問題:一個client將一個檔案上傳到apache寫伺服器(kvm1)上,但是訪問的卻是apache讀(kvm2)伺服器,那麼就看不到這個檔案了!所以需要讓寫伺服器的/var/www/html下的檔案隨時的同步到讀伺服器主機上。 兩種解決辦法: 1、後端兩臺伺服器通過rsync+inotify進行資料同步,保證上傳的資料可以同步到後端所有伺服器上 2、做一個儲存伺服器,通過NFS共享在102和103分別掛載使用

三、要做寫伺服器的/var/www/html同步到讀伺服器的此目錄下。 在讀伺服器設定如下

[[email protected] ~]# yum -y install rsync
[[email protected] ~]# vim /etc/rsyncd.conf 
uid = root   #設定rsync執行許可權為root
gid = root  #設定rsync執行許可權為root
use chroot = no   #預設為true,修改為no,增加對目錄檔案軟連線的備份
port = 873     #預設埠
max connections = 2000  
pid file = /var/run/rsyncd.pid   #自定義,啟動建立
log file = /var/log/rsyncd.log       #自定義,啟動建立
lock file = /var/run/rsyncd.lock      #自定義,啟動建立 
log format = %t %a %m %f %b
#exclude = lost+found/ 
##transfer logging = yes
#timeout = 200
  
[rsync]  #模組名
path = /var/www/html/  #rsync服務端資料目錄路徑
commemt = rsync  #模組名稱與自定義名稱相同
list = yes   #不顯示rsync服務端資源列表
read only = no 
write only = no
auth users = test   #執行資料同步的使用者名稱,可以設定多個,用英文狀態下逗號隔開
secrets file = /etc/rsyncd.secret  #使用者認證配置檔案,裡面儲存使用者名稱稱和密碼,檔案自定義
ignore errors = yes   
hosts allow = 192.168.122.0/255 #允許進行資料同步的客戶端IP地址,可以設定多個,用英文狀態下逗號隔開
hosts deny = 0.0.0.0/32  #禁止進行資料同步的客戶端IP地址,可以設定多個,用英文狀態下逗號隔開

建立使用者認證配置檔案的目錄,修改許可權為600,擁有者是root
[[email protected] ~]# echo "test:test123" > /etc/rsyncd.secret    #格式為  使用者:密碼
[[email protected] ~]# chmod 600 /etc/rsyncd.secret
[[email protected] ~]# ll /etc/rsyncd.secret
-rw------- 1 root root 13 Oct 20 11:07 /etc/rsyncd.secret
[[email protected] ~]# systemctl restart rsyncd.service 

寫伺服器端操作:安裝rsync同步,通過inotify自動同步
1、安裝rsync,配置如下
[[email protected] ~]# cat /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
port = 873
max connections = 2000
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log 
lock file = /var/run/rsyncd.lock
log format = %t %a %m %f %b
#exclude = lost+found/
#transfer logging = yes
timeout = 200
#ignore nonreadable = yes
#dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[rsync]
path = /var/www/html/
commemt = rsync
list = yes
read only = no
write only = no
auth users = test
secrets file = /etc/rsyncd.secret
ignore errors = yes
hosts allow = *
[[email protected] ~]# cat /etc/rsyncd.secret 
test123  #只有密碼
[[email protected] ~]# ll /etc/rsyncd.secret
-rw------- 1 root root 8 Oct 20 11:07 /etc/rsyncd.secret   #許可權必須是600
2、測試能否同步
[[email protected] ~]#rsync -avH --port=873 --progress --delete  /var/www/html/  [email protected]::rsync  --password-file=/etc/rsyncd.secret
sending incremental file list
sent 150 bytes  received 12 bytes  324.00 bytes/sec
total size is 782  speedup is 4.83
或者用以下命令
[[email protected] ~]# cd /var/www/html && rsync -aruz -R --delete ./  --timeout=100 [email protected]::rsync --password-file=/etc/rsyncd.secret
3、安裝inotify通過指令碼來實現自動同步
[[email protected] html]# yum install inotify-tools   -y
[[email protected] ~]# cat inotify_rsync.sh 
#!/bin/bash
host=192.168.122.140  #同步的目標ip
src=/var/www/html/  #目錄名
dest=rsync  #模板名
user=test  #認證使用者
rsync_passfile=/etc/rsyncd.secret  #認證密碼
inotify_home=/usr/   #inotify的安裝目錄,主要是為了下面使用inotifywait命令
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Directory"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
rsync -avH --port=873 --progress --delete $src  --timeout=100 [email protected]$host::$dest --password-file=${rsync_passfile}   >/dev/null 2>&1
done
exit 0
#rsync -avH --port=873 --progress --delete  /var/www/html/  [email protected]::rsync --password-file=/etc/rsyncd.secret

4、測試自動同步效果
1、讀伺服器必須開啟rsync服務
[[email protected] ~]# systemctl start rsyncd
2、寫伺服器必須執行指令碼
[[email protected] ~]# sh inotify_rsync.sh &
[1] 3852
3、通過客戶端測試是否代理成功和實現自動同步
[[email protected] ~]# curl -T example.txt http://proxy
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /example.txt has been created.</p>
</body></html>
4、檢視結果
[[email protected] ~]# ls /var/www/html/
example.txt  fstab  hosts  test2.txt  test.txt
[[email protected] ~]# ls /var/www/html/
example.txt  fstab  hosts  test2.txt  test.txt

五、實驗中錯誤 測試能否同步時報錯 @ERROR: auth failed on module rsync rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2] rsync出現這個問題,說明前期的rsync服務搭建已經沒問題了,客戶端已經可以連線上伺服器,auth失敗可能是因為目錄的許可權不是600,密碼錯誤,認證配置的格式錯誤等。