LVS_NAT和LVS_DR模式实现web负载均衡

一、   LVS_NAT模式实现web负载均衡


1.1 实验环境说明


实验拓扑


1.2 配置好个主机的IP地址和主机名(略)


1.3 关闭Linux防火墙和selinux


1.3.1时间同步

ntpdate ntp.aliyun.com

1.3.2关闭selinux

sed -i 's/^SELINUX=.*$/SELINUX=disable/' /etc/selinux/config

setenforce 0

1.3.3关闭防火墙

CentOS 7.x

  systemctl disable firewalld

  systemctl stop firewalld

CentOS 6.x

  /etc/init.d/iptables stop

  chkconfig iptables off


1.4 负载均衡服务器(LVS Director)安装配置


1.4.1 安装lvs的管理工具ipvsadm

[root@localhost ~]# yum install -y ipvsadm


1.4.2 开启路由转发功能

[root@localhost ~]# echo 1 > /proc/sys/net/ipv4/ip_forward


1.4.3 LVS Director 配置路由转发

[root@localhost ~]# iptables -t nat -F

[root@localhost ~]# iptables -t nat -X

[root@localhost ~]#  iptables -t nat -A POSTROUTING -s 192.168.2.0/24  -j MASQUERADE


1.4.4 配置LVS规则

[root@localhost ~]# ipvsadm -A -t 10.10.10.11:http -s rr

[root@localhost ~]# ipvsadm -a -t 10.10.10.11:http -r 192.168.2.11:80 -m

[root@localhost ~]# ipvsadm -a -t 10.10.10.11:http -r 192.168.2.12:80 -m


1.4.5 常用的ipvsadm命令

查看ipvs规则

ipvsadm –Ln

清空ipvs规则

Shell > ipvsadm -C

保存ipvs规则

ipvsadm -S > /etc/sysconfig/ipvsadm 或者ipvsadm-save -n > /etc/sysconfig/ipvsadm

恢复ipvs规则

ipvsadm -R < /etc/sysconfig/ipvsadm 或者ipvsadm-restore < /etc/sysconfig/ipvsadm


1.5 后端RS服务器配置


1.5.1 配置RS的RIP网关必须指向DIP(略)


1.5.2 RS上安装Nginx(两个RS都安装)

[root@localhost ~]# yum install epel-release

[root@localhost ~]# yum install -y nginx


1.5.3 RS1和RS2上分别修改nginx的静态主页

[root@localhost ~]# echo "

Testpage on node1.ruhua.com

" > /usr/share/nginx/html/index.html

[root@localhost ~]# echo "

Testpage on node2.ruhua.com

" > /usr/share/nginx/html/index.html


1.5.3启动nginx服务

[root@localhost ~]# service nginx start  #启动nginx

[root@localhost ~]# nginx -s reload #重载配置

[root@localhost ~]# chkconfig nginx on #设为开机启动


1.6.测试lvs负载效果


1.6.1windows上测试

在Windows平台使用浏览器测试,刷新页面,轮询的转发到后端的web1和web2节点上。

1.6.2在Linux上测试

[root@localhost ~]# curl 10.10.10.11

[root@localhost ~]# for i in {1..6} ; do curl http://10.10.10.11; done

[root@localhost ~]# for i in {1..6} ; do sleep 3; curl http://192.168.2.99; done


二、   LVS_NAT模式实现web负载均衡


2.1 实验环境说明


2.2 实验拓扑


2.3 配置好个主机的IP地址和主机名(略)


2.4 关闭Linux防火墙和selinux


2.4.1时间同步

ntpdate ntp.aliyun.com

2.4.2关闭selinux

sed -i 's/^SELINUX=.*$/SELINUX=disable/' /etc/selinux/config

setenforce 0

2.4.3关闭防火墙

CentOS 7.x

  systemctl disable firewalld

  systemctl stop firewalld


2.5 RS安装配置nginx(Apache也可以)


2.5.1 安装nginx

[root@localhost ~]# yum install epel-release

[root@localhost ~]# yum install -y nginx


2.5.2 修改nginx默认主页

RS1

cp /usr/share/nginx/html/{index.html,index.html_bak}

echo "RS1,192.168.99.101"> /usr/share/nginx/html/index.html

RS2

cp /usr/share/nginx/html/{index.html,index.html_bak}

echo "RS2,192.168.99.102"> /usr/share/nginx/html/index.html


2.5.3 启动nginx服务

systemctl start nginx.service

systemctl enable nginx.service


2.5.3 RS配置vip,RS1和RS2分别运行以下脚本

vim /usr/local/sbin/lvs_dr_rs.sh

bash /usr/local/sbin/lvs_dr_rs.sh start|stop


#!/bin/bash

#

vip=192.168.99.99

mask='255.255.255.255'

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 lo:0 $vip netmask $mask broadcast $vip up

route add -host $vip dev lo:0

;;

stop)

ifconfig lo:0 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 "Usage $(basename $0) start|stop"

exit 1

;;

esac


2.6  Director上安装配置LVS


2.6.1 安装lvs的管理工具ipvsadm

[root@localhost ~]# yum install -y ipvsadm

[root@localhost ~]# rpm -qa ipvsadm


2.6.2 Director上运行以下脚本,配置vip和lvs规则

vim /usr/local/sbin/lvs_dr.sh

bash /usr/local/sbin/lvs_dr.sh start|stop


#!/bin/bash

#

vip='192.168.99.99'

iface='ens33:0'

mask='255.255.255.255'

port='80'

rs1='192.168.99.101'

rs2='192.168.99.102'

scheduler='wrr'

type='-g'

case $1 in

start)

ifconfig $iface $vip netmask $mask broadcast $vip up

ipvsadm -A -t ${vip}:${port} -s $scheduler

ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1

ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1

;;

stop)

ipvsadm -C

ifconfig $iface down

;;

*)

echo "Usage $(basename $0) start|stop"

exit 1

;;

esac


2.7 测试lvs负载效果


在linux测试客户机上用curl命令测试

[root@localhost ~]# curl 192.168.99.99

[root@localhost ~]# for i in {1..10} ; do curl http://192.168.99.99; done


你可能感兴趣的:(LVS_NAT和LVS_DR模式实现web负载均衡)