Local HAProxy, ProxySQL Cluster and MariaDB Galera Cluster with docker-compose
Hands-on setting up local HAProxy with docker-compose
Objective
In this post, we’ll guide you through setting up HAProxy to load balance a MariaDB Galera Cluster with ProxySQL Cluster. We covered this in the previous series. This setup lets you connect to the MariaDB Galera Cluster through ProxySQL Cluster.
Prerequisites
- A bit of knowledge about Docker and docker-compose will be helpful for this setup
- Local MariaDB Galera Cluster and ProxySQL Cluster environment (e.g., you can refer my previous post)
My Environment
Software Version colima 0.8.1 docker CLI 27.5.1 docker-compose 2.33.0
Overview
What is the HAProxy ?
HAProxy is an open-source load balancer and proxy server for TCP and HTTP-based applications. HAProxy can work as an L4 (Transport Layer) load balancer. In this post, we will set up HAProxy configuration designed for L4 load balancing, as it operates at the TCP layer and routes traffic based on IP addresses and TCP ports without inspecting the content of the packets. This allows for efficient and fast load balancing of TCP connections.
In this post, we will set up an L4 load balancer to connect to one of the ProxySQL servers.
Deploy Using docker-compose
File Structure1
1
2
3
4
5
6
7
8
9
10
11
12
.
├── compose.yml # Update
└── docker/
├── db/
├── proxysql/
│ ├── Dockerfile
│ └── conf/
│ └── proxysql.cnf # Update
└── haproxy/
├── Dockerfile # New
└── conf/
└── haproxy.cfg # New
compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
services:
# === Galera Cluster ===
# If you do not have Galera Cluster settings,
# please refer previous post for more detail.
# === ProxySQL Cluster ===
# If you do not have ProxySQL Cluster settings,
# please refer previous post for more detail.
# HAProxy
haproxy:
container_name: haproxy
hostname: haproxy
build: ./docker/haproxy
volumes:
- ./docker/haproxy/conf/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
depends_on:
- proxysql1
- proxysql2
- proxysql3
ports:
- 6033:6033
- 8404:8404
haproxy.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
defaults
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend stats
mode http
bind :8404
stats enable
stats uri /stats
stats refresh 10s
stats admin if LOCALHOST
stats show-modules
frontend mysql
mode tcp
bind :6033
default_backend proxysql-cluster
backend proxysql-cluster
mode tcp
balance roundrobin
server proxysql1 proxysql1:6033 check port 6032
server proxysql2 proxysql2:6033 check port 6032
server proxysql3 proxysql3:6033 check port 6032
How to start
To start the HAProxy, run the following commands:
1
2
3
4
5
# 1. Build containers
docker-compose build
# 2. Run containers
docker-compose up -d
Check HAProxy Status
You can check status via following URI
1
http://localhost:8404/stats
Check connection to MariaDB Galera Cluster
To check the connection to MariaDB Galera Cluster status, you can use port=6033.
1
2
3
4
5
6
7
8
9
10
11
# 1. Login to one of ProxySQL via HAProxy
mysql -h localhost -P 6033 --protocol=tcp -uroot -p
# 2. Check connected node
mysql> show variables like 'hostname';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| hostname | node1 |
+---------------+-------+
1 row in set (0.01 sec)
After this connection test, we will see HAProxy WebUI’s sessions count had increased.
Now we’re ready!
You can connect MariaDB Galera Cluster via HAProxy and ProxySQL Cluster.