前言:
在上篇文章中我们讲了一些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配置
[[email protected] ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS1安装httpd success #安装成功 [[email protected] ~]# route add default gw 172.16.2.2 #设置默认网关为Director的DIP [[email protected] ~]# echo "<h1>This is Real Server 1 </h1>" > /var/www/html/index.html #添加网页 [[email protected] ~]# service httpd start &> /dev/null && echo success #启动httpd服务 success #启动成功 ##以下操作在rs2上执行 [[email protected] ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS1安装httpd success #安装成功 [[email protected] ~]# route add default gw 172.16.2.2 #设置默认网关为Director的DIP [[email protected] ~]# echo "<h1>This is Real Server 2 </h1>" > /var/www/html/index.html #添加网页 [[email protected] ~]# service httpd start &> /dev/null && echo success #启动httpd服务 success #启动成功 Director配置
Director配置
IP地址配置的过程就不写了
[[email protected] ~]# curl 172.16.2.3 #可以访问RS1 <h1>This is Real Server 1 </h1> [[email protected] ~]# curl 172.16.2.4 #可以访问RS2 <h1>This is Real Server 2 </h1> [[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward #查看内核核心转发是否开启 0 #没有开启 [[email protected] ~]# echo 0 > /proc/sys/net/ipv4/ip_forward #开启路由转发, 若要永久修改自行配置配置文件 ##添加ipvs规则, 这里为了直观, 所以选择了Round Robin的调度方法 [[email protected] ~]# ipvsadm -A -t 172.16.1.2:80 -s rr [[email protected] ~]# ipvsadm -a -t 172.16.1.2:80 -r 172.16.2.3 -m [[email protected] ~]# ipvsadm -a -t 172.16.1.2:80 -r 172.16.2.4 -m [[email protected] ~]# 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 [[email protected] ~]#
测试
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配置
[[email protected] ~]# ifconfig eth1:0 172.16.2.5/32 broadcast 172.16.2.5 up #配置VIP地址[[email protected] ~]# route add -host 172.16.2.5 dev eth1:0 #添加一条路由避免地址冲突[[email protected] ~]# ipvsadm -A -t 172.16.2.5:80 -s rr[[email protected] ~]# ipvsadm -a -t 172.16.2.5:80 -r 172.16.2.3 -g[[email protected] ~]# ipvsadm -a -t 172.16.2.5:80 -r 172.16.2.4 -g
Real Server配置
##修改内核参数,若要永久生效请修改配置文件[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [[email protected] ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore [[email protected] ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [[email protected] ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce[[email protected] ~]# ifconfig lo:0 172.16.2.5/32 broadcast 172.16.2.5 up #配置VIP到lo:0[[email protected] ~]# route add -host 172.16.2.5 dev lo:0[[email protected] ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS1安装httpdsuccess #安装成功[[email protected] ~]# route add default gw 172.16.2.2 #设置默认网关为Director的DIP[[email protected] ~]# echo "<h1>This is Real Server 2 </h1>" > /var/www/html/index.html #添加网页[[email protected] ~]# service httpd start &> /dev/null && echo success #启动httpd服务success #启动成功
##以下操作在rs2中执行[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [[email protected] ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore [[email protected] ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [[email protected] ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce[[email protected] ~]# ifconfig lo:0 172.16.2.5/32 broadcast 172.16.2.5 up #配置VIP到lo:0[[email protected] ~]# route add -host 172.16.2.5 dev lo:0[[email protected] ~]# yum install httpd -y &> /dev/null && echo success || echo failure #RS1安装httpdsuccess #安装成功[[email protected] ~]# route add default gw 172.16.2.2 #设置默认网关为Director的DIP[[email protected] ~]# echo "<h1>This is Real Server 2 </h1>" > /var/www/html/index.html #添加网页[[email protected] ~]# service httpd start &> /dev/null && echo success #启动httpd服务success #启动成功
测试
总结:
本文介绍了如何使用lvs来对多台服务器进行负载均衡, 但是还是有多情况是本文没有介绍的, 例如TUN, FULLNAT等, 以后有机会会和大家分享, 过两天可能会写使用keepalive+lvs实现director的高可用, 敬请期待
作者:AnyISalIn QQ:1449472454
感谢:MageEdu
时间: 2024-11-06 17:14:01