Implementation of a Hetzner Cloud High Availability Infrastructure with Keepalived

Implementation of Hetzner Cloud with Keepalived
This tutorial focuses on the steps to set up a Cloud High Availability (HA) cluster using floating IPs and Keepalived in Hetzner Cloud. 

 

Hetzner Cloud is a well-known Internet hosting company and data center operator from Gunzenhausen, Germany. Hetzner Cloud provides best-in-class performance with the help of Intel® Xeon® Gold processor, AMD EPYC second Generation, and speedy NVMe SSDs. Hetzner Cloud also offers services that are good to go in seconds with incredible performance. 

In this tutorial, we are going to set up a Cloud HA cluster using Keepalived and floating IPs. Keepalived is a Linux daemon that monitors services or systems and triggers a failover in case of an error. Keepalived facilities for high availability and load balancing to Linux-based infrastructures.

 

Requirements

  • One Floating IP
  • Minimum of two Cloud Servers

 

Recommendation 

Users should create a separate cloud project for the HA service since they must store critical access data in clear text on the servers. With a separate project, users can minimize the damage in the event of a possible compromise.

 

Set up a Cloud HA Cluster using Keepalived and Floating IPs

To set up a Cloud High Availability cluster using Keepalived and floating IPs in Hetzner Cloud, follow the below steps: 

 

Step 1: Set Up Automatic IP Re-routing

In this step, we will set up automatic failover, where the floating IP is automatically assigned to the other server to operate under the same address.

 

Step 1.1: Create a Cloud API Token 

The Cloud API token is required to control the floating IP assignment from the server. To create a read/write API token, follow the below steps:

  • First, log in to the Hetzner Cloud console and select a project.
  • Next, go to the Security section from the sidebar and click on API Tokens from the horizontal menu.
  • Click, Generate API Token.
  • Enter a description for the API token and select the Read & Write option as the permission.
  • Finally, click the Generate API Token button.

 

Implementation of Hetzner Cloud with Keepalived 1

 

 

  • In a pop-up window, the newly generated API token is displayed, store the API token in a secure place for future references. After saving the token, click the OK button.

 

 

 

Step 1.2: Create a Cloud Network

For the VRRP Heartbeat, a private network channel is required. In order to create a cloud network, follow the below steps:

  • Login to the Hetzner Cloud Console and go to the Networks section.
  • Click on the Create Network button.
  • Enter a name for the network and mention the IP range. Users can create a network with arbitrary size (larger 32) and assign it to the servers.
  • After filling in the details, click CREATE NETWORK.

 

Implementation of Hetzner Cloud with Keepalived 2

 

Step 1.3: Install IP Failover Software

We will install hcloud-ip, an open-source software responsible for provisioning the floating IP in this step. Users can either use the prebuilt binaries or compile the software themself. In order to install the software along with the prebuilt binaries, execute the following commands:

 

$ wget -O /opt/hcloud-ip https://github.com/FootprintDev/hcloud-ip/releases/download/v0.0.1/hcloud-ip-linux64
$ chmod +x /opt/hcloud

 

In order to compile the hcloud-ip software by yourself, follow the below steps:

1. First, install the dependencies by executing the following command:

Ubuntu/Debian: 
$ apt install git wget

 
CentOS/RHEL:
$ yum install git wget

 
Fedora:
$ dnf install git wget

 
SLES/OpenSUSE:
$ zypper install git wget

 

2. Next, install Golang by executing the following commands:

 

$ wget https://golang.org/dl/go1.16.2.linux-amd64.tar.gz
$ tar xfvz go1.16.2.linux-amd64.tar.gz
$ export PATH=$(pwd)/go/bin:$PATH

 

3. After that, clone the repository by executing the following command:

$ git clone https://github.com/FootprintDev/hcloud-ip /tmp/hcloud-ip && cd /tmp/hcloud-ip

 

4. Run the following command to build the project:

 

$ go build

 

5. Make the hcloud-ip folder executable and move the folder under /opt by running the following commands:

 

$ chmod +x hcloud-ip
$ mv hcloud-ip /opt

 

Step 1.4: Configure the Floating IP

For the floating IP to work on all Hetzner Cloud Servers in the event of a failover, it must be included in the network configuration. In order to do so, follow the below steps:

>> Note: If the user uses more than one floating IP, then the number on the interface (eth0:1) will be increased (example eth0:3).

 

Debian/Ubuntu versions before 20.04: 

1. First, access the server via SSH.

2. Create a configuration file and open the same using any of the text editors.

$ touch /etc/network/interfaces.d/60-my-floating-ip.cfg
$ nano /etc/network/interfaces.d/60-my-floating-ip.cfg

 

3. Paste the following configuration in the editor. Replace your.Float.ing.IP in the below content with the user’s floating IP.

 

IPv4: 
auto eth0:1
iface eth0:1 inet static
address your.Float.ing.IP
netmask 32

 
IPv6:
auto eth0:1
iface eth0:1 inet6 static
address one IPv6 address of the subnet, e.g. 2a01:4f9:0:2a1::2
netmask 64

 

 

4. After that, restart the network by executing the following command:

 

$ sudo service networking restart

 

 

Ubuntu 20.04: 

 1. First, access the server via SSH.

 2. Create a configuration file and open the same using any of the text editors.

 

$ touch /etc/netplan/60-floating-ip.yaml
$ nano /etc/netplan/60-floating-ip.yaml

 

