- LVS服务器至少2块物理网卡,一块连接公网(VIP地址),一块连接内网;
- 后端Realserver机器的默认网关设置为LVS的内网IP地址;
- 保证LVS内网网卡通常跟Realserver机器在同一个网段;
- LVS NAT模式后端的Realserver机器数量不可超过30台;
- 用户的请求进入和返回均经过LVS,LVS会成为瓶颈;
LVS:192.168.2.10(内网)10.10.10.128(外网)
Real server:192.168.2.20
[root@bogon ~]# ip add
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 pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:ec:10:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/24 brd 192.168.2.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::cbc:47d:6f9d:da7/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:ec:10:5f brd ff:ff:ff:ff:ff:ff
inet 10.10.10.128/8 brd 10.255.255.255 scope global noprefixroute ens37
valid_lft forever preferred_lft forever
inet6 fe80::8467:f657:ad64:7ab4/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost /]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.2.20"
PREFIX="24"
GATEWAY="192.168.2.10"
[root@localhost /]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.10 0.0.0.0 UG 100 0 0 ens33
192.168.2.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
yum -y install ipvsadm.x86_64
#添加VIP10.10.10.128虚拟服务器集群:
ipvsadm -A -t 10.10.10.128:80 -s rr
-A:添加虚拟服务器VIP
-t:tcp协议
-s:指定算法为RR轮询模式
#在虚拟集群10.10.10.128中加入后端Real server服务器:
ipvsadm -a -t 10.10.10.128:80 -r 192.168.2.20:80 -m -w 100
-a:往虚拟服务器集群中添加真实服务器
-t:tcp协议
-r:指定后端realserver服务器的IP和端口
-m:指定NAT转发模式
-w:weight权重设置
#LVS NAT模式能够实现数据转发,还要依靠Linux内核开启转发功能,所以需如下设置:
echo 1 > /proc/sys/net/ipv4/ip_forward
#查看LVS NAT模式集群配置信息:
[root@bogon ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.10.10.128:80 rr
-> 192.168.2.20:80 Masq 100 0 0
[root@bogon ~]# curl 10.10.10.128
this is 192.168.2.20 page!!!
- LVS服务器和后端Realserver必须在同网段(内网、公网)
- LVS修改请求报文的目标MAC,目录IP(VIP)不修改
- LVS和RealServer需要配置VIP地址,并且VIP地址一样,以及RealServerVIP配置的网卡需要抑制ARP响应。
- 目标IP(VIP)保持不变,在Real server客户端配置VIP,lo网卡上面配置(不冲突)
- Realserver客户端服务器的网关指向路由器吓一跳(默认网关),保证数据能够出去(访问外网)
- 所有的Realserver客户端服务器,抑制VIP ARP广播,禁止VIP相应解析,而且保证真实网卡不能抑制ARP广播
#删除刚才的配置:
ipvsadm -d -t 10.10.10.128:80 -r 192.168.20.20
ipvsadm -D -t 10.10.10.128:80
[root@bogon ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
可以看的到已经清空了
#这里LVS只需要一张网卡就可以,上次实验添加的网卡可以删除掉。
#配置VIP地址,这里LVS和RealServer都需要配置VIP地址:
#LVS服务配置VIP地址如下:
vim /etc/sysconfig/network-scripts/ifcfg-ens33:1
TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens33:1"
DEVICE="ens33:1"
ONBOOT="yes"
IPADDR="192.168.2.100"
NETMASK="255.255.255.255"
#重启服务:
systemctl restart network
[root@localhost ~]# ip add
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 pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:82:7c:0e brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/24 brd 192.168.2.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.2.100/32 brd 192.168.2.100 scope global noprefixroute ens33:1
valid_lft forever preferred_lft forever
inet6 fe80::3685:ac7b:7c5:a888/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#可以看得出来VIP已经配置成功
#在RealServer配置VIP地址
vim /etc/sysconfig/network-scripts/ifcfg-lo:1
DEVICE=lo
IPADDR=192.168.2.100
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
#注意:上面再ens33网卡上配置的内容需要修改回原来模样。
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.2.20"
NETMASK="255.255.255.0"
GATEWAY="192.168.2.2"
DNS1="192.168.2.2"
DNS2="114.114.114.114"
#重启服务:
systemctl restart network
#验证是否配置成功:
[root@localhost ~]# ip add
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
inet 192.168.2.100/32 brd 192.168.2.100 scope global 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 pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:87:bc:35 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.20/24 brd 192.168.2.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe87:bc35/64 scope link
valid_lft forever preferred_lft forever
#在RealServer机器上lo网卡抑制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
#在LVS上添加VIP192.168.2.100虚拟服务器集群:
ipvsadm -A -t 192.168.2.100:80 -s rr
-A:添加虚拟服务器VIP
-t:tcp协议
-s:指定算法为RR轮询模式
#在虚拟集群192.168.2.100中加入后端Real server服务器:
ipvsadm -a -t 192.168.2.100:80 -r 192.168.2.20:80 -g -w 100
-a:往虚拟服务器集群中添加真实服务器
-t:tcp协议
-r:指定后端realserver服务器的IP和端口
-g:指定DR转发模式
-w:weight权重设置
ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.100:80 rr
-> 192.168.2.20:80 Route 100 0 0
#安装ipvsadm
yum -y install ipvsadm.x86_64
#安装keepalive高可用软件:
yum -y install keepalived.x86_64
#master上keepalived.conf配置如下:
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state MASTER
interface ens33
lvs_sync_daemon_inteface ens33
virtual_router_id 51
priority 100
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.100
}
}
virtual_server 192.168.2.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.2.10 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.2.20 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
# Backup上keepalived.conf配置代码:
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state BACKUP
interface ens33
lvs_sync_daemon_inteface ens33
virtual_router_id 51
priority 90
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.100
}
}
virtual_server 192.168.2.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.2.10 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.2.20 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}