LVS +keepalived
环境准备:
准备4台机器,两台装好LVS和Keepalived,作为调度器,另外两台装好httpd服务,作为Real Server。
网络配置:
Diretcory:
名称 | 接口 | IP | 用途 |
MASTER | eth0 | 192.168.1.19 | 用于WAN的数据转发 |
eth1 |
10.0.0.19 | 用于LAN的数据转发 | |
eth2 |
10.0.2.19 | 用于LB之间的心跳连接 | |
VIP |
|
192.168.1.29 | 解析到www.zydev.com |
BACKUP |
eth0 |
192.168.1.16 | 用于WAN的数据转发 |
eth1 |
10.0.0.16 | 用于LAN的数据转发 | |
eth2 |
10.0.2.16 | 用于LB之间的心跳连接 | |
VIP | 暂无 | ||
LAN不需要配置网关 |
具体的网卡参数配置以MASTER的3块网卡为例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DEVICE=eth0 HWADDR=00:0c:29:e7:ba:c6 TYPE=Ethernet UUID=af441a22-3ba9-42fe-9748-eeb78eccb374 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none USERCTL=no IPV6INIT=no IPADDR=192.168.1.19 NETMASK=255.255.255.0 GATEWAY=192.168.1.1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DEVICE=eth1 HWADDR=00:0c:29:e7:ba:bc TYPE=Ethernet ONBOOT=yes BOOTPROTO=none IPADDR=10.0.0.19 NETMASK=255.255.255.0
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DEVICE=eth2 HWADDR=00:0c:29:e7:ba:d0 TYPE=Ethernet ONBOOT=yes BOOTPROTO=none IPADDR=10.0.2.19 NETMASK=255.255.255.0
RSV
名称 | 接口 | IP | 用途 |
RS1 | eth0 | 192.168.1.17 | 用于WAN数据转发 |
eth1 | 10.0.0.17 | 用于LAN数据转发 | |
RS2 | eh0 | 192.168.1.18 | 用于WAN数据转发 |
eth1 | 10.0.0.18 | 用于LAN数据转发 | |
VIP | 暂无 |
具体的网卡参数配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DEVICE=eth0 HWADDR=00:0c:29:a4:65:08 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none DNS2=8.8.8.8 DNS1=114.114.114.114 IPV6INIT=no USERCTL=no IPADDR=192.168.1.17 NETMASK=255.255.255.0 GATEWAY=192.168.1.1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DEVICE=eth1 HWADDR=00:0c:29:a4:65:12 TYPE=Ethernet ONBOOT=yes BOOTPROTO=none IPADDR=10.0.0.17 NETMASK=255.255.255.0 ~
配置调度器
Master
vim /etc/keepalived/keepalived.conf
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
! Configuration File for keepalived global_defs { notification_email { zy5724@163.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_01 } vrrp_instance VI_1 { state MASTER #备份服务器上MASTER为BACKUP interface eth0 lvs_sync_daemon_inteface eth2 #心跳线的网卡 virtual_router_id 51 priority 100 #备份服务上优先级要低于100,如改为90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.29/24 #注意,这是是模拟外网IP。 } } # virtual_server 192.168.1.29 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 #会话保持 protocol TCP real_server 192.168.1.17 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.1.18 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
BACKUP
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
! Configuration File for keepalived global_defs { notification_email { zy5724@163.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_02 } vrrp_instance VI_1 { state BACKUP #备份服务器上MASTER为BACKUP interface eth0 lvs_sync_daemon_inteface eth2 virtual_router_id 51 priority 90 #备份服务上优先级要低于100,如改为90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.29/24 #注意,这是是模拟外网IP。 } } # virtual_server 192.168.1.29 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.1.17 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.1.18 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
启动keepalived,查看LVS情况
配置RealServer
RS1
ifconfig lo:17 192.168.1.29 netmask 255.255.255.255 up route add -host 192.168.1.29 dev lo 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
RS2
ifconfig lo:18 192.168.1.29 netmask 255.255.255.255 up route add -host 192.168.1.29 dev lo 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
访问http://192.168.1.29,可以发现实现了高可用和负载均衡。