How to configure floating IP on CentOS
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 to find out how to get this done on CentOS servers.
As an example of a public floating IP, server 1 (126.96.36.199) and the server 2 (188.8.131.52) have a common floating IP 184.108.40.206, netmask 255.255.252.0 and gateway 220.127.116.11. 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.
Set up static IP address
Check your current network settings with the following command.
Commonly the second network interface card (NIC) named eth0, highlighted in red, has your public IPv4 address assigned to it. For private IP, the NIC is likely called eth1 as highlighted in purple. See the example below for both options.
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 6e:d7:1b:bf:3a:5f brd ff:ff:ff:ff:ff:ff inet 18.104.22.168/22 brd 22.214.171.124 scope global eth0 valid_lft 53810sec preferred_lft 53810sec 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 6e:d7:1b:bf:de:ca brd ff:ff:ff:ff:ff:ff inet 10.2.10.25/22 brd 10.2.11.255 scope global noprefixroute dynamic eth1
In addition to the IP address, you will need the default public gateway which can be found with the command underneath. Private floating IP doesn’t require this.
default via 126.96.36.199 dev eth0 10.0.0.0/8 via 10.1.4.1 dev eth1 10.1.4.0/22 dev eth1 proto kernel scope link src 10.1.7.180 188.8.131.52/22 dev eth0 proto kernel scope link src 184.108.40.206
Next, set the address to static on the NIC with your public IPv4, eth0 in this case. Alternatively, you’ll likely need to edit the eth1 if you are configuring a private floating IP
On CentOS hosts, each NIC is controlled by its own configuration file.
Static public IP
To edit the public address at eth0 for example, use the command underneath.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
You’ll need to change the bootproto from dhcp to static, then add the IP address, netmask and gateway to the end.
DEVICE=eth0 BOOTPROTO=static ONBOOT=yes IPADDR=220.127.116.11 NETMASK=255.255.252.0 GATEWAY=18.104.22.168
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
Static private IP
In the case of a private IP, edit the eth1 with the following command.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth1
You’ll need to change the bootproto from dhcp to static, then add the IP address and netmask.
DEVICE=eth1 BOOTPROTO=static ONBOOT=yes IPADDR=10.2.10.25 NETMASK=255.255.252.0
Then save the changes and exit. You will next need to restart the interface to enable the static configuration.
sudo ifdown eth1 && sudo ifup eth1
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 the floating IP
To set up the floating IP, you’ll need to add it as an alias to the network interface with your static IP configured to.
The easiest way to do this is to duplicate the configuration file of the NIC with your static IP, this way you don’t need to write the whole file from scratch.
Public floating IP
For example, with eth0 you can use the following command.
sudo cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:1
Then edit the new alias interface.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0:1
Add the same “:1” to the device name, enter a new parameter NM_CONTROLLED=no, replace the IP address with the floating IP and then remove the gateway line.
DEVICE=eth0:1 BOOTPROTO=static ONBOOT=yes NM_CONTROLLED=no IPADDR=22.214.171.124 NETMASK=255.255.252.0
Save the file and exit after these changes.
Private floating IP
With private floating IP, make a copy of eth1 using the following command.
sudo cp /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1:1
Then edit the new alias interface.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth1:1
Add the same “:1” to the device name, enter a new parameter NM_CONTROLLED=no, replace the IP address with the floating IP.
DEVICE=eth1:1 BOOTPROTO=static ONBOOT=yes NM_CONTROLLED=no IPADDR=10.2.9.108 NETMASK=255.255.252.0
Then save the changes and exit the editor.
Repeat the process to add the alias on any other servers you wish to use the floating IP on.
Enabling traffic to the floating IP
With the new alias interfaces set up, check which server the floating IP is attached to under the network list at your UpCloud control panel. You should then turn on the alias NIC on that server. In this public floating IP example using the following command.
sudo ifup eth0:1
With a private floating IP, you’ll need to call the eth1:1 NIC instead.
sudo ifup eth1:1
Check your network configuration again with the same command as before.
The newly added alias shows up under the NIC with your public IP with the floating IP.
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 6e:d7:1b:bf:3a:5f brd ff:ff:ff:ff:ff:ff inet 126.96.36.199/22 brd 188.8.131.52 scope global eth0 valid_lft forever preferred_lft forever inet 184.108.40.206/22 brd 220.127.116.11 scope global secondary eth0:1 valid_lft forever preferred_lft forever
Or with under eth1 in the case of a private floating IP.
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 6e:d7:1b:bf:de:ca brd ff:ff:ff:ff:ff:ff inet 10.2.10.25/22 brd 10.2.11.255 scope global noprefixroute eth1 valid_lft forever preferred_lft forever inet 10.2.9.108/22 brd 10.2.11.255 scope global secondary eth1:1 valid_lft forever preferred_lft forever inet6 fe80::6cd7:1bff:febf:deca/64 scope link 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.
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, 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, on CentOS servers use the following command.
sudo ifdown eth0:1
Then turn on the alias NIC on your other server.
sudo ifup eth0:1
Enabling the alias on a new host running CentOS will automatically direct any connections through the floating IP to that server.
Test the floating IP again with any method you prefer. When you get a connection you have successfully transferred the floating IP.
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.