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

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

前言:

在上篇文章中我们讲了一些LVS的基本概念和相应模型的实验原理和流程,本篇文章我们主要使用lvs为web服务提供负载均衡

NAT实现

实验拓扑

blob.png

实验环境

主机 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 ~]#

测试

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

DR实现

实验拓扑

blob.png

实验环境

主机 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专题: NAT和DR模型实现Web负载均衡

总结:

本文介绍了如何使用lvs来对多台服务器进行负载均衡, 但是还是有多情况是本文没有介绍的, 例如TUN, FULLNAT等, 以后有机会会和大家分享, 过两天可能会写使用keepalive+lvs实现director的高可用, 敬请期待 
作者:AnyISalIn QQ:1449472454 
感谢:MageEdu

原创文章,作者:Net18-AnyISalIn,如若转载,请注明出处:http://www.178linux.com/14212

(0)
Net18-AnyISalInNet18-AnyISalIn
上一篇 2016-04-05 21:24
下一篇 2016-04-05 22:40

相关推荐

  • haproxy代理服务

    HAProxy: LB Cluster:         四层:   lvs, nginx(stream),haproxy(mode tcp)         七层:   http: nginx(http, ngx_http_upstrea…

    Linux干货 2017-05-17
  • 系统基础之权限管理作业题

    1.问题:  在/data/testdir里创建的新文件自动属于g1组,组g2的成员如: alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@wen-7 testdir]# mkdir -p /data/…

    Linux干货 2016-08-04
  • grub知识与故障排除

    知识点回顾 at 任务的存放位置:/var/spool/at/ crond 任务存放位置:/var/spool/cron/username 查看服务有没有运行 centos6:  service  atd  status    chkconfig –list atd chkconfig atd o…

    Linux干货 2016-09-13
  • heartbeartv2实现lamp高可用-week17

    3、基于heartbeat v2 crm实现HA LAMP组合;要求,部署wordpress,用于编辑的文章中的任何数据在节点切换后都能正常访问; 拓扑: 环境: CentOS6.6NFS: 172.16.0.34 输出mysql数据目录ntp: 172.16.0.31 时间服务器node1: 172.16.0.32 heartbeart+httpd+php…

    Linux干货 2017-05-23
  • 0803作业

    课上练习 1.当用户xiaoming 对/testdir  目录无执行权限时,意味着无法做哪些操作?  drwxr-xr–. 14 root root 4096 Aug  3 13:35 /testdir [xiaoming@localhost ~]$ touch /testdir/f1…

    Linux干货 2016-08-04
  • linux系统启动及kickstart

    1、简述linux操作系统启动流程
    2、简述grub启动引导程序配置及命令行接口详解
    3、实现kickstart文件制作与光盘镜像制作

    2018-01-17