赞
踩
内网:192.168.50.0/24
主路由:华硕AX86U,主路由做DHCP、AP,之前DHCP网关和DNS指向旁路由
旁路由:群晖虚拟机跑的OpenWrt,跑一些插件
习惯于用硬路由做主路由,软路由实现插件功能,主要原因有以下几点:
由于网关和DNS都指旁路由,当旁路由挂了,家里的网络就瘫痪了,这绝不能忍。
周末突然想到既然各种web应用服务可以负载均衡,为啥网络ip不行,于是发现神器keepalived。其实以前知道这个东西,只是不了解它的应用场景。这里顺便简单提一下三种负载均衡工具的区别:
这里将网关和DNS地址做负载来保证高可用。两台路由器安装keepalived,创建网络虚拟IP:192.168.50.5。将主路DHCP的网关和DNS配置成虚拟IP,两台路由器分别作为keepalived主备节点。实现旁路由挂了的情况,网络仍然可用。
这里使用Entware来安装,以下步骤使用ssh执行
1.安装Entware
不会还有人用华硕不用梅林固件[1]吧?如果不是则使用手动安装教程[2]
2.安装keepalived
opkg update
opkg install keepalived
3.备份默认配置
mv /opt/etc/keepalived/keepalived.conf /opt/etc/keepalived/keepalived.conf.bak
4.应用备用节点配置,注意替换下面2处参数值
cat > /opt/etc/keepalived/keepalived.conf <<-EOF global_defs { router_id LVS_1 } vrrp_instance VI_1 { interface br0 # 这里是lan口网卡名,通过ip a查看 state BACKUP virtual_router_id 1 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.50.5 # 这里是虚拟ip地址 } } EOF
5.启动程序
nohup /opt/sbin/keepalived -n -f /opt/etc/keepalived/keepalived.conf &
# 以下命令有回显则启动成功
ps | grep keepalived | grep -v grep
6.添加开机启动,以下为梅林固件添加方式,其他固件未测试
cat >> /jffs/scripts/post-mount <<-EOF
nohup /opt/sbin/keepalived -n -f /opt/etc/keepalived/keepalived.conf &
EOF
以下步骤使用ssh执行
1.安装keepalived
opkg update
opkg install keepalived
2.备份默认配置
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
3.应用主节点配置,注意替换下面2处参数值
cat > /opt/etc/keepalived/keepalived.conf <<-EOF global_defs { router_id LVS_1 } vrrp_instance VI_1 { interface br-lan # 这里是lan口网卡名,通过ip a查看 state MASTER virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.50.5 # 这里是虚拟ip地址,与主路由相同 } } EOF
4.启动程序
nohup /usr/sbin/keepalived -n -f /opt/etc/keepalived/keepalived.conf &
# 以下命令有回显则启动成功
ps | grep keepalived | grep -v grep
6.添加开机启动
sed -i '/exit 0/i\nohup \/usr\/sbin\/keepalived -n -f \/etc\/keepalived\/keepalived.conf &' /etc/rc.local
将局域网设备的默认网关和DNS Server都指向虚拟IP地址192.168.50.5
1.关机时ping有几s中断2.开机时ping也有几s的中断和延迟波动3.开机后由于keepalived主节点的权重设置的比备份节点高,所以请求成功切换到主节点,整体上体验还是比较满意的
这里我没有外部的AP,华硕主路由重启后WIFI丢失,无法测试。不过理论上,重启备份节点,虚拟IP持续可以访问。我这里暂时不需要双路由可用的场景,所以就不做测试了。
• 线路1:光猫 => 路由A keepalived => AP1 => 用户
• 线路2:光猫 => 路由B keepalived => AP1 => 用户
DHCP的网关和DNS配置需要配置为Keepalived的虚拟IP,这里有个问题,局域网只能有一台路由器开启DHCP,不过即使DHCP的节点挂了,也不影响网络的临时的可用性
[1]
梅林固件: https://github.com/RMerl/asuswrt-merlin.ng/wiki/Entware
[2]
手动安装教程: https://github.com/Entware/Entware/wiki/Install-on-Asus-stock-firmware
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。