Tutorials How to configure floating IP on Ubuntu

How to configure floating IP on Ubuntu

Once you get a confirmation from the UpCloud Support that a floating IP has been added, whether public or private, you can find it attached to one of the servers at your UpCloud control panel as requested. However, using the new floating IP will require some manual setup. Follow the steps below on how to get this done on Ubuntu servers.

As an example of a public floating IP, server 1 (185.20.139.167) and the server 2 (185.20.139.162) have a common floating IP 185.20.139.29, netmask 255.255.252.0 and gateway 185.20.136.1. These are highlighted below in red.

In case you wish to configure a private floating IP, see the example with server 1 (10.2.9.231), server 2 (10.2.10.25), private floating IP 10.2.9.108, netmask 255.255.252.0 and gateway 10.2.8.1. Follow the instructions underneath highlighted in purple.

Setting up static public IP address

Firstly, you’ll need to configure the servers at the OS level, so start up your servers at your UpCloud control panel and log in.

If you are configuring a private floating IP, skip this part.

Check your current network settings with the following command.

ip addr

Commonly the second network interface card (NIC) named eth0 has your public IPv4 address assigned to it, see the example below.

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 6e:d7:1b:bf:5a:b0 brd ff:ff:ff:ff:ff:ff
    inet 185.20.139.167/22 brd 80.69.175.255 scope global eth0
    valid_lft forever preferred_lft forever

In addition to the IP address, you will need the default gateway and netmask. These can be found with the command underneath.

route -n
Kernel IP routing table
Destination  Gateway       Genmask        Flags  Metric  Ref  Use  Iface
0.0.0.0      185.20.136.1  0.0.0.0        UG     0       0    0    eth0
10.0.0.0     10.1.4.1      255.0.0.0      UG     0       0    0    eth1
10.1.4.0     0.0.0.0       255.255.252.0  U      0       0    0    eth1
185.20.136.0  0.0.0.0      255.255.252.0  U      0       0    0    eth0

Next, set the address as static on the NIC with your public IPv4, eth0 in this case. You can do this by editing the interfaces configuration file.

sudo nano /etc/network/interfaces

At the settings group that starts with auto eth0, change the dhcp to static and add the address, netmask and gateway as shown in the example below. Replace the IPs highlighted in the example below with your corresponding addresses.

auto eth0
iface eth0 inet static
address 185.20.139.167
netmask 255.255.252.0
gateway 185.20.136.1

After making the changes, save the file and exit. You will then need to restart the interface to enable the static configuration.

sudo ifdown eth0 && sudo ifup eth0

If you were connected with SSH, the interface restart should be fast enough to not cause you to disconnect. In case you do loose connection and are unable to reconnect, you can always use the web Console at UpCloud control panel under your Server settings to go through the setup again to make sure everything is entered correctly.

Repeat the steps on your other server to configure it with static IP address as well.

Configuring public floating IP

To enable the floating IP, add it as an alias to the network interface eth0 with your public static IP. On Ubuntu servers, this can be done by adding eth0:1 to the same interfaces file.

sudo nano /etc/network/interfaces

Enter the example configuration shown below with your floating IP and netmask. The address highlighted in red below is an example, replace it your floating IP.

auto eth0:1
iface eth0:1 inet static
address 185.20.139.29
netmask 255.255.252.0

Save the file and exit after these changes.

Repeat the process to add the alias on any other servers you wish to use the floating IP on.

Configuring private floating IP

Enabling a private floating IP only requires you to add it as an alias to the private network interface, commonly eth1. On Ubuntu servers, this can be done by adding eth1:1 to the interfaces file.

First, find the correct network interface by checking your IPs. Look for the NIC with your private IP address e.g. 10.2.9.231.

ip addr
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 6e:d7:1b:bf:de:ca brd ff:ff:ff:ff:ff:ff
    inet 10.2.9.231/22 brd 10.2.11.255 scope global eth1
    valid_lft forever preferred_lft forever

