DigitalOcean fully manages Regional Load Balancers and Global Load Balancers, ensuring they are highly available load balancing services. Load balancers distribute traffic to groups of backend resources in specific regions or across different regions, which prevents the health of a service from depending on the health of a single server, cluster, or region.
Network load balancers route traffic at the network transport level, based on IP addresses and ports. You must update the network configuration of all backend Droplets to properly handle this traffic.
First, find the IP address of your network load balancer. The address is listed on the Load Balancers page in the control panel, or use doctl
to retrieve it with the following command:
doctl compute load-balancer list --format Name,IP
This outputs a table with load balancer names and IP addresses:
Name IP
nyc3-load-balancer-01 203.0.113.67
global-load-balancer-01
network-load-balancer 203.0.113.2
Note the correct load balancer IP address to use in the next steps.
Run the ip route add
command to add a route to the kernel’s IP routing table, using the IP address you retrieved in the previous step. Use eth0
for <your_interface>
if the load balancer is public, and eth1
if it’s a private, internal-only load balancer:
ip route add to local <your_loadbalancer_ip> dev <your_interface>
Remember to replace <your_loadbalancer_ip>
with the correct IP address.
This command creates a routing rule that directs traffic for the load balancer IP to the local system, regardless of its actual source. The system claims ownership of the IP address on the specified interface, ensuring that any incoming packets for the IP address are processed by the local machine, and any services listening on the IP address can handle this traffic.
Your Droplet can now handle traffic from the load balancer. Wait for the load balancer health checks to pass before testing connectivity.
Routes added with ip route add
do not persist across reboots. In the next step we set up systemd to run the command during the boot process.
There are many different ways to run the ip route add
command automatically when your Droplet boots. To do so with systemd, we set up a systemd unit file.
First, open the file with nano
or your preferred text editor:
sudo nano /etc/systemd/system/add-ip-route.service
Paste in the following, remembering to replace <your_loadbalancer_ip>
with the correct IP address, and <your_interface>
with eth0
for public load balancers or eth1
for private:
[Unit]
Description=Add IP Route for Load Balancer
After=network.target
[Service]
ExecStart=/sbin/ip route add to local <your_loadbalancer_ip> dev <your_interface>
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Save the file and quit your editor to return to the command prompt.
Use systemctl
to enable and then start the add-ip-route
service:
sudo systemctl enable add-ip-route
sudo systemctl start add-ip-route
You can check the status of the service with systemctl status
:
sudo systemctl status add-ip-route
The output should show enabled
and active (exited)
. The routing changes are now persistent across system reboots.