LVS的DR,NAT模式实现 http 负载均衡

LVS 的DR模式

LVS服务器(DR) DIP:192.168.220.9 VIP:192.168.220.250
apache服务器(RS1) IP:192.168.220.10 VIP:192.168.220.250
apache服务器(RS2) IP:192.168.220.17 VIP:192.168.220.250
[root@localhost ~]# hostnamectl set-hostname DR
[root@localhost ~]# bash
[root@DR ~]# 


[root@localhost ~]# hostnamectl set-hostname RS1
[root@localhost ~]# bash
[root@RS1 ~]#


[root@localhost ~]# hostnamectl set-hostname RS2
[root@localhost ~]# bash
[root@RS2 ~]#

关闭防火墙 seLinux

[root@DR ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@DR ~]# setenforce 0

[root@RS1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# setenforce 0

[root@RS2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# setenforce 0

RS1和RS2下载安装httpd修改内容来提供访问

[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo "hello 192.168.220.10" > /usr/share/httpd/noindex/index.html
[root@RS1 ~]# systemctl restart httpd

[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo "hello 192.168.220.17" > /usr/share/httpd/noindex/index.html
[root@RS2 ~]# systemctl restart httpd

DR配置VIP

[root@DR ~]# ip addr add 192.168.220.250/24 dev ens33   # 临时IP
[root@DR ~]# ip a
   .......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:34:91:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.220.9/24 brd 192.168.220.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.220.250/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::2166:5bdf:402b:32c9/64 scope link dadfailed tentative noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::cc61:eeb0:86a:e547/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

RS配置apr内核参数

[root@RS1 ~]# cat /etc/sysctl.conf   # 添加两行
net.ipv4.conf.all.arp_ignore = 1  # 将对应网卡设置为只回应目标IP为自身接口地址的ARP请求
net.ipv4.conf.all.arp_announce = 2 # 将ARP请求的源IP设置为ens33上的IP,也就是RIP

[root@RS1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

[root@RS2 ~]# cat /etc/sysctl.conf   # 添加两行
net.ipv4.conf.all.arp_ignore = 1  # 将对应网卡设置为只回应目标IP为自身接口地址的ARP请求
net.ipv4.conf.all.arp_announce = 2 # 将ARP请求的源IP设置为ens33上的IP,也就是RIP

[root@RS2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

RS上配置VIP

一定要先设置好内核参数在配置VIP,如果先配置VIP,VIP配置好后会立即通告给所有人,而修改内核参数就是为了不通告

[root@RS1 ~]# ip addr add 192.168.220.250/24 dev ens33  # 临时IP
[root@RS1 ~]# ip a
 .......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:05:72:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.220.10/24 brd 192.168.220.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.220.250/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::2166:5bdf:402b:32c9/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
       
[root@RS2 ~]# ip addr add 192.168.220.250/24 dev ens33  # 临时IP
[root@RS2 ~]# ip a
  .......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:21:52:e8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.220.17/24 brd 192.168.220.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.220.250/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1792:21f6:7f28:5ffa/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
       

DR上配置 转发规则

#LVS依赖于ipvsadm来进行配置,所以首先安装ipvsadm
[root@DR ~]# yum -y install ipvsadm
[root@DR ~]# ipvsadm -A -t 192.168.220.250:80 -s rr
[root@DR ~]# ipvsadm -a -t 192.168.220.250:80 -r 192.168.220.10:80 -g
[root@DR ~]# ipvsadm -a -t 192.168.220.250:80 -r 192.168.220.17:80 -g
[root@DR ~]# ipvsadm -Sn
-A -t 192.168.220.250:80 -s rr
-a -t 192.168.220.250:80 -r 192.168.220.10:80 -g -w 1
-a -t 192.168.220.250:80 -r 192.168.220.17:80 -g -w 1
[root@DR ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm    #策略都会保存到这个文件
 

-A:添加虚拟服务器

-t :指定vip及tcp端口

-s:指定算法 

rr:轮询


-a :添加节点 

-t :指定vip和端口

-r :指定节点ip及端口

-g:表示使用DR模式

-w:设置权重

访问测试

LVS的DR,NAT模式实现 http 负载均衡

LVS的DR,NAT模式实现 http 负载均衡

LVS 的NAT 模式

LVS服务器(DR) DIP:192.168.220.9 VIP:192.168.220.250
apache服务器(RS1) IP:192.168.220.10 网关为DR网关
apache服务器(RS2) IP:192.168.220.17 网关为DR网关

关闭防火墙和seLinux

[root@DR ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@DR ~]# setenforce 0

[root@RS1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# setenforce 0

[root@RS2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# setenforce 0

配置 DR的VIP

[root@DR ~]# ip addr add 192.168.220.250/24 dev ens33   # 临时IP
[root@DR ~]# ip a
   .......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:34:91:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.220.9/24 brd 192.168.220.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.220.250/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::2166:5bdf:402b:32c9/64 scope link dadfailed tentative noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::cc61:eeb0:86a:e547/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
       
# 查看DR的网关
[root@DR ~]# tail /etc/sysconfig/network-scripts/ifcfg-ens33 
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=37b83d64-7591-47bc-b204-7b7ca9d23b5c
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.220.9
GATEWAY=192.168.220.2    # 网关
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8

下载httpd并修改显示内容

[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo "hello ! 192.168.220.10" > /usr/share/httpd/noindex/index.html
[root@RS1 ~]# systemctl restart httpd

[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo "hello ! 192.168.220.17" > /usr/share/httpd/noindex/index.html
[root@RS2 ~]# systemctl restart httpd

配置 RS1网关为DR网关

[root@RS1 ~]# sed -ri 's/^(GATEWAY=).*/\1192.168.220.2/' /etc/sysconfig/network-scripts/ifcfg-ens33
[root@RS1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
.......
NAME=ens33
UUID=37b83d64-7591-47bc-b204-7b7ca9d23b5c
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.220.10
GATEWAY=192.168.220.2    # 修改成功
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.

配置 RS2网关为DR网关

[root@RS2 ~]# sed -ri 's/^(GATEWAY=).*/\1192.168.220.2/' /etc/sysconfig/network-scripts/ifcfg-ens33
[root@RS2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
.......
NAME=ens33
UUID=6a92f43d-2a92-4b14-acf3-523cdfc35f02
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.220.17
GATEWAY=192.168.220.2    # 成功
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8

DR上配置 转发规则

#LVS依赖于ipvsadm来进行配置,所以首先安装ipvsadm
[root@DR ~]# yum -y install ipvsadm

#开启转发功能
[root@DR ~]# vi /etc/sysctl.conf
[root@DR ~]# sysctl -p
net.ipv4.ip_forward = 1

# 配置转发规则
[root@DR ~]# ipvsadm -A -t 192.168.220.250:80 -s rr
[root@DR ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.220.250:80 rr

[root@DR ~]# ipvsadm -a -t 192.168.220.250:80 -r 192.168.220.10:80 -m
[root@DR ~]# ipvsadm -a -t 192.168.220.250:80 -r 192.168.220.17:80 -m

[root@DR ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@DR ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.220.250:80 -s rr
-a -t 192.168.220.250:80 -r 192.168.220.10:80 -m -w 1
-a -t 192.168.220.250:80 -r 192.168.220.17:80 -m -w 1

-A:添加虚拟服务器

-t :指定vip及tcp端口

-s:指定算法 

rr:轮询


-a :添加节点 

-t :指定vip和端口

-r :指定节点ip及端口

-g:表示使用DR模式

-w:设置权重

测试

[root@DR ~]# curl 192.168.220.250
hello ! 192.168.220.10
[root@DR ~]# curl 192.168.220.250
hello ! 192.168.220.17
上一篇:centOS 7下无法启动网络(service network start)错误解决办法


下一篇:nginx+keepalived实现主从模式双机热备份