步骤
#修改主机名
[root@localhost ~]# hostname DR
[root@localhost ~]# bash
[root@DR ~]#
[root@localhost ~]# hostname RS1
[root@localhost ~]# bash
[root@RS1 ~]#
[root@localhost ~]# hostname RS2
[root@localhost ~]# bash
[root@RS2 ~]#
#关闭防火墙等操作
[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
#安装httpd修改内容来提供访问
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo "hello 192.168.230.50" > /usr/share/httpd/noindex/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo "hello 192.168.230.51" > /usr/share/httpd/noindex/index.html
[root@RS2 ~]# systemctl restart httpd
#DR配置VIP
[root@DR ~]# ip addr add 192.168.230.250/24 dev ens160
[root@DR ~]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:db:9f:99 brd ff:ff:ff:ff:ff:ff
inet 192.168.230.95/24 brd 192.168.230.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.230.250/24 scope global secondary ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fedb:9f99/64 scope link
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
[root@RS1 ~]# ip addr add 192.168.230.250/24 dev ens33
[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:7a:f0:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.230.132/24 brd 192.168.230.255 scope global dynamic noprefixroute ens33
valid_lft 1248sec preferred_lft 1248sec
inet 192.168.230.250/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::79b5:2695:ddad:e15d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@RS2 ~]# ip addr add 192.168.230.250/24 dev ens33
[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:19:97:fc brd ff:ff:ff:ff:ff:ff
inet 192.168.230.131/24 brd 192.168.230.255 scope global dynamic noprefixroute ens33
valid_lft 1238sec preferred_lft 1238sec
inet 192.168.220.250/24 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::2430:80b0:b79e:f709/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#DR上配置 转发规则
[root@DR ~]# ipvsadm -A -t 192.168.230.250:80 -s rr
[root@DR ~]# ipvsadm -a -t 192.168.230.250:80 -r 192.168.230.132:80 -g
[root@DR ~]# ipvsadm -a -t 192.168.230.250:80 -r 192.168.230.131:80 -g
[root@DR ~]# ipvsadm -Sn
-A -t 192.168.230.250:80 -s rr
-a -t 192.168.230.250:80 -r 192.168.230.131:80 -g -w 1
-a -t 192.168.230.250:80 -r 192.168.230.132: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 的NAT 模式
[root@DR ~]# ip addr add 192.168.230.250/24 dev ens160
[root@DR ~]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:db:9f:99 brd ff:ff:ff:ff:ff:ff
inet 192.168.230.95/24 brd 192.168.230.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.230.250/24 scope global secondary ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fedb:9f99/64 scope link
valid_lft forever preferred_lft forever
#查看DR的网关
[root@DR ~]# tail /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=static
NAME=ens160
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.230.95
PERFIX=24
GATEWAY=192.168.230.2
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.230.132" > /usr/share/httpd/noindex/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo "hello ! 192.168.230.131" > /usr/share/httpd/noindex/index.html
[root@RS2 ~]# systemctl restart httpd
#配置 RS1网关指向DIP
[root@RS1 ~]# sed -ri 's/^(GATEWAY=).*/\1192.168.230.95/' /etc/sysconfig/network-scripts/ifcfg-ens33
[root@RS2 ~]# sed -ri 's/^(GATEWAY=).*/\1192.168.230.95/' /etc/sysconfig/network-scripts/ifcfg-ens33
#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.230.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.230.250:80 rr
[root@DR ~]# ipvsadm -a -t 192.168.230.250:80 -r 192.168.230.50:80 -m
[root@DR ~]# ipvsadm -a -t 192.168.230.250:80 -r 192.168.230.51:80 -m
[root@DR ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@DR ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.230.250:80 -s rr
-a -t 192.168.230.250:80 -r 192.168.230.50:80 -m -w 1
-a -t 192.168.230.250:80 -r 192.168.230.51:80 -m -w 1
[root@DR ~]# curl 192.168.230.250
hello ! 192.168.230.50
[root@DR ~]# curl 192.168.230.250
hello ! 192.168.230.51