LVS-DR模式配置搭建

2022-12-21 18:58:21 浏览数 (1)

1.Director配置

操作IP 192.168.0.108,配置VIP(创建eth0:0并配置)

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0

vi /etc/sysconfig/network-scripts/ifcfg-eth0:0

代码语言:javascript复制
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=eth0:0
UUID=b6790a67-bcb1-4b5f-87f8-8355ef4ebea0
DEVICE=eth0:0
ONBOOT=yes
IPADDR=192.168.0.108
NETMASK=255.255.255.0

systemctl restart network

2.安装ipvsadm并配置

yum install -y ipvsadm

ipvsadm -A -t 192.168.0.108 -s rr ipvsadm -a -t 192.168.0.108:80 -r 192.168.0.133 -g ipvsadm -a -t 192.168.0.108:80 -r 192.168.0.134 -g

[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.0.108:80 rr   -> 192.168.0.133:80             Route   1      0          0   -> 192.168.0.134:80             Route   1      0          0 #防火墙设置

firewall-cmd --permanent --add-port=80/tcp

forewall-cmd --reload

3、两台apache配置

yum install -y httpd systemctl enable httpd systemctl start httpd cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:1

vi /etc/sysconfig/network-scripts/ifcfg-lo:1

代码语言:javascript复制
DEVICE=lo:1
IPADDR=192.168.0.108
NETMASK=255.255.255.255
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
ONBOOT=yes
NAME=loopback
~

systemctl restart network

firewall-cmd --permanent --add-port=80/tcp

#关闭VIP ARP响应

echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

服务器添加首页

echo abctest192.168.0.134 >/var/www/html/index.html

echo edf is test192.168.0.133 >/var/www/html/index.html

4.测试 [root@lvs ~]# curl http://192.168.0.108 edf is test192.168.0.133 [root@lvs ~]# curl http://192.168.0.108 abctest192.168.0.134 [root@lvs ~]# curl http://192.168.0.108 edf is test192.168.0.133 [root@lvs ~]# curl http://192.168.0.108 edf is test192.168.0.133

5.脚本实现

lvs管理器上的脚本

代码语言:javascript复制
#!/bin/bash
#lvs管理器配置,lvs之DR模式
# vim /usr/local/sbin/lvs_nat.sh
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.0.108
rs1=192.168.0.133
rs2=192.168.0.134
#添加vip网卡之eth0:0
ifconfig eth0:0 down
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eth0:0
ipvsadm -C
ipvsadm -A -t 192.168.0.108 -s rr
ipvsadm -a -t 192.168.0.108:80 -r 192.168.0.133 -g
ipvsadm -a -t 192.168.0.108:80 -r 192.168.0.134 -g
#关闭VIP ARP响应
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

两台apache上的配置脚本

代码语言:javascript复制
#!/bin/bash
# vim /usr/local/sbin/lvs_dr_rs.sh
vip=192.168.0.108
#添加lo:1
ifconfig lo:1 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:1
#关闭VIP ARP响应
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

0 人点赞