Home » Categories » Multiple Categories |
How To Use HAProxy to Set Up MySQL Load Balancing |
Article Number: 189 | Rating: Unrated | Last Updated: Thu, Dec 26, 2013 at 10:57 PM
|
PreludeHAProxy is an open source software which can load balance HTTP and TCP servers. In the previous article on HAProxy we configured load balancing for HTTP and in this one we'll do the same for MySQL. All your MySQL servers have to be configured to perform Master-Master replication as load balancing involves both reading and writing to all the backends.The following three panels will be used in this article: panel 1 - Load Balancer panel 2 - Node 1 panel 3 - Node 2 Before proceeding, make sure all MySQL servers are up, running and are properly replicating database writes. Prepare MySQL ServersWe need to prepare the MySQL servers by creating two additional users for HAProxy. The first user will be used by HAProxy to check the status of a server.
A MySQL user is needed with root privileges when accessing the MySQL cluster from HAProxy. The default root user on all the servers are allowed to login only locally. While this can be fixed by granting additional privileges to the root user, it is better to have a separate user with root privileges.
Replace haproxy_root and password with your own secure values. It is enough to execute these queries on one MySQL master as changes will replicate to others. Install MySQL ClientMySQL client has to be installed on the HAProxy to test connectivity.
Now try executing a query on one of the masters as the haproxy_root user.
This should display a list of MySQL databases. Installing HAProxyOn the HAProxy server install the package.
Enable HAProxy to be started by the init script.
To check if this change is done properly execute the init script of HAProxy without any parameters.
Configuring HAProxyRename the original configuration file
Create and edit a new one
The first block is the global and defaults configuration block.
More information about each of these options are covered in this article. Since we've told HAProxy to send log messages to 127.0.0.1 we have to configure rsyslog to listen on it. This has too been covered in the same article under Configure Logging for HAProxy. Moving to the main configuration part.
Unlike HTTP load balancing HAProxy doesn't have a specific "mode" for MySQL so we use tcp. We've set HAProxy to listen only on the loopback address (assuming that application is on the same server) however if your application resides on a different panel make it listen on 0.0.0.0 or the private IP address. We need one more configuration block to see the statistics of load balancing. This is completely optional and can be omitted if you don't want stats.
Replace the usernames and passwords in "stats auth". This will make HAProxy listen on port 8080 for HTTP requests and the statistics will be protected with HTTP Basic Authentication. So you can access stats at
Once you're done configuring start the HAProxy service.
Use the mysql client to query HAProxy.
The "-h" option has to be present with the loopback IP address. Omitting it or using localhost will make the MySQL client connect to the mysql.sock file which will fail. Testing Load Balancing and FailoverTo check if load balancing is working query the server_id variable twice or more.
This demonstrates roundrobin load balancing with equal weights, we'll now change the weight for mysql-2 and see the results.
Reload to apply this change.
Query for the server_id multiple times.
Now load balancing works in the ratio of 1:2 with one-thirds of the requests going to mysql-1 and two-thirds going to mysql-2. Fail a MySQL server either by stopping the service
or bringing the interface down.
Try the "show variables" query now to see the result. The following log entries will indicate when and how HAProxy detected the failure.
Reducing Failover IntervalWhen a MySQL server goes down HAProxy takes some time to detect this failure and remove it from the cluster. In this section we'll see how to control this time. First we'll see how to measure this value. One way is to block the MySQL port using iptables for a certain amount of time, then remove the rule and check the log.
The port 3306 was blocked for 20 seconds, we'll look at the log file now.
It took 6 seconds to detect a failure (difference between 16:49:38 and 16:49:32) and 4 seconds_to detect that the server can be reached (difference between 16:49:56 and 16:49:52). This is determined by the server parameters rise, fall and inter. The rise parameter sets the number of checks a server must pass to be declared operational. Default is 2. The fall parameter sets the number of checks a server must pass to be declared dead. Default is 3. The inter parameter sets the interval between these checks. Default is 2000 milliseconds. Putting this info together a server must fail 3 continuous checks which are performed at an interval of 2 seconds to be considered dead. So in our example above the following would've happened.
And when the firewall rule was removed.
The following settings will reduce the test interval to 1 second and also reduce the number of fall tests.
Sometimes you may not want to flood the private network with too many "test" packets especialy if you have a large amount of MySQL servers. In such cases the fastinter and downinter parameters will come handy. The fastinter parameter sets the interval between checks while a server is transitioning UP or DOWN. The downinter parameter sets the test interval when a server is DOWN. That explanation might be confusing so we'll see it with an example.
Since we haven't specified the "inter" parameter it defaults to 2000ms. With this configuration we'll restart HAProxy and do the test again.
Check the HAProxy log file.
Now it took only 4 seconds (compared to 6 earlier) to detect a failure and 3 seconds (compared to 4) to detect that the server was up. Behind the scenes this is what happened.
And when the port was unblocked.
First notice the interval between the port block event (17:18:48) and the first check (17:18:50), it is 2 seconds (the "inter" interval). Then notice the interval between Test 1 <-> Test 2 and Test 2 <-> Test 3, it is only 1 second (the "fastinter" interval). The same intervals can be noticed when the server moved from DOWN to UP. So "fastinter" controls the interval between these checks. So what is downinter? When a server has been declared DOWN HAProxy continues checking it every 2 seconds (or the interval mentioned in inter). If you feel you're using up unnecessary network resources setting the downinter to say 5000 will make HAProxy check a DOWN server only once in 5 seconds. ImportantThe tests we did previously REJECTed the packets which means when HAProxy initiated a connection by sending a SYN packet to mysql-1 it received a RST packet (instead of SYN + ACK). This is why the log entry mentioned "Connection refused". In this case only the fall, inter and fastinter values come into the scene.Instead if HAProxy didn't receive anything after sending SYN the connection times out. In this case in addition to the above mentioned parameters the "timeout" duration comes into the scene. This situation can happen if
|
Attachments
There are no attachments for this article.
|
How To Copy Files With Rsync Over SSH
Viewed 6520 times since Fri, Dec 27, 2013
How To Launch Your Site on a New Ubuntu 12.04 Server with LAMP, SFTP, and DNS
Viewed 2561 times since Thu, Dec 26, 2013
Initial Server Setup with Debian 7
Viewed 3676 times since Fri, Dec 27, 2013
How to Get Started With mod_pagespeed with Apache on an Ubuntu and Debian Cloud Server
Viewed 3807 times since Thu, Dec 26, 2013
How To Install phpMyAdmin on a LEMP server
Viewed 2432 times since Thu, Dec 26, 2013
The Basics of Using the Sed Stream Editor to Manipulate Text in Linux
Viewed 6500 times since Fri, Dec 27, 2013
How To Create a New User and Grant Permissions in MySQL
Viewed 3284 times since Thu, Dec 26, 2013
How To Set Up Apache Virtual Hosts on Ubuntu 12.04 LTS
Viewed 3512 times since Mon, Dec 23, 2013
How To Use a Simple Bash Script To Restart Server Programs
Viewed 3236 times since Fri, Dec 27, 2013
Upgrading Debian to Unstable
Viewed 3595 times since Thu, Dec 26, 2013
|