# Part 2 - Configure HAProxy, test the cluster and set up monitoring [TOC] ## Configure HAProxy for your Galera Cluster - In this scenario, we are setting up HAProxy on OPNSense, where it is available as a plugin. If you have a different reverse proxy or have HAProxy deployed in a different way, you can try following the steps before, although it may differ. - In case you do not yet have HAProxy installed/enabled on your OPNSense, go ahead to ‘System’ → ‘Firmware’ → ‘Plugins’ and download it.
- Log into OPNSense and go to ‘Services’ → ‘HAProxy’ → ‘Settings’. Then again to ‘Settings’ → ‘Service’ to ensure that the service is running. - Create a health check for the MySQL service that MariaDB needs: - In the top menu, go to ‘Rules & Checks’ → ‘Health Monitors’ - Click on the + sign to add a new one (this button is a bit less visible). - Name/Description: as you like, such as mariadb_health_check - Check type: MySQL - Check internal 3s (or as you prefer) - Port: 3306 - Now let’s make HAProxy aware of each container’s existence. While still in ‘Services’ → ‘HAProxy’ → ‘Settings’ in the left menu, go to ‘Real servers’ → ‘Real servers’ drop-down option. Click on the + sign to add a new one for each instance. - Name: name of each galera instance, such as galera_a1 - Type: static - IP: the real IP of each instance - Port: 3306 (optional, since our health check has it configured already) - Mode: active - In the end, you should have all four listed: - The next step is to create a backend pool. Go to ‘Virtual Services’ → ‘Backends’ and click on the + sign again. - Name/Description: As per your preference, such as mariadb_galeraA_pool - Mode: TCP - Algorithm: Round robin - Servers: list all your MariaDB containers that you want in there - Tick box for Health Checking - Tick ‘Log status Changes’ if you would like to. - At this point, it would be good after saving this change to click on the ‘Test syntax’ button to ensure that there are no errors and then you can click on the ‘Apply’ button. - So now HAProxy knows what to check and where and how often. Yet, for our web apps, we would want a single point of contact. We will need a virtual IP address. - In the left menu of your OPNSense, go to ‘Interfaces’ → ‘Virtual IPs’ → ‘Settings’ and click on the + sign to add a new one. - Mode: CARP (as we have OPNSense in a HA cluster already and want them to take over in case one fails) - Interface: LAN - Network address: Choose an address on your LAN that is not on your DHCP range (mine starts from .101 onwards). The subnet should be specific to that IP only, i.e. /32. - Set up a custom password - if you have more OPNSense units, it needs to match on all instances. - VHID: needs to be unique from others and match with other OPNSense units in the cluster (if you have one). - advbase: same as above - advskew: The main unit should have a lower value, the secondary (backup) unit(s) higher value(s). - Check out my previous article on [**how CARP and HA works on OPNSense**](https://bachelor-tech.com/opnsense-high-availability/configure-carp-on-lan-wan-interfaces/) for more details. - You might object why to add another CARP interface when you already have your OPNSense in a cluster (and if not, why not - just [**follow this guide**](https://bachelor-tech.com/detailed-guides/opnsense-in-ha-with-carp-with-dual-wans/)). This is the cleanest way of separating our network's gateway IP from our database service IP. If we ever need to add firewall or NAT rules in the future, we can simply refer to this virtual IP. - This is how it looks on my end after configuring the virtual IP for the Galera DB service: - Finally, we can head back to HAProxy via ‘Services’ → ‘HAProxy’ → ‘Settings’ and find the ‘Virtual Services’ in the top menu and select ‘Public Services’ from the drop-down. Click on the + sign. - Name/Description: As per your preference, something like ‘galera_db_cluster_listener’ may do. - Listen address: your virtual IP - Type: TCP (not HTTP!) - Default back-end pool: our previously configured pool, e.g. ‘mariadb_galeraA_pool’. - After saving it, testing the syntax and applying, it would be good to ensure that this new config gets replicated to the other OPNSense instance in HA that we have configured in our previous guide. - Head to ‘System’ → ‘High Availability’ → ‘Status’. - Click on the button to the right of ‘Synchronize config to backup’. - Then it would be good to log into your backup instance(s) to confirm that all the HAProxy and virtual IP config is in there. Esp. in HAProxy, it is often necessary to apply the new configuration: - Now we are talking! Let’s run some tests to confirm it is working as expected. ## Testing your Galera MariaDB cluster & Troubleshooting - **Note:** the `haproxy_check` user created earlier is scoped to only log in from the OPNSense nodes' own IPs (`opnsense_node1_ip_here` / `opnsense_node2_ip_here`), that's deliberate, it's a health-check account, not a general-purpose one. Run the test below from one of those OPNSense nodes, not from an arbitrary machine, or it will fail to authenticate. - Connect to any Linux machine that has MariaDB client installed, run the following (using the user we created earlier for this type of tests): ```sql mariadb -h 192.168.8.70 -u haproxy_check -p -e "SELECT @@wsrep_node_name;" ``` - The output will reveal which instance is being queried. If you run it repeatedly, you will get the same result due to stick-table persistence. - If you try connecting from another instance, you will likely reach another one, instead: - This confirms that the round-robin works for each new client connecting. --- Hardware Set up for the Pi - Grab a Raspberry Pi (any version will do if it's not already busy with other tasks). - The Pi should be connected ideally via the same switch as the Proxmox nodes on the same network for low latency. I would not recommend WiFi connection here. - The OS is up to your choice, a basic headless Raspberry Pi OS Lite would do. In this guide, I assume it is a Debian-based distro. - Ideally, have it connected to UPS. Either have one that covers several devices (such as your Proxmox nodes + router(s) + the Pi) or one that is dedicated to the Pi, that is a power bank with UPS capabilities. Ping me for tips if you are struggling to find some 🙂 What to install - Connect to the Pi via SSH and run the following: ```bash # Update / Upgrade the OS to its newest version sudo apt update && sudo apt upgrade -y # Install MariaDB client only (no need for a server) sudo apt install galera-arbitrator-4 -y ``` - Let’s configure the Arbitrator: ```bash sudo nano /etc/default/garb # A comma-separated list of other node addresses (IPs) in your Galera cluster. # At least one of these must be contactable at startup. GALERA_NODES="