Layer 4 (LVS)
Load balance UDP with LVS and NAT
This page applies to:
- HAProxy ALOHA - all versions
In this section, we describe how to load balance UDP services with the embedded Linux Virtual Server (LVS) to perform the load balancing at layer 4. In this scenario, we set LVS to use its NAT mode to change the destination IP address in packets. We also use the NAT tab to change the source IP address in packets so that responses from servers return through HAProxy ALOHA (that is, not Direct Server Return).
Multiple ways to support UDP
HAProxy ALOHA also supports UDP load balancing on its LB Layer7 tab, which uses a custom UDP module. You might prefer the UDP module for better session tracking, logging, and statistics. You might prefer LVS if you require the utmost speed, since it routes UDP packets without any overhead.
HAProxy Enterprise also has UDP support
Looking for UDP load balancing support in HAProxy Enterprise instead? Visit the HAProxy Enterprise UDP load balancing page.
Resource usage
NAT load balancing introduces:
- a limit on the number of simultaneous connections to a backend server. Only 65534 source ports are available for HAProxy ALOHA addresses.
- a heavy load on HAProxy ALOHA (connection tracking must be enabled).
Enable UDP load balancing Jump to heading
To enable HAProxy ALOHA to load balance UDP at layer 4:
-
Configure LVS on the LB Layer4 tab so that it translates the destination IP from the public VIP on which HAProxy ALOHA listens to a backend server’s private IP. You do this in the
directorconfiguration section by settingmode natand setting one or moreserverdirectives.In this example, the VIP is
10.0.0.3and the backend server’s IP is10.0.0.20. The application listens at port 8000.haproxydirector web 10.0.0.3:8000 UDPbalance leastconnmode natserver web1 10.0.0.20:8000 weight 10 checkhaproxydirector web 10.0.0.3:8000 UDPbalance leastconnmode natserver web1 10.0.0.20:8000 weight 10 check -
Click OK and Apply.
-
Go to the NAT tab and click Insert to add a NAT rule. In the New Rule area, fill in the fields as described below to create a NAT rule that changes the client’s source IP to the HAProxy ALOHA appliance’s IP.
Field Description IN Inbound network interface. This indicates the interface of your VIP, where you receive traffic from clients. OUT Outbound network interface. If your appliance has only one network interface, set this to the same as the inbound interface. If your appliance has two interfaces and one of those interfaces is in a network containing the backend servers, use that interface for OUT. Protocol UDP. Before - Source Leave blank or set to any, which means match any source IP address.Before - Source port Leave blank or set to any, which means match any source port. Source ports are random.Before - Destination The VIP address that clients connect to, to match traffic destined for that address. Before - Destination port The UDP port of your application. You can set a single port like 8000, or a port range like 50000-51000. After - Source The VIP address to set as the source address on incoming packets. We recommend using a VIP instead of a physical IP address so that it’s highly available between HAProxy ALOHA appliances. When you have two network interfaces, you can create a new VIP for the private network containing your real servers, to better differentiate client-facing and server-facing traffic by having two, distinct VIPs. In contrast, a physical IP address can’t be shared between the members of a cluster. After - Source port Leave blank or set to same, since we won’t change the source port.After - Destination Leave blank or set to same, since we won’t change the destination IP address via the NAT tab. Destination NAT is handled by LVS.After - Destination port Leave blank or set to same, since we won’t change the destination port.Info
In configurations having two network interfaces, if you have failover configured on one VIP, configure a VIP with failover on the other interface as well.
-
Check your NAT configuration.
- One network interface (IN and OUT fields are the same, there is one VIP).

- Two network interfaces (IN and OUT fields differ, there are two VIPs).

-
Click Add and Apply.
-
To ensure that packets from the same client flow to the same backend server during a session, enable LVS connection tracking. Click the Services tab, locate the lvs service, and click Setup.
-
If the configuration contains the line
no autostart, delete the line to ensure that the LVS service starts automatically on boot. -
Enable connection tracking through the
conntrackkeyword.textservice lvs############ Linux Virtual Server, layer 3/4 load balancingconntracktextservice lvs############ Linux Virtual Server, layer 3/4 load balancingconntrack -
Click OK and then Close.
-
Locate the lvs service and click Restart.
-
To make the changes persist after a reboot, go to the Setup tab and click Save within the Configuration section.