LVS-DR&NAT模式实现http负载均衡

DR模式

LVS服务器(DR)DIP:192.168.235.135VIP:192.168.235.250
apache服务器(RS1)IP:192.168.235.125VIP:192.168.235.250
apache服务器(RS2)IP:192.168.235.156VIP:192.168.235.250
关闭防火墙和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@RS1 ~]# systemctl disable --now firewalld
[root@rs2 ~]# systemctl disable --now firewalld

RS1和RS2下载安装httpd修改内容来提供访问
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS1 ~]# echo "hello 192.168.235.125" > /usr/share/httpd/noindex/index.html


[root@rs2 ~]# yum -y install httpd
[root@rs2 ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@rs2 ~]# echo "hello 192.168.235.156" > /usr/share/httpd/noindex/index.html



DR配置VIP
[root@DR ~]# ip addr add 192.168.235.250/24 dev ens160 
[root@DR ~]# ip a
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:9f:b9:ba brd ff:ff:ff:ff:ff:ff
    inet 192.168.235.135/24 brd 192.168.235.255 scope global dynamic noprefixroute ens160
       valid_lft 1698sec preferred_lft 1698sec
    inet 192.168.235.250/24 scope global secondary ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::7ff5:eb54:cd1e:90c1/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设置为ens160上的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设置为ens160上的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.235.250/24 dev eth0 
[root@RS1 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:73:89:b1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.235.125/24 brd 192.168.235.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 192.168.235.250/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe73:89b1/64 scope link 
       valid_lft forever preferred_lft forever

[root@rs2 ~]# ip addr add 192.168.235.250/24 dev ens33
[root@rs2 ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d7:f4:87 brd ff:ff:ff:ff:ff:ff
    inet 192.168.235.156/24 brd 192.168.235.255 scope global noprefixroute dynamic ens33
       valid_lft 1481sec preferred_lft 1481sec
    inet 192.168.235.250/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::e7a9:b0e1:c122:7cf6/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.235.250:80 -s rr
[root@DR ~]# ipvsadm -a -t 192.168.235.250:80 -r 192.168.235.125:80 -g
[root@DR ~]# ipvsadm -a -t 192.168.235.250:80 -r 192.168.235.156:80 -g
[root@DR ~]# ipvsadm -Sn
-A -t 192.168.235.250:80 -s rr
-a -t 192.168.235.250:80 -r 192.168.235.125:80 -g -w 1
-a -t 192.168.235.250:80 -r 192.168.235.156: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:设置权重


测试

在这里插入图片描述
在这里插入图片描述

NAT模式

LVS服务器(DR)DIP:192.168.235.135VIP:192.168.235.250
apache服务器(RS1)IP:192.168.235.125网关指向DIP
apache服务器(RS2)IP:192.168.235.156网关指向DIP
关闭防火墙和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.235.250/24 dev ens160 
[root@DR ~]# ip a
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:9f:b9:ba brd ff:ff:ff:ff:ff:ff
    inet 192.168.235.135/24 brd 192.168.235.255 scope global dynamic noprefixroute ens160
       valid_lft 1698sec preferred_lft 1698sec
    inet 192.168.235.250/24 scope global secondary ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::7ff5:eb54:cd1e:90c1/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

下载httpd并修改显示内容
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo "hello yaya 192.168.235.125" > /usr/share/httpd/noindex/index.html
[root@RS1 ~]# systemctl restart httpd

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


配置 RS1网关指向DIP
[root@RS1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.235.125
GATEWAY=192.168.235.9  / 成功
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8

[root@RS2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.235.156
GATEWAY=192.168.235.9    /成功
NETMASK=255.255.255.0
DNS1=114.114.114.114

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.235.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.235.250:80 rr

[root@DR ~]# ipvsadm -a -t 192.168.235.250:80 -r 192.168.235.125:80 -m
[root@DR ~]# ipvsadm -a -t 192.168.235.250:80 -r 192.168.235.156:80 -m

[root@DR ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@DR ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.235.250:80 -s rr
-a -t 192.168.235.250:80 -r 192.168.235.125:80 -m -w 1
-a -t 192.168.235.250:80 -r 192.168.235.156:80 -m -w 1
-A:添加虚拟服务器

-t :指定vip及tcp端口

-s:指定算法 

rr:轮询

-a :添加节点 

-t :指定vip和端口

-r :指定节点ip及端口

-g:表示使用DR模式

-w:设置权重



测试
[root@DR ~]# curl 192.168.235.250
hello yaya 192.168.235.125
[root@DR ~]# curl 192.168.235.250
hello yaya 192.168.235.156
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值