Next, open the network configuration file for edit.

sudo nano /etc/network/interfaces

Enter the alias configuration with your floating private IP and netmask. The address highlighted in red below is an example, replace it your floating private IP.

auto eth1:1
iface eth1:1 inet static
address 10.2.9.108
netmask 255.255.252.0

Then save the file and exit.

Repeat the process to add the alias on any other servers you wish to use the floating private IP on.

Enabling traffic to floating IP

With the new alias interfaces set up, check which server the floating IP is attached to under the Public network list at your UpCloud control panel. You should then turn on the alias NIC on that server, in this case using the following command on your Ubuntu host.

sudo ifup eth0:1

Alternatively, if you are setting up a private floating IP, use the NIC you identified above.

sudo ifup eth1:1

Check your network configuration again with the same command as before.

ip addr

The newly added alias shows up under the NIC with your public IP and the floating IP.

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 6e:d7:1b:bf:30:50 brd ff:ff:ff:ff:ff:ff
    inet 185.20.139.167/22 brd 185.20.139.255 scope global eth0
    valid_lft forever preferred_lft forever
    inet 185.20.139.29/22 brd 185.20.139.255 scope global secondary eth0:1
    valid_lft forever preferred_lft forever

With private floating IP, the output is very similar.

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 6e:d7:1b:bf:f8:6c brd ff:ff:ff:ff:ff:ff
    inet 10.2.9.231/22 brd 10.2.3.255 scope global eth1
    valid_lft forever preferred_lft forever
    inet 10.2.9.108/22 brd 10.2.3.255 scope global secondary eth1:1
    valid_lft forever preferred_lft forever

Now attempt to connect to your server through the floating IP with SSH or if you have a web server configured open the floating IP on your web browser. For private floating IPs, you’ll need to test it from another UpCloud server on your account, for example, using ping.

If you get a reply, the floating IP works on that server and you can continue forward. If it didn’t work, make sure you entered the IP address and netmask correctly, the alias network interface was raised successfully, that your firewall isn’t blocking your connections, or try another method to connect.

Testing the configuration

Your configuration should now be complete, you can test that it works by transferring the floating IP from one server to another.

To do this, turn off the floating IP alias NIC on the server it’s currently attached to by using the following command.

sudo ifdown eth0:1

Then turn on the alias NIC on your other server.

sudo ifup eth0:1

With a private floating IP, just turn off and on the correct alias interface using the same commands, for example, eth1:1.

Lastly, update the gateway on the new floating IP destination server with the following.

arping -C 1 -S floating_ip gateway

Replace the floating IP and gateway with your corresponding IP addresses. In this example, the command would be the following.

sudo arping -C 1 -S 185.20.139.29 185.20.136.1

When using a private floating IP, use the same command with your private IP address and gateway instead.

sudo arping -C 1 -S 10.2.9.108 10.2.8.1

Test the floating IP again with any method you prefer. When you get a connection you have successfully transferred the floating IP.

If you didn’t have arping already installed on your server, get it with the command below.

sudo apt install arping

Then try running the arping command again.

Using your new floating IP

You can now transfer the floating IP between your cloud servers by turning off the alias NIC on the current server, and then turning on the alias on the new destination host.

Depending on your intended use case for the floating IP you may wish to continue by setting up automated load balancing, but if you wish to manually transfer the traffic between your servers, it might be useful to create scripts of the required commands.

One thought on “How to configure floating IP on Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *

Locations

Helsinki (HQ)

In the capital city of Finland, you will find our headquarters, and our first data centre. This is where we handle most of our development and innovation.

London

London was our second office to open, and a important step in introducing UpCloud to the world. Here our amazing staff can help you with both sales and support, in addition to host tons of interesting meetups.

Seattle

Seattle is our 4th and latest office to be opened, and our way to reach out across the pond to our many users in the Americas.

Singapore

Singapore was our 3rd office to be opened, and enjoys one of most engaged and fastest growing user bases we have ever seen.