閑來無事,搭建一個負載均衡集群,至於負載均衡集群搭建過程,找時間寫下。這次主要寫集群之間的文件同步,以及線上測試環境的搭建。
筆者看過很多公司都沒有線上測試環境,真是崩潰了,不造怎么確保線上線下環境一致的。
筆者此次使用三台服務器:
192.168.138.3 web服務器
192.168.138.4 web服務器
192.168.138.10 web服務器+線上測試環境+源站
其中3 4 服務器作為集群中的web服務器,對外開放,是負載均衡集群的部分。
其中10 服務器不對外開放,代碼發布到該服務器,在該服務器上進行測試,完成后程序由該服務器同步到其他集群服務器上,同時網站后台以及自動腳本位於該服務器上。(如果為了安全期間,大家可以對該服務器進行配置,只允許你們公司內部網絡訪問,在家時通過VPN連接內部網絡,這樣就可以確保后台安全)
本文主要是大家rsync服務,至於IP配置,web服務器搭建,大家可以看我之前的文章。
這里我們為了開發方便,在10服務器上制定一個規則,即只要rsync.txt存在我們就開始同步,這樣只要開發上傳該文件同步就開始,同步完成后自動刪除該文件。
第一步:筆者這里安裝的是centos6.4 已經默認安裝了rsync 所以筆者就不再進行安裝,未安裝的可以自行安裝,過程相對簡單
第二步:安裝inotify inotify-tools
筆者這里centos6.4已經默認安裝了inotify , 如果要查看是否安裝可以使用如下命令
ll /proc/sys/fs/inotify
如果列出如下三項,則證明已經安裝
-rw-r--r-- 1 root root 0 2月 1 13:59 max_queued_events
-rw-r--r-- 1 root root 0 2月 1 13:59 max_user_instances
-rw-r--r-- 1 root root 0 2月 1 13:59 max_user_watches
沒有安裝的讀者可以自行安裝,然后我們需要安裝inotify-tools工具
tar -zxvf inotify-tools-3.14.tar.gz cd inotify-tools-3.14 ./configure --prefix=/usr/local/inotify-tools make && make install
第三步:對客戶端 服務端進行配置
講之前一定要搞清楚 10是客戶端 3 4 是否服務端 這個不要搞倒了 是10的文件同步到3 4 上面
首先對 3 4 進行配置,這里筆者貼出自己的配置文件 /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.138.10 hosts deny = * list = false uid = root gid = root auth users = laiwojia 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.138.10 hosts deny = * list = false uid = root gid = root auth users = laiwojia secrets file = /usr/local/rsync/conf/server.pass
我們這里配置了兩個模塊 'laiwojia-data'和'laiwojia' ,其中'laiwojia-dada'只要發生變化就要同步的(想想大家把后台放在10上面,那么這個data里面的文件就是后台生成的持久化文件,后台配置,然后整個集群通用),而'laiwojia'了這個模塊是發布系統所用的模塊,需要在站點下有'rsync.txt'文件時才同步。(由此我們可以看出,'data'中的文件不能通過發布系統發布)
看了上面的配置,大家就明白,'/usr/website/html/www.laiwojia.la/data/'這個目錄必須存在, '/usr/local/rsync/conf/server.pass'這個秘密文件也必須存在
秘密文件中的內容為
laiwojia:123abc+-
記住3 4 作為服務端 密碼文件要有前綴 有的淫寫成 '123abc+-' 后面就會不通過
然后我們對10進行配置,這里筆者貼出配置文件
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
然后還要建立一個秘密文件'/usr/local/rsync/conf/server.pass' 由於10是客戶端,因此秘密沒有前綴
123abc+-
第四步:編寫shell腳本
首先是改變就同步的shell腳本,我們命名為 'inotify-ha-rsync.sh' 內容為
#!/bin/bash host3=192.168.138.3 host4=192.168.138.4 src=/usr/website/html/www.laiwojia.la/data/ dst3=laiwojia-data dst4=laiwojia-data user=laiwojia /usr/local/inotify-tools/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@$host3::$dst3 /usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/conf/server.pass $src $user@$host4::$dst4 echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1 done
OK,我們來測試下
在'/usr/website/html/www.laiwojia.la/data/' 目錄下建立test.php 並改寫其內容,我們看看結果(事先要運行這個shell)
vim test.php #以下是內容 <?php this is a test hello tom!
看看運行結果
然后看看3 和 4 機器上是否有test.php 發現兩個中都有test.php 文件,好了我們看看內容
cat test.php
<?php
this is a test
hello tom
OK,這個實時同步的算是完成了
剩下我們要看看考慮到線上測試部署的,首先我們還是要建立一個shell腳本,命名為ha-rsync.sh,內如如下
!/bin/bash host3=192.168.138.4 host4=192.168.138.3 src=/usr/website/html/www.laiwojia.la/ excludedir=$src"data/" $src"rsync.txt" dst3=laiwojia dst4=laiwojia user=laiwojia 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@$host3::$dst3 /usr/bin/rsync -vzrtopg --delete --progress --exclude=$excludedir --password-file=/usr/local/rsync/conf/server.pass $src $user@$host4::$dst4 fi rm -rf $src"rsync.txt"
好我們來測試下
touch rsync.txt touch test.html
然后運行腳本,到各個服務器上看看,是不是都有了。這樣的話,如果你把代碼發到10上,沒有rsync.txt就不會同步,那么這是測試可以進行線上測試,等測試好了,開發就上傳一個rsync.txt 然后就開始同步了
當然了,這里還可以搞得更復雜,比如安全起見,一些配置文件不要讓它同步以免某天你發錯了把本地的給發上去了,還可以指定那些目錄不同步,等等,這些都可以實現,只不過shell寫的復雜一點而已