LVS的NAT,DR模式实现 http 负载均衡
lvs 的 NAT
LVS服务器(DR) | DIP:192.168.220.158 | VIP:192.168.200.250 |
---|
apache服务器(RS1) | IP:192.168.200.161 | VIP:192.168.200.250 |
apache服务器(RS2 | IP:192.168.200.162 | VIP:192.168.200.250 |
// 关闭防火墙和seLinux
[root@DR ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@DR ~]
[root@RS1 ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]
[root@RS2 ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]
// 配置DR的VIP
[root@DR ~]
[root@DR ~]
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d1:c0:12 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.158/24 brd 192.168.200.255 scope global dynamic noprefixroute ens160
valid_lft 1725sec preferred_lft 1725sec
inet 192.168.200.250/24 scope global secondary ens160
valid_lft forever preferred_lft forever
inet6 fe80::43a6:c5c4:3410:614/64 scope link noprefixroute
valid_lft forever preferred_lft forever
// 查看DR的网关
[root@DR ~]
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens160"
UUID="2a3547ea-2c29-4092-9c60-b02280d37718"
DEVICE="ens160"
ONBOOT="yes"
IPADDR=192.168.200.158
GATEWAY=192.168.200.2
NETMASK=255.255.255.0
DNS1=114.114.114.114
// 下载httpd并修改显示内容
[root@RS1 ~]
[root@RS1 ~]
echo "hello ! 192.168.200.161" > /usr/share/httpd/noindex/index.html
systemctl restart httpd
[root@RS2 ~]
[root@RS2 ~]
echo "hello ! 192.168.200.162" > /usr/share/httpd/noindex/index.html
systemctl restart httpd
// 配置 RS1网关为DR网关
[root@RS1 ~]
[root@RS1 ~]
TYPE=Ethernet
......
IPADDR=192.168.200.161
GATEWAY=192.168.200.2
NETMASK=255.255.255.0
DNS1=114.114.114.114
// 配置 RS2网关为DR网关
[root@RS2 ~]
[root@RS1 ~]
TYPE=Ethernet
......
IPADDR=192.168.200.162
GATEWAY=192.168.200.2
NETMASK=255.255.255.0
DNS1=114.114.114.114
// DR上配置 转发规则
// LVS依赖于ipvsadm来进行配置,所以首先安装ipvsadm
[root@DR ~]
// 开启转发功能
[root@DR ~]
[root@DR ~]
net.ipv4.ip_forward = 1
// 配置转发规则
[root@DR ~]
[root@DR ~]
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.250:80 rr
[root@DR ~]
[root@DR ~]
[root@DR ~]
[root@DR ~]
-A -t 192.168.200.250:80 -s rr
-a -t 192.168.200.250:80 -r 192.168.200.161:80 -m -w 1
-a -t 192.168.220.250:80 -r 192.168.200.162:80 -m -w 1
-A:添加虚拟服务器
-t :指定vip及tcp端口
-s:指定算法
rr:轮询
-a :添加节点
-t :指定vip和端口
-r :指定节点ip及端口
-g:表示使用DR模式
-w:设置权重
// 测试
[root@DR ~]
hello ! 192.168.200.161
[root@DR ~]
hello ! 192.168.200.162
lvs 的 DR
LVS服务器(DR) | DIP:192.168.220.158 | VIP:192.168.200.250 |
---|
apache服务器(RS1) | IP:192.168.200.161 | VIP:192.168.200.250 |
apache服务器(RS2 | IP:192.168.200.162 | VIP:192.168.200.250 |
[root@localhost ~]
[root@localhost ~]
[root@DR ~]
[root@localhost ~]
[root@localhost ~]
[root@RS1 ~]
[root@localhost ~]
[root@localhost ~]
[root@RS2 ~]
关闭防火墙 seLinux
[root@DR ~]
[root@DR ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@DR ~]
[root@RS1 ~]
[root@RS1 ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]
[root@RS2 ~]
[root@RS2 ~]
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]
RS1和RS2下载安装httpd修改内容来提供访问
[root@RS1 ~]
[root@RS1 ~]
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS1 ~]
[root@RS1 ~]
[root@RS2 ~]
[root@RS2 ~]
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS2 ~]
[root@RS2 ~]
DR配置VIP
[root@DR ~]
[root@DR ~]
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d1:c0:12 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.158/24 brd 192.168.200.255 scope global dynamic noprefixroute ens160
valid_lft 1725sec preferred_lft 1725sec
inet 192.168.200.250/24 scope global secondary ens160
valid_lft forever preferred_lft forever
inet6 fe80::43a6:c5c4:3410:614/64 scope link noprefixroute
valid_lft forever preferred_lft forever
RS配置apr内核参数
[root@RS1 ~]
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS1 ~]
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS2 ~]
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS2 ~]
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
RS上配置VIP
一定要先设置好内核参数在配置VIP,如果先配置VIP,VIP配置好后会立即通告给所有人,而修改内核参数就是为了不通告
[root@RS1 ~]
[root@RS1 ~]
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:ba:9d:68 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.161/24 brd 192.168.200.255 scope global dynamic noprefixroute ens160
valid_lft 1460sec preferred_lft 1460sec
inet 192.168.200.250/24 scope global secondary ens160
valid_lft forever preferred_lft forever
inet6 fe80::237c:e121:7a55:6152/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@RS2 ~]
[root@RS2 ~]
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:7f:60:fd brd ff:ff:ff:ff:ff:ff
inet 192.168.200.162/24 brd 192.168.200.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.200.250/24 scope global secondary ens160
valid_lft forever preferred_lft forever
inet6 fe80::c2a:60f1:204f:da39/64 scope link noprefixroute
valid_lft forever preferred_lft forever
DR上配置 转发规则
[root@DR ~]
[root@DR ~]
[root@DR ~]
[root@DR ~]
[root@DR ~]
-A -t 192.168.200.250:80 -s rr
-a -t 192.168.200.250:80 -r 192.168.200.161:80 -g -w 1
-a -t 192.168.200.250:80 -r 192.168.200.162:80 -g -w 1
[root@DR ~]
A:添加虚拟服务器
-t :指定vip及tcp端口
-s:指定算法
rr:轮询
-a :添加节点
-t :指定vip和端口
-r :指定节点ip及端口
-g:表示使用DR模式
-w:设置权重
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a2a90f4f00bdaa57321bef6f1acc2519.png#pic_center)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ef7bf25cb7ab700157a922b4e032a814.png#pic_center)