LVS默認模式,應用最普遍,經過爲請求報文從新封裝一個MAC首部進行轉發源MAC是DIP所在的接口的MAC,目標MAC是某挑選出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目標IP/PORT均保持不變
(1)Director和各RS都配置有VIP
(2)確保前端路由將目標IP爲VIP的請求報文發往Director
(3)RS的RIP能夠使用私網地址,也能夠是公網地址;RIP與DIP在同一IP網絡,RIP的網關不能只想DIP,以確保響應報文不會經由Director
(4)RS和Director要在同一個物理網絡
(5)請求報文要經由Director,但響應報文不經由Director,而有RS直接發往Cliebt
(6)不支持端口映射(端口不能修改)
(7)RS可以使用大多數OS系統html
網卡橋接模式
ens37網關指向路由的網關:172.22.142.159前端
route add -net 0.0.0.0/0 gw 172.22.142.159
須開啓路由轉發功能:web
vim /etc/sysctl.conf net.ipv4.ip_forward=1
ens33僅主機模式兩個網址192.168.30.46/2四、10.0.0.200/24 vim
ifconfig ens33:1 10.0.0.200 netmask 255.255.255.0
ens37橋接模式 172.22.142.159/16bash
route add -net 10.0.0.0/24 dev ens33
ens33僅主機模式 192.168.30.16/24,網關指向任意ip
配置步驟寫爲腳本lvs_dr_vs.sh服務器
#!/bin/bash vip=10.0.0.100 iface='ens33:1' #將VIP加在ens33網卡上 mask='255.255.255.0' port='80' #指定服務端口 rs1='192.168.30.26' #指明DIP rs2='192.168.30.36' #指明DIP scheduler='wrr' #訪問模式爲加權輪詢 type='-g' #工做模式-g表示dr模式 rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null #裝ipvsadm包 case $1 in start) ifconfig $iface $vip netmask $mask #broadcast $vip up #添加VIP的ip iptables -F #清空防火牆規則 ipvsadm -A -t ${vip}:${port} -s $scheduler #添加集羣服務 ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1 #在集羣中加入受控主機1 ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1 #在集羣中加入受控主機2 echo "The VS Server is Ready!" ;; stop) #中止集羣服務並清空規則 ipvsadm -C ifconfig $iface down echo "The VS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac
查看ipvsadm規則網絡
ipvsadm -Ln
ens33僅主機模式 192.168.30.26
網關gateway指向路由:192.168.30.46
配置步驟寫爲腳本lvs_dr_rs.sh架構
#!/bin/bash vip=10.0.0.100 mask='255.255.255.255' dev=lo:1 rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null #安裝httpd包 service httpd start &> /dev/null && echo "The httpd Server is Ready!" echo "<h1>this is real server1</h1>" > /var/www/html/index.html case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore #僅在請求的目標IP配置在本地主機的接收到請求報文的接口上時,纔給予響應 echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce #必須避免將接口信息向非本網絡進行通告 echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #添加VIP的IP #route add -host $vip dev $dev echo "The RS Server is Ready!" ;; stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac
ens33僅主機模式 192.168.30.36
網關gateway指向路由:192.168.30.46
執行腳本 bash lvs_dr_rs.sh startcurl
#!/bin/bash vip=10.0.0.100 mask='255.255.255.255' dev=lo:1 rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null service httpd start &> /dev/null && echo "The httpd Server is Ready!" echo "<h1>this is real server2</h1>" > /var/www/html/index.html case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #route add -host $vip dev $dev echo "The RS Server is Ready!" ;; stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac
curl 10.0.0.100
ide