LVS專題: NAT和DR模型實現Web負載均衡
前言:
在上篇文章中我們講了一些LVS的基本概念和相應模型的實驗原理和流程,本篇文章我們主要使用
lvs
為web服務提供負載均衡
NAT實現
實驗拓撲
實驗環境
主機 | IP地址 | 功用 |
---|---|---|
Director.anyisalin.com | 172.16.1.2,172.16.2.2 | LVS-Director |
rs1.anyisalin.com | 172.16.2.3 | Real Server |
rs2.anyisalin.com | 172.16.2.3 | Real Server |
注意: 本文實驗中所有主機SElinux和iptables都是關閉的
實驗步驟
Real Server配置
[root@rs1 ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS1安裝httpd
success #安裝成功
[root@rs1 ~]# route add default gw 172.16.2.2 #設置默認網關為Director的DIP
[root@rs1 ~]# echo "<h1>This is Real Server 1 </h1>" > /var/www/html/index.html #添加網頁
[root@rs1 ~]# service httpd start &> /dev/null && echo success #啟動httpd服務
success #啟動成功
##以下操作在rs2上執行
[root@rs2 ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS1安裝httpd
success #安裝成功
[root@rs2 ~]# route add default gw 172.16.2.2 #設置默認網關為Director的DIP
[root@rs2 ~]# echo "<h1>This is Real Server 2 </h1>" > /var/www/html/index.html #添加網頁
[root@rs2 ~]# service httpd start &> /dev/null && echo success #啟動httpd服務
success #啟動成功
Director配置
IP地址配置的過程就不寫了
[root@director ~]# curl 172.16.2.3 #可以訪問RS1
<h1>This is Real Server 1 </h1>
[root@director ~]# curl 172.16.2.4 #可以訪問RS2
<h1>This is Real Server 2 </h1>
[root@director ~]# cat /proc/sys/net/ipv4/ip_forward #查看內核核心轉發是否開啟
0 #沒有開啟
[root@director ~]# echo 0 > /proc/sys/net/ipv4/ip_forward #開啟路由轉發, 若要永久修改自行配置配置文件
##添加ipvs規則, 這里為了直觀, 所以選擇了Round Robin的調度方法
[root@director ~]# ipvsadm -A -t 172.16.1.2:80 -s rr
[root@director ~]# ipvsadm -a -t 172.16.1.2:80 -r 172.16.2.3 -m
[root@director ~]# ipvsadm -a -t 172.16.1.2:80 -r 172.16.2.4 -m
[root@director ~]# ipvsadm -L -n #查看ipvs規則的信息
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.1.2:80 rr
-> 172.16.2.3:80 Masq 1 0 0
-> 172.16.2.4:80 Masq 1 0 0
[root@director ~]#
測試
DR實現
實驗拓撲
實驗環境
主機 | IP地址 | 功用 |
---|---|---|
director.anyisalin.com | 172.16.2.2,172.16.2.5 | lvs-director |
rs1.anyisalin.com | 172.16.2.3,172.16.2.5 | lvs-rs |
rs.anyisalin.com | 172.16.2.4,172.16.2.5 | lvs-rs |
注意: 本文實驗中所有主機SElinux和iptables都是關閉的
實驗步驟
由于LVS-NAT模式較為復雜,所以配置較為麻煩, 如果對LVS-DR模式還不是很理解的可以看我上一篇博客
Director配置
[root@director ~]# ifconfig eth1:0 172.16.2.5/32 broadcast 172.16.2.5 up #配置VIP地址
[root@director ~]# route add -host 172.16.2.5 dev eth1:0 #添加一條路由避免地址沖突
[root@director ~]# ipvsadm -A -t 172.16.2.5:80 -s rr
[root@director ~]# ipvsadm -a -t 172.16.2.5:80 -r 172.16.2.3 -g
[root@director ~]# ipvsadm -a -t 172.16.2.5:80 -r 172.16.2.4 -g
Real Server配置
##修改內核參數,若要永久生效請修改配置文件
[root@rs1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rs1 ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
[root@rs1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@rs1 ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
[root@rs1 ~]# ifconfig lo:0 172.16.2.5/32 broadcast 172.16.2.5 up #配置VIP到lo:0
[root@rs1 ~]# route add -host 172.16.2.5 dev lo:0
[root@rs1 ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS1安裝httpd
success #安裝成功
[root@rs1 ~]# echo "<h1>This is Real Server 2 </h1>" > /var/www/html/index.html #添加網頁
[root@rs1 ~]# service httpd start &> /dev/null && echo success #啟動httpd服務
success #啟動成功
##以下操作在rs2中執行
[root@rs2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rs2 ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
[root@rs2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@rs2 ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
[root@rs2 ~]# ifconfig lo:0 172.16.2.5/32 broadcast 172.16.2.5 up #配置VIP到lo:0
[root@rs2 ~]# route add -host 172.16.2.5 dev lo:0
[root@rs2 ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS1安裝httpd
success #安裝成功
[root@rs2 ~]# echo "<h1>This is Real Server 2 </h1>" > /var/www/html/index.html #添加網頁
[root@rs2 ~]# service httpd start &> /dev/null && echo success #啟動httpd服務
success #啟動成功
測試
總結:
本文介紹了如何使用lvs來對多臺服務器進行負載均衡, 但是還是有多情況是本文沒有介紹的, 例如TUN, FULLNAT等, 以后有機會會和大家分享, 過兩天可能會寫使用keepalive+lvs實現director的高可用, 敬請期待
作者:AnyISalIn QQ:1449472454
感謝:MageEdu
原創文章,作者:Net18-AnyISalIn,如若轉載,請注明出處:http://www.www58058.com/14212