Senin, 03 Februari 2014

ECMP load balancing with masquerade

From MikroTik Wiki

Introduction

This example is improved (different) version of round-robin load balancing example. It adds persistent user sessions, i.e. a particular user would use the same source IP address for all outgoing connections. Consider the following network layout:


Quick Start for Impatient
Configuration export from the gateway router:
/ ip address
add address=192.168.0.1/24 network=192.168.0.0 broadcast=192.168.0.255 interface=Local
add address=10.111.0.2/24 network=10.111.0.0 broadcast=10.111.0.255 interface=wlan2
add address=10.112.0.2/24 network=10.112.0.0 broadcast=10.112.0.255 interface=wlan1

/ ip route
add dst-address=0.0.0.0/0 gateway=10.111.0.1,10.112.0.1 check-gateway=ping

/ ip firewall nat
add chain=srcnat out-interface=wlan1 action=masquerade
add chain=srcnat out-interface=wlan2 action=masquerade

/ ip firewall mangle
add chain=input in-interface=wlan1 action=mark-connection new-connection-mark=wlan1_conn
add chain=input in-interface=wlan2 action=mark-connection new-connection-mark=wlan2_conn
add chain=output connection-mark=wlan1_conn action=mark-routing new-routing-mark=to_wla1
add chain=output connection-mark=wlan1_conn action=mark-routing new-routing-mark=to_wla2

/ ip route
add dst-address=0.0.0.0/0 gateway=10.111.0.1 routing-mark=to_wla1
add dst-address=0.0.0.0/0 gateway=10.111.0.2 routing-mark=to_wla2
Explanation
First we give a code snippet and then explain what it actually does.
IP Addresses
/ ip address
add address=192.168.0.1/24 network=192.168.0.0 broadcast=192.168.0.255 interface=Local
add address=10.111.0.2/24 network=10.111.0.0 broadcast=10.111.0.255 interface=wlan2
add address=10.112.0.2/24 network=10.112.0.0 broadcast=10.112.0.255 interface=wlan1
The router has two upstream (WAN) interfaces with the addresses of 10.111.0.2/24 and 10.112.0.2/24. The LAN interface has the name "Local" and IP address of 192.168.0.1/24.

NAT
/ ip firewall nat
add chain=srcnat out-interface=wlan1 action=masquerade
add chain=srcnat out-interface=wlan2 action=masquerade
As routing decision is already made we just need rules that will fix src-addresses for all outgoing packets. if this packet will leave via wlan1 it will be NATed to 10.112.0.2/24, if via wlan2 then NATed to 10.111.0.2/24

Routing
/ ip route
add dst-address=0.0.0.0/0 gateway=10.111.0.1,10.112.0.1 check-gateway=ping
This is typical ECMP (Equal Cost Multi-Path) gateway with check-gateway. ECMP is "persistent per-connection load balancing" or "per-src-dst-address combination load balancing". As soon as one of the gateway will not be reachable, check-gateway will remove it from gateway list. And you will have a "failover" effect.

You can use asymmetric bandwidth links also - for example one link is 2Mbps other 10Mbps. Just use this command to make load balancing 1:5
/ ip route
add dst-address=0.0.0.0/0 gateway=10.111.0.1,10.112.0.1,10.112.0.1,10.112.0.1,10.112.0.1,10.112.0.1 check-gateway=ping

Connections to the router itself
/ ip firewall mangle
add chain=input in-interface=wlan1 action=mark-connection new-connection-mark=wlan1_conn
add chain=input in-interface=wlan2 action=mark-connection new-connection-mark=wlan2_conn
add chain=output connection-mark=wlan1_conn action=mark-routing new-routing-mark=to_wlan1
add chain=output connection-mark=wlan1_conn action=mark-routing new-routing-mark=to_wlan2
/ ip route
add dst-address=0.0.0.0/0 gateway=10.111.0.1 routing-mark=to_wlan1
add dst-address=0.0.0.0/0 gateway=10.111.0.2 routing-mark=to_wlan2
With all multi-gateway situations there is a usual problem to reach router from public network via one, other or both gateways. Explanations is very simple - Outgoing packets uses same routing decision as packets that are going trough the router. So reply to a packet that was received via wlan1 might be send out and masqueraded via wlan2.
To avoid that we need to policy routing those connections.


Known Issues
DNS issues
ISP specific DNS servers might have custom configuration that treats specific requests from ISP's network differently than requests from other network. So in case connection is made via other gateway those sites will not be accessible.
To avoid that we suggest to use 3rd-party (public) DNS servers, and in case you need ISP specific recourse, create static DNS entry and policy route that traffic to specific gateway.
Routing table flushing
Every time when something triggers flush of the routing table and ECMP cache is flushed. Connections will be assigned to gateways once again and may or may not be on the same gateway.(in case of 2 gateways there are 50% chance that traffic will start to flow via other gateway).

If you have fully routed network (clients address can be routed via all available gateway), change of the gateway will have no ill effect, but in case you use masquerade, change of the gateway will result in change of the packet's source address and connection will be dropped.

Routing table flush can be caused by 2 things:
1) routing table change (dynamic routing protocol update, user manual changes)
2) every 10 minutes routing table is flushed for security reasons (to avoid possible DoS attacks)
So even if you do not have any changes of routing table, connections may jump to other gateway every 10 minutes


============================================

