Linux實現Ftp服務雙機熱備
系統:opensuse 11.3 32位
熱備服務軟件:
LVS:
ipvsadm-1.26-3.1.i586.rpm
KeepAlived:
keepalived-1.2.2.tar.gz
www.2cto.com
安裝步驟:
一,
lvs采用rpm包安裝比較方便
#rpm -ivh ipvsadm-1.26-3.1.i586.rpm
二,
keepalived使用源碼安裝
#tar -zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure --prefix=/usr/local/keepalived
#make && make install
配置
編輯/etc/keepalived/keepalived.conf
#===================================配置開始====================================
! Configuration File for keepalived
#全局配置
global_defs {
notification_email {
[email protected]
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#VRRP配置
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.80
}
}
virtual_server 192.168.1.80 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.1.81 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.82 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
#===================================配置結束====================================
1,啟動lvs
#ipvsadm
查看是否啟動成功,
#lsmod |grep ip_v
ip_vs_rr 1122 1
ip_vs 106759 3 ip_vs_rr
libcrc32c 971 1 ip_vs
2,啟動keepalive
#keepalived start
查看是否啟動成功,
#ps -ef|grep keepalived
root 3242 1 0 23:35 ? 00:00:00 keepalived start
root 3243 3242 0 23:35 ? 00:00:00 keepalived start
root 3244 3242 0 23:35 ? 00:00:00 keepalived start
root 3280 3107 0 23:41 pts/0 00:00:00 grep keepalived
3,查看熱備情況,
#ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.80:http rr persistent 50
#查看熱備運行日志
#tail -f /var/log/message
Mar 12 23:35:24 linux-e1j0 avahi-daemon[2016]: Registering new address record for 192.168.1.80 on eth1.IPv4.
Mar 12 23:35:26 linux-e1j0 Keepalived_healthcheckers: TCP connection to [192.168.1.81]:80 failed !!!
Mar 12 23:35:26 linux-e1j0 Keepalived_healthcheckers: Removing service [192.168.1.81]:80 from VS [192.168.1.80]:80
Mar 12 23:35:26 linux-e1j0 Keepalived_healthcheckers: Remote SMTP server [127.0.0.1:25] connected.
4,查看虛擬地址是否正常加載到網卡上。
#ip a
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:0c:29:cb:c8:7d brd ff:ff:ff:ff:ff:ff
inet 192.168.1.71/24 brd 192.168.1.255 scope global eth1
inet 192.168.1.80/32 scope global eth1
inet6 fe80::20c:29ff:fecb:c87d/64 scope link
valid_lft forever preferred_lft forever
可以看到 192.168.1.80/32 虛擬地址正常加載。
測試熱備服務:
開啟日志監控
#tail -f /var/log/message
關掉主服務
#killall -9 keepalived
可以看到備用服務日志輸出:
Mar 13 23:00:27 linux-backup Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 13 23:00:28 linux-backup Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 13 23:00:28 linux-backup avahi-daemon[2053]: Registering new address record for 192.168.1.80 on eth2.IPv4.
Mar 13 23:00:28 linux-backup avahi-daemon[2053]: Withdrawing address record for fe80::20c:29ff:fec4:da16 on eth2.
Mar 13 23:00:28 linux-backup avahi-daemon[2053]: Withdrawing address record for 192.168.1.72 on eth2.
開啟主服務器服務
#keepalived start
可以看到備用服務日志輸出:
Mar 13 23:01:00 linux-backup Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Mar 13 23:01:00 linux-backup Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 13 23:01:00 linux-backup avahi-daemon[2053]: Server startup complete. Host name is linux-backup-89.local. Local service cookie is 2700730627.
Mar 13 23:01:00 linux-backup avahi-daemon[2053]: Withdrawing address record for 192.168.1.80 on eth2.
這只是能看到服務正常切換了。真正測試雙備服務的方法是,在主備服務上同時開啟ftp服務。
#service vsftpd start
然後用第三方電腦訪問虛擬地址 ftp 192.168.1.80,
可以看到當主備服務切換時,ftp客戶端會分別登錄到不同的服務器上(71或72),證明ftp服務雙機熱備正常搭建。