LVS的DR模式和NAT模式配置http
lvs 的 NAT
LVS服务器(DR) |
DIP:192.168.10.201 |
VIP:192.168.10.250 |
apache服务器(RS1) |
RIP:192.168.10.202 |
|
apache服务器(RS2) |
RIP:192.168.10.203 |
|
##三台虚拟机都关闭防火墙跟selinx
[root@localhost ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
##下载软件
[root@localhost ~]# yum install -y ipvsadm net-tools
##配置DR的IP
[root@localhost html]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR0=192.168.10.201
IPADDR1=192.168.10.250
GATEWAY=192.168.10.2
PREFIX=24
DNS1=114.114.114.114
##开启转发功能
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 ##添加此行
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
##添加规则调度器
[root@localhost ~]# ipvsadm -A -t 192.168.10.201:80 -s rr
[root@localhost ~]# 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.10.201:80 rr
[root@localhost ~]# ipvsadm -a -t 192.168.10.201:80 -r 192.168.10.202:80 -m
[root@localhost ~]# ipvsadm -a -t 192.168.10.201:80 -r 192.168.10.203:80 -m
[root@localhost ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@localhost ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.10.201:80 -s rr
-a -t 192.168.10.201:80 -r 192.168.10.202:80 -m -w 1
-a -t 192.168.10.201:80 -r 192.168.10.203:80 -m -w 1
##在192.168.10.202虚拟机httpd的index.html上追加node1
[root@localhost ~]# echo 'node1' > /var/www/html/index.html
[root@localhost ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
##在192.168.10.203虚拟机httpd的index.html上追加node2
[root@localhost ~]# echo 'node2' > /var/www/html/index.html
[root@localhost ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
##测试
[root@localhost ~]# curl 192.168.10.201
node1
[root@localhost ~]# curl 192.168.10.201
node2
lvs 的 DR
LVS服务器(DR) |
DIP:192.168.10.201 |
VIP:192.168.10.250 |
apache服务器(RS1) |
RIP:192.168.10.202 |
VIP:192.168.10.250 |
apache服务器(RS2) |
RIP:192.168.10.203 |
VIP:192.168.10.250 |
##关闭三台虚拟机的防火墙与selinux
[root@localhost ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
##DR配置VIP
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.10.201
IPADDR1=192.168.10.250
GATEWAY=192.168.10.2
PREFIX=24
DNS1=114.114.114.114
[root@localhost ~]# ifdown ens33 ;ifup ens33
Connection 'ens33' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@localhost ~]# ip a
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:77:cc:9a brd ff:ff:ff:ff:ff:ff
inet 192.168.10.201/24 brd 192.168.10.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.10.250/24 brd 192.168.10.255 scope global secondary noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe77:cc9a/64 scope link tentative
valid_lft forever preferred_lft forever
##配置RS1与RS2的APR内核参数
[root@RS1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@RS2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
##RS1与RS2上配置VIP(一定要先设置好内核参数在配置VIP,如果先配置VIP,VIP配置好后会立即通告给所有人,而修改内核参数就是为了不通告)
[root@RS1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.10.202
IPADDR1=192.168.10.250
GATEWAY=192.168.10.2
PREFIX=24
DNS1=114.114.114.114
[root@RS1 ~]# ifdown ens33 ;ifup ens33
成功停用连接 "ens33"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/1)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
[root@RS1 ~]# ip a
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:21:00:bb brd ff:ff:ff:ff:ff:ff
inet 192.168.10.202/24 brd 192.168.10.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.10.250/24 brd 192.168.10.255 scope global secondary noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe21:bb/64 scope link
valid_lft forever preferred_lft forever
[root@RS2 ~]# ip a
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:67:09:bd brd ff:ff:ff:ff:ff:ff
inet 192.168.10.203/24 brd 192.168.10.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.10.250/24 brd 192.168.10.255 scope global secondary noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe67:9bd/64 scope link
valid_lft forever preferred_lft forever
##DR上安装ipvsadm
[root@localhost ~]# yum -y install ipvsadm
##ipvsadm配置
[root@localhost ~]# ipvsadm -A -t 192.168.10.250:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.10.250:80 -r 192.168.10.202:80 -g
[root@localhost ~]# ipvsadm -a -t 192.168.10.250:80 -r 192.168.10.203:80 -g
[root@localhost ~]# ipvsadm -Sn
-A -t 192.168.240.250:80 -s rr
-a -t 192.168.240.250:80 -r 192.168.240.50:80 -g -w 1
-a -t 192.168.240.250:80 -r 192.168.240.60:80 -g -w 1
##保存策略文件
[root@localhost ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
(-A:添加虚拟服务器
-t :指定vip及tcp端口
-s:指定算法
rr:轮询
-a :添加节点
-t :指定vip和端口
-r :指定节点ip及端口
-g:表示使用DR模式
-w:设置权重)
##给R1和R2http追加文件进行测试
[root@RS1 ~]# echo "node1-2" > /var/www/html/index.html
[root@RS2 ~]# echo "node2-2" > /var/www/html/index.html
访问192.168.10.201