簡介mysql
LVS是linux virtual server的簡寫linux虛擬服務器,是一個虛擬的服務器集羣系統,能夠再unix/linux平臺下實現負載均衡集羣功能。linux
DR模式(直接路由模式)nginx
Virtual server via direct routing (vs/dr)算法
DR模式是經過改寫請求報文的目標MAC地址,將請求發給真實服務器的,而真實服務器響應後的處理結果直接返回給客戶端用戶。同TUN模式同樣,DR模式能夠極大的提升集羣系統的伸縮性。並且DR模式沒有IP隧道的開銷,對集羣中的真實服務器也沒有必要必須支持IP隧道協議的要求。可是要求調度器LB與真實服務器RS都有一塊網卡鏈接到同一物理網段上,必須在同一個局域網環境。sql
DR模式是互聯網使用比較多的一種模式。vim
DR模式原理圖:bash
LVS調度算法的生產環境選型:服務器
1、通常的網絡服務,如http,mail,mysql等經常使用的LVS調度算法爲:網絡
a.基本輪詢調度rr負載均衡
b.加權最小鏈接調度wlc
c.加權輪詢調度wrc
DR配置
Director server | chumjtest01 |
eth0 172.31.24.199 | VIP:172.31.24.197 |
Realserver01 | chumjtest02 | eth0 172.31.24.200 | VIP lo:0:172.31.24.197 |
Realserver02 | chumjtest03 | eth0 172.31.24.198 | VIP lo:0:172.31.24.197 |
實驗準備
1.關閉防火牆 iptables -F;
2.關閉selinux: setenforce 0
3.在Director Server安裝好lvs
4.realserver都須要安裝nginx
Dirserver配置
首先爲了執行方便編寫一個腳本
#!/bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward //開啓轉發功能 ipv=/sbin/ipvsadm vip=172.31.24.197 //VIP的IP地址 rs1=172.31.24.200 //RS1的IP地址 rs2=172.31.24.198 //RS2的IP地址 ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev eth0:0 $ipv -C $ipv -A -t $vip:80 -s rr //輪詢,意思是一次轉發, $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 //g表示LVS-DR模式 w表示權重 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
Realserver 配置
一樣創建一個腳本,分別在放在兩臺RS上面。
#!/bin/bash vip=172.31.24.197 ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
而後director上執行: bash /usr/local/sbin/lvs_dr.sh
兩臺rs上執行: bash /usr/local/sbin/lvs_dr_rs.sh
測試
[root@chumjtest04 ~]# curl 172.31.24.197 Chumjtest03
注:
主要realserver 權重不同要修改dirserver配置文件
#!/bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward //開啓轉發功能 ipv=/sbin/ipvsadm vip=172.31.24.197 //VIP的IP地址 rs1=172.31.24.200 //RS1的IP地址 rs2=172.31.24.198 //RS2的IP地址 ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev eth0:0 $ipv -C $ipv -A -t $vip:80 -s wrr //加權輪詢,依照優先級轉發 $ipv -a -t $vip:80 -r $rs1:80 -g -w 3 //w表示權重,修改權重 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
Keepalived安裝
若是由以前的配置要清理下
ipvsadm -C
ifconfig eth0:0 down
因爲環境限制這裏將RS02當作keepalived備機,在真是環境中不建議這麼作。最好另外有一臺設備當作備機
進入keepalived,對配置文件進行配置
[root@chumjtest01 sbin]# cat /etc/keepalived/keepalived.conf vrrp_instance VI_1 { state MASTER #備用服務器上爲 BACKUP interface eth0 virtual_router_id 51 priority 100 #備用服務器上爲90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.31.110 } } virtual_server 192.168.31.110 80 { delay_loop 6 #(每隔10秒查詢realserver狀態) lb_algo wlc #(lvs 算法) lb_kind DR #(Direct Route) persistence_timeout 60 #(同一IP的鏈接60秒內被分配到同一臺realserver) protocol TCP #(用TCP協議檢查realserver狀態) real_server 192.168.31.100 80 { weight 100 #(權重) TCP_CHECK { connect_timeout 10 #(10秒無響應超時) nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.31.101 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
將主機的keepalived配置文件拷貝到備機上
只須要修改
state MASTER -> state BACKUP
priority 100 -> priority 90
[root@chumjtest01 sbin]# scp keepalived.conf root@172.31.24.200:/etc/keepalived 進入chumjtest02 [root@chumjtest02 keepalived]# vim keepalived.conf vrrp_instance VI_1 { state BACKUP #備用服務器上爲 BACKUP interface eth0 virtual_router_id 51 priority 90 #備用服務器上爲90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.31.24.197 } } virtual_server 172.31.24.197 80 { delay_loop 6 #(每隔10秒查詢realserver狀態) lb_algo wlc #(lvs 算法) lb_kind DR #(Direct Route) persistence_timeout 60 #(同一IP的鏈接60秒內被分配到同一臺realserver) protocol TCP #(用TCP協議檢查realserver狀態) real_server 172.31.24.200 80 { weight 100 #(權重) TCP_CHECK { connect_timeout 10 #(10秒無響應超時) nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 172.31.24.198 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
注:
1.配置完keepalived後須要開啓端口轉發(主從都須要)
2.RS上都須要執行lvs_dr_rs.sh腳本
3.兩個director上啓動keepalived服務(先主後從)
啓動keepalived服務會自動生成vip和ipvsadm規則,不須要再去執行上面提到的/usr/local/sbin/lvs_dr.sh 腳本。
啓動keepalived
[root@chumjtest01 ~]# service keepalived restart Stopping keepalived: [ OK ] Starting keepalived: [ OK ] [root@chumjtest01 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.31.24.197:80 wlc persistent 60 -> 172.31.24.198:80 Route 100 0 0 -> 172.31.24.200:80 Route 100 0 0 [root@chumjtest01 sbin]# ip addr 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 mq state UP qlen 1000 link/ether 00:50:56:b3:79:e3 brd ff:ff:ff:ff:ff:ff inet 172.31.24.199/24 brd 172.31.24.255 scope global eth0 inet 172.31.24.197/32 scope global eth0 //VIP已經啓動 inet6 fe80::250:56ff:feb3:79e3/64 scope link valid_lft forever preferred_lft forever
測試
能夠將keepalived的master服務中止,看下在備機上是否啓動。
[root@chumjtest01 sbin]# service keepalived stop Stopping keepalived: [ OK ] [root@chumjtest01 sbin]#
從備機的日誌上看到VIP已經在備機上啓動
[root@chumjtest02 sbin]# tail -f /var/log/messages Jan 11 17:21:00 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 11 17:21:01 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 11 17:21:01 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) setting protocol VIPs. Jan 11 17:21:01 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.31.24.197 Jan 11 17:21:01 chumjtest02 Keepalived_healthcheckers[1997]: Netlink reflector reports IP 172.31.24.197 added Jan 11 17:21:06 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.31.24.197
[root@chumjtest02 sbin]# ip addr 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 inet 172.31.24.197/32 brd 172.31.24.197 scope global lo:0 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:50:56:b3:0e:c3 brd ff:ff:ff:ff:ff:ff 3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:b3:68:a0 brd ff:ff:ff:ff:ff:ff inet 172.31.24.200/24 brd 172.31.24.255 scope global eth0 inet 172.31.24.197/32 scope global eth0 //VIP已經在備機上啓動 inet6 fe80::250:56ff:feb3:68a0/64 scope link valid_lft forever preferred_lft forever
注:
keepalived的日誌在/var/log/messages,能夠經過檢查日誌來發現問題。