公司如今要對全部的重要服務進行雙機,高可用,或者冷備份等等。當前有臺很重要的業務數據存儲在本地是一件很不安全的作法。在一次升級討論中,領導提出要進行升級改造,將業務數據存放在單獨的文件服務器,由於有些業務機器運行在windows中,Unix&&Windows 平臺共享方案--Samba首當其衝了。
那麼Samba如何實現雙機以及數據同步呢?答案不止一種了。node通過研究,發現這種方案對於當前的業務場景很是貼近:vim
Heartbeat+Samba實現雙機熱備 實現samba的雙機集羣,當smbsvr1宕機後,smbsvr2能及時的提供服務;當smbsvr1恢復正常後,smbsvr2退出做爲備用機 Rsync+Inotify-tools實現數據的實時同步 保證文件的一致性。
在文章以前,你們最好可以看下heartbeat的基礎內容,大神請繞過。
https://blog.51cto.com/ljohn/2047150windows
拓撲:
安全
VIP:172.16.3.110 smbsvr1: eth0:172.16.3.89 smbsvr2: eth0:172.16.3.90 ##注意: 配置集羣的前提: (1) 時間同步; (2) 基於當前正在使用的主機名互相訪問; (3) 是否會用到仲裁設備;
程序 | 版本 |
---|---|
samba | 3.6.23 |
heartbeat | 3.0.4 |
rsync | 3.0.6 |
inotify-tools | 3.14 |
一、smbsvr1bash
# yum -y install samba heartbeat # cp /usr/share/doc/heartbeat-3.0.4/{ha.cf,authkeys,haresources} /etc/ha.d/ # cd /etc/ha.d/ 配置ha.cf # grep -E -v '^#|^$' /etc/ha.d/ha.cf debugfile /var/log/ha-debug logfile /var/log/ha-log logfacility local0 keepalive 2 deadtime 30 warntime 10 initdead 120 udpport 694 ucast eth0 172.16.3.90 auto_failback on node smbsvr1 node smbsvr2 ping 172.16.3.254 respawn hacluster /usr/lib64/heartbeat/ipfail 配置authkeys # grep -E -v '^#|^$' /etc/ha.d/authkeys auth 3 3 md5 Hello! ##去掉這兩行前的#號 配置haresources # grep -E -v '^#|^$' /etc/ha.d/haresources smbsvr1 172.16.3.110/24/eth0:0 smb ##文件末尾添加此行 配置samba # mkdir -pv /szt #建立共享文件夾 # vim /etc/samba/smb.conf 修改security = share 在末行加入如下內容: [szt] comment = share all path = /szt browseable = yes public = yes writeable = yes guest ok = yes
二、smbsvr2服務器
配置參照「一、smbsvr1」 除了ha.cf配置有變化(ucast eth0 172.16.3.89 IP地址爲對方節點的),其他都步驟均保持同樣 # grep -E -v '^#|^$' /etc/ha.d/ha.cf debugfile /var/log/ha-debug logfile /var/log/ha-log logfacility local0 keepalive 2 deadtime 30 warntime 10 initdead 120 udpport 694 ucast eth0 172.16.3.89 auto_failback on node smbsvr1 node smbsvr2 ping 172.16.3.254 respawn hacluster /usr/lib64/heartbeat/ipfail
三、啓動測試app
兩臺機器分別heartbeat # service heartbeat start # 查看smbsvr1 ip地址 # ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:8C:14:95 inet addr:172.16.3.89 Bcast:172.16.3.255 Mask:255.255.255.0 inet6 addr: fd22:455a:117:0:250:56ff:fe8c:1495/64 Scope:Global inet6 addr: fe80::250:56ff:fe8c:1495/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1057 errors:0 dropped:0 overruns:0 frame:0 TX packets:469 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:128877 (125.8 KiB) TX bytes:79903 (78.0 KiB) eth0:0 Link encap:Ethernet HWaddr 00:50:56:8C:14:95 inet addr:172.16.3.110 Bcast:172.16.3.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 # service smb status smbd (pid 1701) is running...
如此以來就實現了heartbeat+samba雙機熱備,是否是很簡單呢,固然還存在一些問題,數據如何保持一致?若是smb服務關閉,heartbeat會不會產生腦裂呢? ide
數據同步也可參照筆者的另外一篇文章:
https://blog.51cto.com/ljohn/2047156
這篇文章僅僅實現了單向的文件同步,雙向同步要按照步驟反過來再次部署一次。oop
smbsvr1與smbsvr2 兩個節點的配置基本保持一致,這裏僅提供相關服務的配置過程,其餘再也不贅述。測試
# yum -y install rsync xinetd inotify-tools # cp /etc/xinetd.d/rsync{,.bak} #配置sync # sed -i -e 's/= yes/= no/g' /etc/xinetd.d/rsync # cat >/etc/rsyncd.conf <<EOF logfile = /var/log/rsyncd.log pidfile = /var/run/rsyncd.pid lockfile = /var/run/rsync.lock secretsfile = /etc/rsync.pass motdfile = /etc/rsyncd.Motd [app_rsync_server] path = /szt comment = app_rsync_server uid = root gid = root port =873 use chroot = no read only = no list = no mac connections = 200 timeout = 600 auth users = rsync hosts allow = 172.16.3.89 hosts deny = 172.16.3.100,172.16.3.88 EOF #配置rsync同步帳號密碼 echo "rsync:123456" >/etc/rsync.pass echo "123456" >/etc/passwd.txt #賦權限並啓動 # chmod 600 /etc/passwd.txt # chmod 600 /etc/rsyncd.conf # chmod 600 /etc/rsync.pass # /etc/init.d/xinetd restart
#配置inotify-tools cat >>/etc/sysctl.conf<<EOF # inotify kernel config fs.inotify.max_queued_events = 99999999 fs.inotify.max_user_watches = 99999999 fs.inotify.max_user_instances = 65535 #sysctl -p 參數當即生效 # cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events} #檢查參數是否生效 65535 99999999 99999999
#實時同步腳本 #smbsvr1中: # cat /usr/local/inotify/rsync.sh #!/bin/bash # author ljohn # last uptime 2017.12.1 src_dir="/szt/" dst_dir="app_rsync_client" #目標目錄標識 exclude_dir="/usr/local/inotify/exclude.list" rsync_user="rsync" rsync_passwd="/etc/passwd.txt" dst_ip="172.16.3.90" #目標IP rsync_command(){ rsync -avH --port=873 --progress --delete --exclude-from=$exclude_dir $src_dir $rsync_user@$ip::$dst_dir --password-file=$rsync_passwd } for ip in $dst_ip;do rsync_command done /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $src_dir \ | while read file;do for ip in $dst_ip;do rsync_command echo "${file} was rsynced" >> /tmp/rsync.log 2>&1 done done
注意:
dst_dir="app_rsync_client" #目標目錄標識,在smbsvr2爲app_rsync_server
dst_ip="172.16.3.90" #目標IP,在smbsvr2中爲172.16.3.89
添加爲開機啓動 # cat >> /etc/rc.d/rc.local <<EOF nohup /bin/sh /usr/local/inotify/rsync.sh & EOF
一、關閉主節點heartbeat 服務,是否failover,啓動heartbeat 是否failback
[root@smbsvr1 ~]# /etc/init.d/heartbeat stop Stopping High-Availability services: Done. [root@smbsvr2 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:8C:61:EC inet addr:172.16.3.90 Bcast:172.16.3.255 Mask:255.255.255.0 inet6 addr: fd22:455a:117:0:250:56ff:fe8c:61ec/64 Scope:Global inet6 addr: fe80::250:56ff:fe8c:61ec/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:83388 errors:0 dropped:0 overruns:0 frame:0 TX packets:80369 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5447750 (5.1 MiB) TX bytes:6242625 (5.9 MiB) eth0:0 Link encap:Ethernet HWaddr 00:50:56:8C:61:EC inet addr:172.16.3.110 Bcast:172.16.3.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 [root@smbsvr1 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:8c:14:95 brd ff:ff:ff:ff:ff:ff inet 172.16.3.89/24 brd 172.16.3.255 scope global eth0 inet 172.16.3.110/24 brd 172.16.3.255 scope global secondary eth0:0 inet6 fd22:455a:117:0:250:56ff:fe8c:1495/64 scope global dynamic valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe8c:1495/64 scope link valid_lft forever preferred_lft forever [root@smbsvr1 ~]# /etc/init.d/heartbeat start Starting High-Availability services: INFO: Resource is stopped Done. [root@smbsvr1 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:8c:14:95 brd ff:ff:ff:ff:ff:ff inet 172.16.3.89/24 brd 172.16.3.255 scope global eth0 inet 172.16.3.110/24 brd 172.16.3.255 scope global secondary eth0:0 inet6 fd22:455a:117:0:250:56ff:fe8c:1495/64 scope global dynamic valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe8c:1495/64 scope link valid_lft forever preferred_lft forever [root@smbsvr1 ~]# /etc/init.d/smb status smbd (pid 2735) is running...
二、關閉主節點系統,重啓系統,是否failover;啓動主節點的系統,是否failback
重啓測試,這裏就不演示了,但本身必定要測試。 注意: 要被高可用的的服務必定不能開機啓動(這裏的samba服務) #chkconfig smb off
一、主節點建立文件
[root@smbsvr1 szt]# touch smbsvr1{1..10} [root@smbsvr1 szt]# ls smbsvr11 smbsvr110 smbsvr12 smbsvr13 smbsvr14 smbsvr15 smbsvr16 smbsvr17 smbsvr18 smbsvr19 [root@smbsvr2 szt]# ls smbsvr11 smbsvr110 smbsvr12 smbsvr13 smbsvr14 smbsvr15 smbsvr16 smbsvr17 smbsvr18 smbsvr19
二、備用節點建立文件
[root@smbsvr2 szt]# touch smbsvr2{1..10} [root@smbsvr2 szt]# ls smbsvr11 smbsvr12 smbsvr14 smbsvr16 smbsvr18 smbsvr21 smbsvr22 smbsvr24 smbsvr26 smbsvr28 smbsvr110 smbsvr13 smbsvr15 smbsvr17 smbsvr19 smbsvr210 smbsvr23 smbsvr25 smbsvr27 smbsvr29 [root@smbsvr1 szt]# ls smbsvr11 smbsvr12 smbsvr14 smbsvr16 smbsvr18 smbsvr21 smbsvr22 smbsvr24 smbsvr26 smbsvr28 smbsvr110 smbsvr13 smbsvr15 smbsvr17 smbsvr19 smbsvr210 smbsvr23 smbsvr25 smbsvr27 smbsvr29
三、 在客戶端建立文件測試
[root@smbsvr1 szt]# ls client1.txt.txt smbsvr12 smbsvr15 smbsvr18 smbsvr210 smbsvr24 smbsvr27 smbsvr11 smbsvr13 smbsvr16 smbsvr19 smbsvr22 smbsvr25 smbsvr28 smbsvr110 smbsvr14 smbsvr17 smbsvr21 smbsvr23 smbsvr26 smbsvr29 [root@smbsvr2 szt]# ls client1.txt.txt smbsvr12 smbsvr15 smbsvr18 smbsvr210 smbsvr24 smbsvr27 smbsvr11 smbsvr13 smbsvr16 smbsvr19 smbsvr22 smbsvr25 smbsvr28 smbsvr110 smbsvr14 smbsvr17 smbsvr21 smbsvr23 smbsvr26 smbsvr29
這裏要提供一個腳本:在測試時發現,若是有人或者意外關閉了samba服務
集羣不會Failover。
``` #cat /server/scripts/smb.sh #!/bin/bash #it's about to watch smb's status while : do i=`ps aux |grep smbd |grep -v "grep smbd" |wc -l` if [ $i = 0 ];then service heartbeat stop && exit 1 fi done #在smbsvr1中開機啓動(/etc/rc.d/rc.local),或者手動啓動。 ```
至此《heartbeat+rsync+inotify+samba》雙機集羣 部署完畢!!