# Example Configuration for keepalived You can use `keepalived` to implement automatic failover of a Floating IP between multiple servers. The following example has been tested using two Ubuntu 18.04 cloud servers at cloudscale.ch. For this example, install `keepalived`, `nginx`, `curl`, and `jq` and create the following two files on each server. Make sure to adjust the servers' IP addresses and UUIDs, the Floating IPs as well as the API token. /etc/keepalived/keepalived.conf: # Enable script execution global_defs { enable_script_security } # Define a shell command to check whether the service is running or not. vrrp_script service_check { script "/bin/systemctl is-active nginx" interval 2 } # Floating address configuration. vrrp_instance floating_ip { # VID of the VRRP group. virtual_router_id 1 # Interface used by this VRRP instance. interface ens3 # The IPv4 address of the other server. unicast_peer { } # Service check script used by this VRRP instance. track_script { service_check } # Script to assign the Floating IPs to this server when becoming master notify_master "/usr/local/bin/acquire-floating-ips" # VRRP priority of this server. The server with the highest priority # will become master. Use a different priority for each server within # the same VRRP group. priority 100 } /usr/local/bin/acquire-floating-ips: #!/usr/bin/env bash set -euo pipefail # API token (with write permission) to access the cloudscale.ch API. CLOUDSCALE_API_TOKEN="" # The Floating IPs to acquire. FLOATING_IPv4="" FLOATING_IPv6="" # The UUID of the server this script is running on. METADATA_URL="http://169.254.169.254/openstack/2017-02-22/meta_data.json" SERVER_UUID="$(curl -s $METADATA_URL | jq -r '.meta.cloudscale_uuid')" # Assigns the floating IP to this server. function acquire-ip { local ip="$1" curl -H "Authorization: Bearer $CLOUDSCALE_API_TOKEN" \ --form server="$SERVER_UUID" \ --fail \ "https://api.cloudscale.ch/v1/floating-ips/$ip" } # Acquire the Floating IP using linear back-off when the API is unavailable for address in $FLOATING_IPv4 $FLOATING_IPv6; do attempt=0 while ! acquire-ip $address; do attempt=$((attempt + 1)) if (( attempt > 10 )); then exit 1 fi sleep $attempt done done Make this script executable (`chmod +x /usr/local/bin/acquire-floating-ips`) and create the default user needed to execute scripts (`useradd --system --shell /bin/false keepalived_script`). Once you have created the two files as described above on each server you need to restart the `keepalived` service. The server with the highest priority will assign the Floating IPs to itself. If the service monitored by the check script fails or the server itself becomes unresponsive, another server will automatically take over. To manually acquire the Floating IPs, log in to a server and run `acquire-floating-ips`.