23123 发表于 2016-4-7 10:01:12

LVS专题: NAT和DR模型实现Web负载均衡

LVS专题: NAT和DR模型实现Web负载均衡

[*]
前言:在上篇文章中我们讲了一些LVS的基本概念和相应模型的实验原理和流程,本篇文章我们主要使用lvs为web服务提供负载均衡NAT实现实验拓扑实验环境


主机IP地址功用
Director.anyisalin.com172.16.1.2,172.16.2.2LVS-Director
rs1.anyisalin.com172.16.2.3Real Server
rs2.anyisalin.com172.16.2.3Real Server

注意: 本文实验中所有主机SElinux和iptables都是关闭的Real Server配置

# yum install httpd -y &> /dev/null && echo success || echo failure    #RS1安装httpd
success #安装成功
# route add default gw 172.16.2.2#设置默认网关为Director的DIP
# echo "<h1>This is Real Server 1 </h1>" > /var/www/html/index.html    #添加网页
# service httpd start &> /dev/null && echo success #启动httpd服务
success #启动成功

##以下操作在rs2上执行

# yum install httpd -y &> /dev/null && echo success || echo failure    #RS1安装httpd
success #安装成功
# route add default gw 172.16.2.2#设置默认网关为Director的DIP
# echo "<h1>This is Real Server 2 </h1>" > /var/www/html/index.html    #添加网页
# service httpd start &> /dev/null && echo success #启动httpd服务
success #启动成功
Director配置

Director配置

IP地址配置的过程就不写了

# curl 172.16.2.3 #可以访问RS1
<h1>This is Real Server 1 </h1>
# curl 172.16.2.4 #可以访问RS2
<h1>This is Real Server 2 </h1>
# cat /proc/sys/net/ipv4/ip_forward   #查看内核核心转发是否开启
0   #没有开启
# echo 0 > /proc/sys/net/ipv4/ip_forward#开启路由转发, 若要永久修改自行配置配置文件

##添加ipvs规则, 这里为了直观, 所以选择了Round Robin的调度方法
# ipvsadm -A -t 172.16.1.2:80 -s rr
# ipvsadm -a -t 172.16.1.2:80 -r 172.16.2.3 -m
# ipvsadm -a -t 172.16.1.2:80 -r 172.16.2.4 -m

# ipvsadm -L -n   #查看ipvs规则的信息
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP172.16.1.2:80 rr
-> 172.16.2.3:80                Masq    1      0          0         
-> 172.16.2.4:80                Masq    1      0          0         
#
DR实现实验拓扑实验环境


主机IP地址功用
director.anyisalin.com172.16.2.2,172.16.2.5lvs-director
rs1.anyisalin.com172.16.2.3,172.16.2.5lvs-rs
rs.anyisalin.com172.16.2.4,172.16.2.5lvs-rs
注意: 本文实验中所有主机SElinux和iptables都是关闭的实验步骤由于LVS-NAT模式较为复杂,所以配置较为麻烦, 如果对LVS-DR模式还不是很理解的可以看我上一篇博客Director配置# ifconfig eth1:0 172.16.2.5/32 broadcast 172.16.2.5 up   #配置VIP地址
# route add -host 172.16.2.5 dev eth1:0   #添加一条路由避免地址冲突
# ipvsadm -A -t 172.16.2.5:80 -s rr
# ipvsadm -a -t 172.16.2.5:80 -r 172.16.2.3 -g
# ipvsadm -a -t 172.16.2.5:80 -r 172.16.2.4 -g
Real Server配置##修改内核参数,若要永久生效请修改配置文件
# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
# ifconfig lo:0 172.16.2.5/32 broadcast 172.16.2.5 up#配置VIP到lo:0
# route add -host 172.16.2.5 dev lo:0
# yum install httpd -y &> /dev/null && echo success || echo failure    #RS1安装httpd
success #安装成功
# route add default gw 172.16.2.2#设置默认网关为Director的DIP
# echo "This is Real Server 2 " > /var/www/html/index.html    #添加网页
# service httpd start &> /dev/null && echo success #启动httpd服务
success #启动成功

##以下操作在rs2中执行
# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
# ifconfig lo:0 172.16.2.5/32 broadcast 172.16.2.5 up#配置VIP到lo:0
# route add -host 172.16.2.5 dev lo:0
# yum install httpd -y &> /dev/null && echo success || echo failure    #RS1安装httpd
success #安装成功
# route add default gw 172.16.2.2#设置默认网关为Director的DIP
# echo "This is Real Server 2 " > /var/www/html/index.html    #添加网页
# service httpd start &> /dev/null && echo success #启动httpd服务
success #启动成功

测试
页: [1]
查看完整版本: LVS专题: NAT和DR模型实现Web负载均衡