配置過程如下,
假設有兩臺主機作爲集羣的節點,在兩臺主機上如下安裝好keepalived
如果系統中有心跳服務需要先將其關閉,
/etc/init.d/heartbeat stop
chkconfig heartbeat off
然後下載好keepalived的壓縮包(操作中使用的是keepalived-1.3.5.tar.gz),然後進行壓縮解壓縮
tar zxf keepalived-1.3.5.tar.gz
cd keepalived-1.3.5
如果出現版本錯誤或者需要重新安裝的情況,那麼 rm -fr keepalived/、make clean(cd keepalived-1.3.5)
隨後進入解壓後生成的目錄,開始源碼安裝
./configure --prefix=/usr/local/keepalived --with-init=SYSV
make && make install
製作軟鏈接使keepalived命令可以直接在shell中執行
ln -s /usr/local/keeplived/sbin/keepalived /sbin/
ln -s /usr/local/keeplived/etc/keepalived/ /etc/
ln -s /usr/local/keeplived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keeplived/etc/rc.d/init.d/keepalived /etc/init.d/
進入keepalived的目錄下,修改配置文件
cd /etc/keepalived/
vim keepalived.conf
修改結果如下
global_defs {
notification_email {
root@localhost
}
notification_email_from keeplived@server1
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 39
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.39.100
}
}
virtual_server 172.25.39.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 50
protocol TCP
real_server 172.25.39.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.25.39.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
然後將配置好的文件發送給另一個節點
scp -r keepalived/ server4:/usr/local/
同時開啓兩個主機的keepalived服務
/etc/init.d/keepalived start
測試
ip addr
curl 172.25.39.10
ipvsadm -L
注意::iptables -L curl 172.25.40.100 #虛擬服務器ip vip
iptables -F #將火牆策略清除乾淨
在keepalived上面添加ftp協議
關閉ldrirectord服務
/etc/init.d/ldirectord stop
chkconfig ldirectord off
開啓keepalived服務
/etc/init.d/keepalived start
在配置文件中添加ftp協議
vim keepalived.conf
修改如下
57 virtual_server 172.25.39.100 21 {(ftp的端口是21)
58 delay_loop 6
59 lb_algo rr
60 lb_kind DR
61 persistence_timeout 50(開啓後持續連接50s,此期間不會被負載均衡)
62 protocol TCP
63 (爲什麼不會被負載均衡?原因如下:
ftp有兩個端口20和21,在協議磋商的時候開啓20,持續連接。如果關閉此項,那麼一直轉換,始終在協議磋商,不能連接)
64 real_server 172.25.39.2 21 {
65 weight 1
66 TCP_CHECK {
67 connect_timeout 3 68 nb_get_retry 3
69 delay_before_retry 3
70 }
71 }
72
73 real_server 172.25.39.3 21 {
74 weight 1
75 TCP_CHECK {
76 connect_timeout 3
77 nb_get_retry 3
78 delay_before_retry 3
79 }
80 }
81 }
讓服務重新讀取配置文件
/etc/init.d/keepalived reload
在負載均衡主機上安裝並開啓vsftpd服務
yum install -y vsftpd
/etc/init.d/vsftpd start
.在主節點上查看策略是否生效
ipvsadm -L
結果爲
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.39.100:ftp rr persistent 50
TCP 172.25.39.100:http rr
-> server2:http Route 1 0 0
-> server3:http Route 1 0 0