Ok.. coba kita lihat versi yang hampir sama dengan artikel diatas, tetapi versi ini versi implementasi langsung (versi diatas adalah versi buku)

Ceritanya disini saya mempunyai koneksi utama link dari isp Powertel, kemudian link speedy sebagai link kedua.

Kedua isp dapat saling membackup. Jadi user harusnya bisa selalu on kecuali bila router kita mati atau kedua isp juga mati bersamaan



Yang perlu dilakukan hanya cara :
1. setting IP
2. Setting firewall mangle
Tujuannya untuk menandakan / proses cek apakah ada link yang down
3. Setting routing
4. Setting NAT

Berikut detail dari settingan tsb

1. Settingan IP

[richard@MikroTik] > ip address print
Flags: X - disabled, I - invalid, D - dynamic
# ADDRESS NETWORK BROADCAST INTERFACE
0 250.124.50.50/30 250.124.50.48 250.124.50.51 PowerTel
1 192.168.50.49/30 192.168.50.48 192.168.50.51 Sepidol
2 192.168.0.1/25 192.168.0.0 192.168.0.127 ether3
[richard@MikroTik] >


2. Setting Frewall Mangle

[richard@MikroTik] > /ip firewall mangle print
Flags: X - disabled, I - invalid, D - dynamic
0 chain=input in-interface=Sepidol action=mark-connection
new-connection-mark=Sepidol_conn passthrough=yes

1 chain=input in-interface=PowerTel action=mark-connection
new-connection-mark=PowerTel_conn passthrough=yes

2 chain=output connection-mark=Sepidol_conn action=mark-routing
new-routing-mark=to_wla1 passthrough=yes

3 chain=output connection-mark=PowerTel_conn action=mark-routing
new-routing-mark=to_wla2 passthrough=yes
[richard@MikroTik] >


3. Setting Route:

[richard@MikroTik] > ip route print detail
Flags: X - disabled, A - active, D - dynamic, C - connect, S - static, r - rip, b - bgp, o - ospf
0 ADC dst-address=192.168.0.0/25 pref-src=192.168.0.1 interface=ether3 scope=10 target-scope=0

1 ADC dst-address=192.168.50.48/30 pref-src=192.168.50.49 interface=Sepidol scope=10 target-scope=0

2 ADC dst-address=250.124.50.48/30 pref-src=250.124.50.50 interface=PowerTel scope=10 target-scope=0

3 A S dst-address=0.0.0.0/0 gateway=250.124.50.49,250.124.50.49,192.168.50.50 check-gateway=ping
interface=PowerTel,PowerTel,Sepidol gateway-state=reachable,reachable,reachable scope=255
target-scope=10

4 A S dst-address=0.0.0.0/0 gateway=250.124.50.49 interface=PowerTel gateway-state=reachable scope=255
target-scope=10 routing-mark=to_wla1

5 A S dst-address=0.0.0.0/0 gateway=192.168.50.50 interface=Sepidol gateway-state=reachable scope=255
target-scope=10 routing-mark=to_wla2
[richard@MikroTik] >

4. Setting Nat

[richard@MikroTik] > ip firewall nat print
Flags: X - disabled, I - invalid, D - dynamic
0 chain=srcnat out-interface=Sepidol action=masquerade

1 chain=srcnat out-interface=PowerTel action=masquerade
[richard@MikroTik] >

Sabtu, 01 Februari 2014

Load Balancing mikrotik Menggunakan ecmp



pada artikel sebelumnya telah di bahas mengenai load balancing menggunakan NTH, kali ini saya akan mencoba membuat load balancing menggunakan metode ecmp khusunya pada konfigurasi firewall mangle dan route,  berikut contoh mangle dan routing pada penggunaan load balancing untuk metode ecmp dengan menggunakan 2 upstream :

/ip firewall mangle
add action=mark-routing chain=prerouting comment="Route HTTP traffic to ECMP" \
    disabled=yes dst-port=80 new-routing-mark=ecmp-http-route passthrough=yes \
    protocol=tcp
add action=mark-connection chain=input disabled=yes in-interface=ether1 \
    new-connection-mark=ether1_conn passthrough=yes
add action=mark-connection chain=input disabled=yes in-interface=ether2 \
    new-connection-mark=ether2_conn passthrough=yes
add action=mark-routing chain=output connection-mark=ether1_conn disabled=yes \
    new-routing-mark=to_wlan1 passthrough=yes
add action=mark-routing chain=output connection-mark=ether1_conn disabled=yes \
    new-routing-mark=to_wlan2 passthrough=yes
setelah mangle ini di buat jangan lupa untuk membuat routing-mark pada ip route :
/ip route
add check-gateway=ping comment="ECMP route for HTTP" disabled=yes distance=1 \
    dst-address=0.0.0.0/0 gateway=[gateway uplink 1],[gateway uplink 2] routing-mark=\
    ecmp-http-route scope=30 target-scope=10
add disabled=no distance=1 dst-address=0.0.0.0/0 gateway= [gateway uplink 1]  \
    routing-mark=to_wlan1 scope=30 target-scope=10
add disabled=no distance=1 dst-address=0.0.0.0/0 gateway= [gateway uplink 2]  \
    routing-mark=to_wlan2 scope=30 target-scope=10
add comment="Default Route to Internet" disabled=no distance=1 dst-address=\
    0.0.0.0/0 gateway=10.10.10.1 scope=30 target-scope=10 
selamat mencoba dan happy blogging :)