虛擬機系統:Ubuntu 12.04算法
hostname | ip | |
---|---|---|
LVSMaster | eth0:192.168.31.198 eth1:10.10.100.100 |
|
LVSBackup | eth0:192.168.31.197 eth1:10.10.100.101 |
LVS 備機 |
RealserverNet1 | eth0:10.10.100.53 | 真實機器 lvs net模式 |
RealserverNet2 | eth0:10.10.100.54 | 真實機器 lvs net模式 |
RealserverDR1 | eth0:192.168.31.186 | 真實機器 lvs dr模式 |
DR模式須要的VIP: 192.168.31.200
NET模式須要的VIP: 10.10.100.200vim
參照lvs 筆記服務器
LVSMaster和LVSBackup安裝keepalived和lvsmarkdown
apt-get install keepalived apt-get install ipvsadm
vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVSMaster } vrrp_instance VI_1 { state MASTER interface eth0 # 對外服務的網卡 virtual_router_id 100 #VRRP組名,兩個節點的設置必須同樣,以指明各個節點屬於同一VRRP組 priority 100 #主節點的優先級(1-254之間),備用節點必須比主節點優先級低 advert_int 1 #設置主備之間同步檢查的時間間隔單位秒 authentication { #設置驗證信息,兩個節點必須一致 auth_type PASS auth_pass 123456 } virtual_ipaddress { #指定虛擬IP, 兩個節點設置必須同樣 192.168.31.200 } } #lvs NAT配置 virtual_server 192.168.31.200 80 { delay_loop 6 lb_algo wrr #設置負載高度算法,wrr帶權循環 lb_kind NAT #設置LVS實現負載均衡的機制,能夠爲{NAT|TUN|DR}三種 #persistence_timeout 50 #會話保持時間,單位爲秒。動態網頁session共享簡單的決解辦法。測試的時候注掉,否則看不出效果 protocol TCP #指定轉發協議類型能夠設置{TCP|UDP}兩種 real_server 10.10.100.53 80 { #真實的主機 weight 10 #真實的主機的權重,數字越大,權重越高 TCP_CHECK { #設置檢測Realserver的方式 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.10.100.54 80 { weight 10 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } #lvs DR配置 virtual_server 192.168.31.200 80 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.31.186 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
scp /etc/keepalived/keepalived.conf 10.10.100.101:/etc/keepalived/keepalived.conf vim /etc/keepalived/keepalived.conf
只用修改
state BACKUP
priority 90session
! Configuration File for keepalived global_defs { router_id LVSMaster } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 100 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.31.200 } } virtual_server 192.168.31.200 80 { delay_loop 6 lb_algo wrr lb_kind NAT #persistence_timeout 50 protocol TCP real_server 10.10.100.53 80 { weight 10 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.10.100.54 80 { weight 10 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } virtual_server 192.168.31.200 80 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.31.186 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
master 和backup 啓動 keepalived負載均衡
service keepalived start
root@LVSMaster:~# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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 valid_lft forever preferred_lft forever 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:0c:29:2b:e9:71 brd ff:ff:ff:ff:ff:ff inet 192.168.31.198/24 brd 192.168.31.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.31.200/32 scope global eth0 #如今序列IP在LVSMaster上 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe2b:e971/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:2b:e9:7b brd ff:ff:ff:ff:ff:ff inet 10.10.100.100/24 brd 10.10.100.255 scope global eth1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe2b:e97b/64 scope link valid_lft forever preferred_lft forever
訪問 http://192.168.31.200/ oop
模擬master出問題測試
root@LVSMaster:~# service keepalived stop #vip已經沒了 root@LVSMaster:~# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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 valid_lft forever preferred_lft forever 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:0c:29:2b:e9:71 brd ff:ff:ff:ff:ff:ff inet 192.168.31.198/24 brd 192.168.31.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe2b:e971/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:2b:e9:7b brd ff:ff:ff:ff:ff:ff inet 10.10.100.100/24 brd 10.10.100.255 scope global eth1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe2b:e97b/64 scope link valid_lft forever preferred_lft forever #vip在LVSBackup上 root@LVSBackup:~# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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 valid_lft forever preferred_lft forever 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:0c:29:7f:11:bd brd ff:ff:ff:ff:ff:ff inet 192.168.31.197/24 brd 192.168.31.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.31.200/32 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe7f:11bd/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:7f:11:c7 brd ff:ff:ff:ff:ff:ff inet 10.10.100.101/24 brd 10.10.100.255 scope global eth1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe7f:11c7/64 scope link valid_lft forever preferred_lft forever