#! /bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward ipv=/usr/sbin/ipvsadm vip=192.168.133.200 rs1=192.168.133.132 rs2=192.168.133.133 #注意這裏的網卡名字 ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev ens33:2 $ipv -C $ipv -A -t $vip:80 -s wrr $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
#/bin/bash vip=192.168.133.200 #把vip綁定在lo上,是爲了實現rs直接把結果返回給客戶端 ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip lo:0 #如下操做爲更改arp內核參數,目的是爲了讓rs順利發送mac地址給客戶端 #參考文檔www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html 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
在生產環境用中的比較多的是DR模式,NAT模式有瓶頸,節省公網IP,對小公司來講公網IP也是花錢的,若是是配置的多臺機器,每臺機器都去配置一個公網IP就是很浪費資源的狀況,並且當下公網IP愈來愈少;html
另外一種方案,搭建內部的lvs,所有都用內網,包括VIP也用內網,用一個公網IP作一個映射;公網的80端口映射到內網VIP的80端口,這樣能夠節省IPvim
[root@hanfeng ~]# vim /usr/local/sbin/lvs_dr.sh #! /bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward #打開端口轉發 ipv=/usr/sbin/ipvsadm #這是一個變量 vip=192.168.202.200 rs1=192.168.202.131 rs2=192.168.202.133 #注意這裏的網卡名字 ifdown ens37 ifup ens37 ifconfig ens37:2 $vip broadcast $vip netmask 255.255.255.255 up #//給一個網卡設置一個虛擬網卡 route add -host $vip dev ens37:2 $ipv -C $ipv -A -t $vip:80 -s wrr $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 #這裏-g 指定DR模式 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1 保存退出
[root@hanfeng ~]# sh /usr/local/sbin/lvs_dr.sh Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8) [root@hanfeng ~]#
B機器 [root@hf-02 ~]# vi /usr/local/sbin/lvs_rs.sh #/bin/bash vip=192.168.202.200 #把vip綁定在lo上,是爲了實現rs直接把結果返回給客戶端 ifdown lo ifup lo ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip lo:0 #如下操做爲更改arp內核參數,目的是爲了讓rs順利發送mac地址給客戶端 #參考文檔www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html 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 保存退出 C機器同上
腳本定義/lo/arp_ignore和/all/arp_ignore的理解:瀏覽器
腳本定義/lo/arp_announce和/all/arp_announce的理解緩存
sh /usr/local/sbin/lvs_rs.sh
B機器 [root@hf-02 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.202.2 0.0.0.0 UG 100 0 0 ens32 192.168.202.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32 192.168.202.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo [root@hf-02 ~]# C機器同上
B機器 [root@hf-02 ~]# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 inet 192.168.202.200/32 brd 192.168.202.200 scope global lo:0 valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e3:68:95 brd ff:ff:ff:ff:ff:ff inet 192.168.202.133/24 brd 192.168.202.255 scope global ens32 valid_lft forever preferred_lft forever inet6 fe80::babb:1944:3d05:ff65/64 scope link valid_lft forever preferred_lft forever [root@hf-02 ~]# C機器同上
A機器 [root@hanfeng ~]# ip add 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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ff:45:8f brd ff:ff:ff:ff:ff:ff inet 192.168.202.130/24 brd 192.168.202.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.202.150/24 brd 192.168.202.255 scope global secondary eno16777736:0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:feff:458f/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ff:45:99 brd ff:ff:ff:ff:ff:ff inet 192.168.80.100/24 brd 192.168.80.255 scope global ens37 valid_lft forever preferred_lft forever inet 192.168.202.200/32 brd 192.168.202.200 scope global ens37:2 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:feff:4599/64 scope link valid_lft forever preferred_lft forever [root@hanfeng ~]#
A機器 [root@hanfeng ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.202.200:80 wrr -> 192.168.202.131:80 Route 1 0 1 -> 192.168.202.133:80 Route 1 1 0 [root@hanfeng ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.202.200:80 wrr -> 192.168.202.131:80 Route 1 1 0 -> 192.168.202.133:80 Route 1 2 0 [root@hanfeng ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.202.200:80 wrr -> 192.168.202.131:80 Route 1 1 1 -> 192.168.202.133:80 Route 1 0 2 [root@hanfeng ~]#