Skip to main content

[How-To] Configure Virtual IP for MariaDB servers with Keepalived

Purpose

This How-To serves as documentation on how to add keepalived to your MariaDB servers so they can all share a virtual IP or a VIP. 

Prerequisites

List of prerequisites:

  • Sudo user
  • Galera cluster of MariaDB servers on Ubuntu 24.04 LTS

Instructions

Step 1: Update and Install Software

Run commands below to update os and install keepalived:

sudo apt update
sudo apt install -y keepalived

Step 2: Configure Keepalived

With keepalived freshly installed, we can open the configuration file on each server with this command:

sudo nano /et/keepalived/keepalived.conf

Here, you'll want to configure the master like this:

vrrp_instance VI_1 {
    state MASTER
    interface eth0         # Replace with your actual network interface
    virtual_router_id 51
    priority 100           # Higher priority on the master
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass securepassword
    }

    virtual_ipaddress {
        192.168.1.100      # Replace with your VIP
    }

    track_script {
        chk_mariadb
    }
}

vrrp_script chk_mariadb {
    script "/usr/local/bin/check_mariadb.sh"
    interval 2
    weight -20
}

Then, you'll want to configure the backups like this:

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90           # Lower priority on the backup
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass securepassword
    }

    virtual_ipaddress {
        192.168.1.100
    }

    track_script {
        chk_mariadb
    }
}

vrrp_script chk_mariadb {
    script "/usr/local/bin/check_mariadb.sh"
    interval 2
    weight -20
}

Step 3: Enable and Start Keepalived

Now we're ready to start the service. Run the following to enable it at startup and then start the service once now:

sudo systemctl enable keepalived
sudo systemctl start keepalived

Check the status of the service to verify its running:

sudo systemctl status keepalived

Step 4: Test the VIP

Finally, test that the VIP is there:

ip addr show