Network
Policy-based routing
Available since
- HAProxy ALOHA 16.5
Policy-based routing allows you to route traffic by information other than just the destination IP address. It lets you route packets differently depending on the source IP and port, destination IP and port, and protocol. For example, you could match UDP or ICMP traffic and route it to a different network, or you could ensure that traffic exits via the same gateway where it arrived.
Flows and outflows
You can also use flows and outflows to accomplish custom routing.
Use a custom routing table Jump to heading
Normally, routing rules are grouped together into a default collection called the main routing table. To route IP packets differently depending on the source address, destination address, and/or protocol, you’ll create a custom routing table. By defining a selector that matches the traffic you want to affect, you can apply your custom routing table and the rules it contains only to matching packets.
-
In the Services tab, click network setup to display the current configuration of your network interfaces.
-
Add a rule through the following syntax:
textip rule <selector> <action>textip rule <selector> <action>For example, to have traffic exit by the same default gateway where it entered:
textservice network eth1ip address 10.20.10.15/24ip rule from 10.20.10.0/24 table 10ip route default 10.20.10.1 table 10textservice network eth1ip address 10.20.10.15/24ip rule from 10.20.10.0/24 table 10ip route default 10.20.10.1 table 10where:
- The
ip address
line sets a static IP address for the interface. - The
ip rule
line uses thefrom
argument to indicate that traffic that has a source IP in the range10.20.10.0/24
, including your application’s VIP, will use the rules defined in the custom routing table10
. - The
ip route
line adds a single rule to that table, which sets the default route to10.20.10.1
.
Note that you do not need to specify an
add
argument to add a rule, such asip rule add
, but justip rule
, since adding the line implies adding it, and deleting the line removes it.In a custom routing table, you can also add IP static routes. And you can add policy-based routing to VLAN and bonded interfaces.
- The
-
Click OK and Close.
-
On the Services tab, click Apply.
-
To make your changes persistent after a reboot, click the Setup tab. Then click Save under Configuration.
Verify your setup Jump to heading
To verify that your changes took effect:
-
Go to the Tools tab and click Launch terminal.
-
Check that the rule exists:
nixip rule listnixip rule listoutputtext0: from all lookup local32765: from 10.20.10.0/24 iif eth1 lookup 1032766: from all lookup main32767: from all lookup defaultoutputtext0: from all lookup local32765: from 10.20.10.0/24 iif eth1 lookup 1032766: from all lookup main32767: from all lookup default -
Check that the custom routing table contains the route. Here we are checking table
10
:nixip route show table 10nixip route show table 10outputtextdefault via 10.20.10.1 dev eth1outputtextdefault via 10.20.10.1 dev eth1
See also Jump to heading
- The online Linux manual describes the syntax of ip rule.
Do you have any suggestions on how we can improve the content of this page?