Eu tenho 3 placas de rede. NIC1 e NIC2 são usados para conexões de internet e uma LAN é usada para conexão de intranet.
Quero equilibrar a carga para ISPs usando tabelas IP. Como sou novato, minha tentativa grosseira é dada no código a seguir, obviamente sem sucesso. Este código foi montado consultando vários sites sobre o assunto.
gostaríamos de usar de forma otimizada a entrada de Internet na LAN usando o método Round-Robin
ou
É possível dividir os dois ISPs, ou seja, para os sistemas 1 a 5 (ISP1) e 5 a 10 (ISP2) utilizando tabelas de roteamento?
Espero que alguém me ajude, o que é muito apreciado.
set -x
IPT="/sbin/iptables"
NIC1="enp4s5f0"
NIC2="enp4s5f1"
LAN="ens2"
ISP1="192.168.9.33"
ISP2="192.168.10.33"
#***************
# reset iptables
#_______________
## reset the default policies in the filter table.
$IPT -P INPUT ACCEPT
$IPT -P FORWARD ACCEPT
$IPT -P OUTPUT ACCEPT
## reset the default policies in the nat table.
$IPT -t nat -P PREROUTING ACCEPT
$IPT -t nat -P POSTROUTING ACCEPT
$IPT -t nat -P OUTPUT ACCEPT
## reset the default policies in the mangle table.
$IPT -t mangle -P PREROUTING ACCEPT
$IPT -t mangle -P OUTPUT ACCEPT
## flush all the rules in the filter and nat tables.
$IPT -F
$IPT -t nat -F
$IPT -t mangle -F
## erase all chains that's not default in filter and nat table.
$IPT -X
$IPT -t nat -X
$IPT -t mangle -X
#*********************
# enable IP forwarding
#_____________________
echo 1 > /proc/sys/net/ipv4/ip_forward
#****************
# rules
#________________
$IPT -A INPUT -m state --state INVALID -j DROP
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A INPUT -p icmp -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$IPT -A FORWARD -m state --state INVALID -j DROP
$IPT -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A FORWARD -p icmp -j ACCEPT
$IPT -t nat -A PREROUTING -i NIC1 -p tcp --dport 80\
-m state --state NEW -m statistic --mode\
random --probability .33 -j DNAT --to-destination 10.1.1.1:1
$IPT -t nat -A PREROUTING -i NIC2 -p tcp --dport 80\
-m state --state NEW -m statistic --mode\
random --probability .33 -j DNAT --to-destination 10.1.1.1:2
$IPT -A FORWARD -i $NIC1 -o $LAN -j ACCEPT
$IPT -A FORWARD -i $NIC2 -o $LAN -j ACCEPT
# ifconfig -a
#********************************
# allow certain hosts full access
#________________________________
allowHost() {
$IPT -A FORWARD -i $LAN -s $1 -j ACCEPT
}
allowHost 192.168.2.10
allowHost 192.168.2.11
allowHost 192.168.2.12
allowHost 192.168.2.13
allowHost 192.168.2.14
allowHost 192.168.2.15
####Extra
$IPT -A FORWARD -d 8.8.8.8 -p tcp -m multiport --dports 53 -j ACCEPT
#********************
# block anything else
#____________________
$IPT -A FORWARD -j LOG -m limit --limit 10/minute --limit-burst 1 --log-prefix "Blocked:"
$IPT -A FORWARD -j DROP
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_nat_ftp
/sbin/modprobe iptable_nat
Responder1
AVISO: LEIA PRIMEIRO ANTES DE EXECUTAR
AVISO LEGAL: Não testado
Pré-requisitos:
Instale o pacote persistente iptables
sudo apt-get install iptables-persistent
Faça backup de suas regras existentes do iptables:
sudo iptables-save > ~/iptables-export
Restaurar:
sudo iptables-restore < ~/iptables-export
Libere as antigas regras do iptables:
sudo iptables -F && sudo iptables -t nat -F && sudo iptables -t mangle -F && sudo iptables -X && sudo iptables -t nat -X && sudo iptables -t mangle -X
Passos:
Adicione uma nova "tabela de rotas" usando este comando do seu terminal para manipular os pacotes de uma das NICs:
sudo bash -c 'echo "1 rt2" >> /etc/iproute2/rt_tables'
Habilitar encaminhamento ipv4
sudo sysctl net.ipv4.ip_forward=1 sudo sysctl -p
Adicionar filtro que permite "mascarar"
sudo iptables -t nat -A PREROUTING -i enp4s5f1 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.90 sudo iptables -t nat -A PREROUTING -i enp4s5f0 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.90 sudo iptables -t nat -A POSTROUTING -o ens2 -j MASQUERADE sudo netfilter-persistent save sudo netfilter-persistent reload
Edite o
/etc/network/interfaces
arquivo e adicione a configuração para a segunda NIC, mas é clarofaça uma cópia do antigo,sudo cp /etc/network/interfaces /etc/network/interfaces.bkup
:iface enp4s5f1 inet static address <YOUR_NIC_ADD>/24 netmask 255.255.255.0 post-up ip route add 192.168.10.0/24 dev enp4s5f1 src 192.168.10.33 table rt2 post-up ip route add default via 192.168.10.1 dev enp4s5f1 table rt2 post-up ip rule add from 192.168.10.33/32 table rt2 post-up ip rule add to 192.168.10.33/32 table rt2
Reinicie o gerenciador de rede:
sudo systemctl restart NetworkManager.service
Adicione regras de iptable:
sudo iptables -t nat -A PREROUTING -i enp4s5f0 -m state --state NEW -m statistic --mode nth --every 5 --packet 0 -j DNAT --to-destination 192.168.2.26 sudo iptables -t nat -A PREROUTING -i enp4s5f0 -m state --state NEW -m statistic --mode nth --every 4 --packet 0 -j DNAT --to-destination 192.168.2.27 sudo iptables -t nat -A PREROUTING -i enp4s5f0 -m state --state NEW -m statistic --mode nth --every 3 --packet 0 -j DNAT --to-destination 192.168.2.28 sudo iptables -t nat -A PREROUTING -i enp4s5f0 -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j DNAT --to-destination 192.168.2.29 sudo iptables -t nat -A PREROUTING -i enp4s5f0 --to-destination 192.168.2.30
=====================================
sudo iptables -t nat -A PREROUTING -i enp4s5f1 -m state --state NEW -m statistic --mode nth
--every 5 --packet 0 -j DNAT --to-destination 192.168.2.21
sudo iptables -t nat -A PREROUTING -i enp4s5f1 -m state --state NEW -m statistic --mode nth
--every 4 --packet 0 -j DNAT --to-destination 192.168.2.22
sudo iptables -t nat -A PREROUTING -i enp4s5f1 -m state --state NEW -m statistic --mode nth
--every 3 --packet 0 -j DNAT --to-destination 192.168.2.23
sudo iptables -t nat -A PREROUTING -i enp4s5f1 -m state --state NEW -m statistic --mode nth
--every 2 --packet 0 -j DNAT --to-destination 192.168.2.24
sudo iptables -t nat -A PREROUTING -i enp4s5f1 -j DNAT --to-destination 192.168.2.25
sudo netfilter-persistent save
sudo netfilter-persistent reload
Fonte:
https://scalingo.com/articles/2018/04/20/iptables.html
https://www.webair.com/community/simple-stateful-load-balancer-with-iptables-and-nat/
http://ipset.netfilter.org/iptables-extensions.man.html
https://www.linuxquestions.org/linux/answers/Networking/Spanning_Multiple_DSLs
Como salvar regras do iptables?
https://linuxconfig.org/how-to-restart-network-on-ubuntu-16-04-xenial-xerus-linux