网络拓扑
VS服务器,一个接口,两个IP
DIP要求和RS在同一个网段中
VIP是公有地址
在生产中,VIP是公网地址
在网络环境中,VIP的子网掩码任意,只有一它台主机在独立的网段中,所以子网掩码任意
路由器上有两个接口,一个和VIP在一个网段,通过这个地址连接到VIP上,一个和私网的地址进行通讯的
RS的地址和DIP和是私网地址,不需要互联网可以访问
实现了在同一个物理络但逻辑上是两个不同的网段
只有路由器是两块网卡,其他都是一块网卡
主机:5台
路由器router
hostname router
exec bash
设置网络
ens33
IPADDR=172.18.0.200
PREFIX=16
DNS1=223.5.5.5
DNS2=223.6.6.6
ens34
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.111.101
PREFIX=24
GATEWAY=192.168.111.1
ens34:1 定义网卡别名
[root@router /etc/sysconfig/network-scripts]# cp ifcfg-ens34 ./ifcfg-ens34:1
[root@router /etc/sysconfig/network-scripts]# vim ifcfg-ens34:1
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
DEVICE=ens34:1
ONBOOT=yes
IPADDR=10.0.0.200
PREFIX=8
[root@router ~]# systemctl restart network
CentOS6需要关闭NetworkManagre服务
客户端
hostname client
[root@localhost ~]# exec bash
配置网络
nmcli connection modify ens33 ipv4.addresses 172.18.0.123/16 ipv4.gateway 172.18.0.200 ipv4.method manual
nmcli connection up ens33
[root@client ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.18.0.200 0.0.0.0 UG 100 0 0 ens33
172.18.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens33
测试网络连通性
[root@client ~]# ping 10.0.0.200
PING 10.0.0.200 (10.0.0.200) 56(84) bytes of data.
64 bytes from 10.0.0.200: icmp_seq=1 ttl=64 time=1.08 ms
[root@client ~]# ping 192.168.111.101
PING 192.168.111.101 (192.168.111.101) 56(84) bytes of data.
64 bytes from 192.168.111.101: icmp_seq=1 ttl=64 time=0.609 ms
[root@client ~]# ping 172.18.0.200
PING 172.18.0.200 (172.18.0.200) 56(84) bytes of data.
64 bytes from 172.18.0.200: icmp_seq=1 ttl=64 time=0.356 ms
网络配置完成!
RS1,RS2
RS1
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.111.102
PREFIX=24
GATEWAY=192.168.111.101
RS2
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.111.103
PREFIX=24
GATEWAY=192.168.111.101
ifconfig ens33 down
route -n
[root@RS1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.111.101 0.0.0.0 UG 100 0 0 ens34
192.168.111.0 0.0.0.0 255.255.255.0 U 100 0 0 ens34
[root@RS2 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.111.101 0.0.0.0 UG 100 0 0 ens34
192.168.111.0 0.0.0.0 255.255.255.0 U 100 0 0 ens34
彼此可以ping通
[root@RS1 ~]# ping 172.18.0.123
PING 172.18.0.123 (172.18.0.123) 56(84) bytes of data.
64 bytes from 172.18.0.123: icmp_seq=1 ttl=63 time=2.25 ms
[root@RS2 ~]# ping 172.18.0.123
PING 172.18.0.123 (172.18.0.123) 56(84) bytes of data.
64 bytes from 172.18.0.123: icmp_seq=1 ttl=63 time=2.04 ms
注意:route机器需要启用路由功能
[root@router ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
生效
[root@router ~]# sysctl -p
配置VS
LVS机器
hostname LVS
exec bash
网络配置
[root@LVS ~]# v34
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=ens34
DEVICE=ens34
ONBOOT=yes
IPADDR=192.168.111.100
PREFIX=24
GATEWAY=192.168.111.1(实验时,网关指向路由器的网关),这里指向的是本机IP地址的网关,实验过程中也是可以的,但还是建议把这个网关指向路由器的私有地址的接口上
66:00分钟的时候提到网关的问题:网关先配上
VS服务器可以不配置网关,请求包到达VS后,不通过VS返回
注意:配上网关,先配上,配的网关是路由器的网关
LV
[root@LVS ~]# cat lvs_dr_vs.sh
#!/bin/bash
vip=’10.0.0.100′
iface=’ens34:1′
mask=’255.0.0.0’#子网掩码是什么都可以
port=’80’
rs1=’192.168.111.102′
rs2=’192.168.111.103′
scheduler=’wrr’
type=’-g’
rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null
case $1 in
start)
ifconfig $iface $vip netmask $mask #broadcast $vip up
iptables -F
ipvsadm -A -t ${vip}:${port} -s $scheduler
ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
echo “The VS Server is Ready!”
;;
stop)
ipvsadm -C
ifconfig $iface down
echo “The VS Server is Canceled!”
;;
*)
echo “Usage: $(basename $0) start|stop”
exit 1
;;
esac
[root@LVS ~]#
[root@LVS ~]# bash lvs_dr_vs.sh start
[root@LVS ~]# ip a
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:49:43:34 brd ff:ff:ff:ff:ff:ff
inet 192.168.111.100/24 brd 192.168.111.255 scope global ens34
valid_lft forever preferred_lft forever
inet 10.0.0.100/8 brd 10.255.255.255 scope global ens34:1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe49:4334/64 scope link
valid_lft forever preferred_lft forever
[root@LVS ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 wrr
-> 192.168.111.102:80 Route 1 0 0
-> 192.168.111.103:80 Route 1 0 0
RS配置
[root@RS1 ~]# cat lvs_dr_rs.sh
#!/bin/bash
vip=10.0.0.100
mask=’255.0.0.0’#子网掩码是什么都可以
dev=lo:1
rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
service httpd start &> /dev/null && echo “The httpd Server is Ready!”
echo “`hostname`” > /var/www/html/index.html
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $dev $vip netmask $mask #broadcast $vip up广播可以不写
#route add -host $vip dev $dev 路由可以不写
echo “The RS Server is Ready!”
;;
stop)
ifconfig $dev down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo “The RS Server is Canceled!”
;;
*)
echo “Usage: $(basename $0) start|stop”
exit 1
;;
esac
[root@RS1 ~]#
[root@RS1 ~]# bash lvs_dr_rs.sh start
[root@RS2 ~]# bash lvs_dr_rs.sh start
确定VIP被绑定到lo上
[root@RS1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
inet 10.0.0.100/8 scope global lo:1
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
关闭防火墙、关闭selinux、关闭桥接网卡、开启httpd服务
iptables -F && setenforce 0 && ifconfig ens33 down && systemctl start httpd
测试
[root@client ~]# for i in {1..100};do curl 10.0.0.100; sleep 0.1;done
RS1 Server
RS2 Server
RS1 Server
RS2 Server
脚本中的lo网卡的arp_announce和arp_ignore是不是可以不加?
修改脚本,注释掉lo网卡所在的行,测试
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
也就是说,加了all是不是可以不加lo
RS1和RS2
修改对应的选项后重新执行
bash lvs_dr_rs.sh start
是可以的!
all就是代表所有,lo是单独的针对lo网卡,加上all 和 lo 相对于是双保险
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/91936