3. Paste the following configuration in the editor. Replace your.Float.ing.IP in the below content with the user’s floating IP.

 

IPv4: 
network:
version: 2
ethernets:
 eth0:
   addresses:
   - your.float.ing.ip/32

IPv6:
network:
version: 2
ethernets:
 eth0:
   addresses:
   - your.float.ing.ip/64

 

 

4. After that, restart the network by executing the following command:

 

$ sudo netplan apply

 

 

Fedora/CentOS/RHEL:

 1. First, access the server via SSH.

 2. Create a configuration file and open the same using any of the text editors.

 

$ touch /etc/sysconfig/network-scripts/ifcfg-eth0:1
$ vi /etc/sysconfig/network-scripts/ifcfg-eth0:1

 

 3. Paste the following configuration in the editor. Replace your.Float.ing.IP in the below content with the user’s floating IP.

 

IPv4: 
 BOOTPROTO=static
 DEVICE=eth0:1
 IPADDR=your.Float.ing.IP
 PREFIX=32
 TYPE=Ethernet
 USERCTL=no
 ONBOOT=yes

IPv6:
 BOOTPROTO=none
 DEVICE=eth0:1
 ONBOOT=yes
 IPV6ADDR=one IPv6 address of the subnet, e.g. 2a01:4f9:0:2a1::2/64
 IPV6INIT=yes

 

 

 4. After that, restart the network by executing the following command:

 

$ systemctl restart network

 

 

Step 2: Set up Keepalived

Step 2.1: Install Keepalived

In order to install Keepalived, execute the following command.

 

Ubuntu / Debian:
$ apt install keepalived

CentOS / RHEL:
$ yum install keepalived

Fedora:
$ dnf instal keepalived

openSUSE / SLES:
zypper install keepalived

 

Step 2.2: Enable Keepalived AutoStart

 To enable Keepalived autostart, execute the following command:

 

Systemd based systems:
$ systemctl enable keepalived

CentOS / RHEL:
$ chkconfig keepalived on

 

Step 2.3: Configure Keepalived

 The below configuration corresponds to a sample using a High Availability webserver (nginx). To configure Keepalived, follow the below process: 

 

Configuration of the Master Server

Open the keepalived.conf file using any of the text editors and copy-paste the following content. Replace the outlined values [] with the user’s specifications.

 

 $ vim /etc/keepalived/keepalived.conf

 

vrrp_script chk_nginx {
    script "/usr/bin/pgrep nginx"
    interval 2
}

vrrp_instance VI_1 {
    interface [cloud_network_adapter]
    state MASTER
    priority 200

    virtual_router_id 30
    unicast_src_ip [master_private_IP]
    unicast_peer {
        [slave_private_IP]
    }

    authentication {
        auth_type PASS
        auth_pass [password]
    }

    track_script {
        chk_nginx
    }

    notify_master /etc/keepalived/failover.sh
}

 

 

 Configuration of the Slave Server

 Open the keepalived.conf file using any of the text editors and copy-paste the following content. Replace the outlined values [] with the user’s specifications.

 

 $ vim /etc/keepalived/keepalived.conf

 

vrrp_script chk_nginx {
    script "/usr/bin/pgrep nginx"
    interval 2
}

vrrp_instance VI_1 {
    interface [cloud_network_adapter]
    state SLAVE
    priority 100

    virtual_router_id 30
    unicast_src_ip [slave_private_IP]
    unicast_peer {
        [master_private_IP]
    }

    authentication {
        auth_type PASS
        auth_pass [password]
    }

    track_script {
        chk_nginx
    }

    notify_master /etc/keepalived/failover.sh
}

 

 

Contents of failover.sh

The failover.sh script contains the actions that are to be executed in the event of a failover. Replace the outlined values [] with the user’s specifications.

#!/bin/bash
IP='[Floating-IP-Name]'
TOKEN='[CloudToken]'

n=0
while [ $n -lt 10 ]
do
    if [ "$(/opt/hcloud-ip -ip $IP -key $TOKEN)" == "Server called $HOSTNAME was found" ]; then
        break
    fi
    n=$((n+1))
    sleep 3
done

 

 

Step 3: Test the Configuration

In regular operation, the master web server handles all requests. As soon as the master web server fails, there is a failover to the slave web server. After the failover, it is switched back to the master web server as soon as it becomes reachable again.

 

Conclusion

 This tutorial presents the steps to set up a Cloud High Availability (HA) cluster using floating IPs and Keepalived in Hetzner Cloud. Hope this tutorial was helpful, and do reach out to us if you have any query or suggestions.

Share this post

Services to Explore

Stay up to date!

Stay up to date with the Web Hosting, Cloud and Server Management Industry News and Tutorials!

We will send you only the relevant emails, and we respect your privacy. Please review our privacy policy for more info.

Managed Hetzner Cloud Services

Focus on your business, and let us take care of your Hetzner Cloud Infrastructure!
From what you are reading, it seems you are interested in Hetzner Cloud and related technologies. If you have a moment to spare, please take a look at our Managed Hetzner Cloud Services, which might interest you even more!
Managed Hetzner Cloud

Value-Added Services

We have services that can help you run a successful business. With us, you don't have to worry about these areas because our experts will take care of it for you.

Introducing - WPHealers!

Complete WordPress Management, 24x7 Support & White Label Agency Plans!

WordPress migration service, security, white-label reseller services and lot more! Grab the introductory deals now!

WPHealers

ServerHealers uses cookies.