SMarcell
(usa Slackware)
Enviado em 05/01/2021 - 12:08h
Você pode tentar dessa maneira. Estou pressupondo que nesse seu servidor, o iptables já esteja configurado e fazendo NAT de saída com os IPs dos seus ISPs (Vivo e Net).
#1 - Edite o arquivo "/etc/iproute2/rt_tables" e adicione as linhas:
1 rt_isp1 # Vivo
2 rt_isp2 # Net
#2 - Adicione ao arquivo "/etc/sysctl.conf" as linhas:
net.ipv4.fib_multipath_hash_policy=1
net.ipv4.conf.all.arp_filter=1
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.default.arp_filter=1
net.ipv4.conf.default.arp_ignore=1
net.ipv4.conf.default.arp_announce=2
net.ipv4.conf.all.rp_filter=2
net.ipv4.conf.default.rp_filter=2
#3 - Reinicie o servidor
#4 - Salve o script abaixo em "/usr/local/sbin" com o nome de "load-balance.sh" e edite estas variáveis do script:
GW_LINK1
GW_LINK2
IFACE_LINK1
IFACE_LINK2
#!/bin/sh
PATH='/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
export LC_ALL='POSIX'
# Variaveis de controle. Altere conforme necessario
TABLE_LINK1='rt_isp1' # Vivo
TABLE_LINK2='rt_isp2' # Net
GW_LINK1='xxx.xxx.xxx.xxx' # Gateway/Vivo
GW_LINK2='yyy.yyy.yyy.yyy' # Gateway/Net
IFACE_LINK1='ethX' # Interface/Vivo
IFACE_LINK2='ethY' # Interface/Net
start() {
echo "Starting load-balancing: $(which ip)"
# Populando as novas tabelas de roteamento
ip route add 127.0.0.0/8 dev lo table "$TABLE_LINK1"
ip route add 127.0.0.0/8 dev lo table "$TABLE_LINK2"
ip route show table main | grep -v '^default' | while IFS= read -r i; do
ip route add $i table "$TABLE_LINK1"
ip route add $i table "$TABLE_LINK2"
done
# Definindo os gateways padroes para as novas tabelas de roteamento
ip route add default via "$GW_LINK1" table "$TABLE_LINK1" proto static metric 1 quickack 1
ip route add default via "$GW_LINK2" table "$TABLE_LINK2" proto static metric 1 quickack 1
# Politicas de roteamento
ip addr show dev "$IFACE_LINK1" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | xargs -I {} ip rule add from {} table "$TABLE_LINK1"
ip addr show dev "$IFACE_LINK1" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | xargs -I {} ip rule add to {} table "$TABLE_LINK1"
ip addr show dev "$IFACE_LINK2" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | xargs -I {} ip rule add from {} table "$TABLE_LINK2"
ip addr show dev "$IFACE_LINK2" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | xargs -I {} ip rule add to {} table "$TABLE_LINK2"
# Removendo os gateways padroes da tabela principal...
ip route flush default
# ... e adicionando politica de rotas com multiplos caminhos atraves de balanceamento de conexoes
ip route add default proto static quickack 1 scope global nexthop via "$GW_LINK1" dev "$IFACE_LINK1" weight 1 nexthop via "$GW_LINK2" dev "$IFACE_LINK2" weight 1
# Limpando cache residual
ip route flush cache
return 0
}
stop() {
echo 'Stopping load-balancing'
# Limpando as politicas de roteamento
ip route flush default
for i in "$TABLE_LINK1" "$TABLE_LINK2"; do
ip route flush table $i
ip rule flush table $i
done
# Restaurando gateways na tabela principal
ip route add default via "$GW_LINK1" proto static metric 1 quickack 1
ip route add default via "$GW_LINK2" proto static metric 2 quickack 1
# Limpando cache residual
ip route flush cache
return 0
}
case "$1" in
'start') start;;
'stop') stop;;
'restart') stop; sleep 1; start;;
*) echo "Usage: $(basename $0) start|stop|restart"
esac
#5 - Dê permissão de execução ao script "load-balance.sh":
chmod 755 /usr/local/sbin/load-balance.sh
#6 - Inicialize o script:
/usr/local/sbin/load-balance.sh start
Seguindo os passos acima, você terá um balaceamento entre os links de seus ISPs.