Setup HA Load Balance with HAProxy for MySQL
My Demo setup I am showing how to setup HA proxy for database
server MySQL
The load balancer HA proxy server is 192.167.57.150 
OS :   Centos 6.7
IP Address          :   192.168.57.150
MySQL Servers 
details 
Database1
OS                          :   Centos 6.7
IP Address          :   192.168.57.151
Database2
OS                          :   Centos 6.7
IP Address          :   192.168.57.152
For demo purpose we needed to install the MySQL on both
the servers 
# yum install mysql-server*
# service mysql
start
After server  of mysql please set password  to the mysql
server 
# mysql_secure_installation
NOTE: RUNNING
ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log
into MySQL to secure it, we'll need the current
Enter current
password for root (enter for none):
OK,
successfully used password, moving on...
Setting the
root password ensures that nobody can log into the MySQL
Set root
password? [Y/n] Y
New password:
Re-enter new
password:
Password
updated successfully!
Reloading
privilege tables ..
 ... Success!
By default, a
MySQL installation has an anonymous user, allowing anyone
Remove
anonymous users? [Y/n]  ...  Success!
Normally, root
should only be allowed to connect from 'localhost '.  This
Disallow root
login remotely? [Y/n] n  ...  skipping.
By default,
MySQL comes with a database named 'test' that anyone can
Remove test
database and access to it? [Y/n] y
 - Dropping test database... ... Success!
 - Removing privileges on test database ... ...
Success!
Reloading the
privilege tables will ensure that all changes made so far
Reload
privilege tables now? [Y/n] y  ... 
Success!
Cleaning up...
All done!  If you've completed all of the above steps,
your MySQL
Thanks for
using MySQL!
Prepare MySQL
Servers
We 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.
# mysql -u root
-p -e "INSERT INTO mysql.user (Host, User) values
('192.168.87.150','haproxy_check'); FLUSH PRIVILEGES;"
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.
# mysql -u root
-p -e "GRANT ALL PRIVILEGES ON *. * TO 'haproxy_root'@'192. 168.87.150'
IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH 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
Client
MySQL client has to be installed on the HAProxy server to
test connectivity.
# yum install
mysql*
Now try executing a query on one of the masters as  the
haproxy_root user.
# mysql -h
192.168.87.151 -u haproxy_root -p -e "SHOW DATABASES"
Installing HAProxy
Server
# yum install
haproxy  
192.168.85.150     
haproxy.demo.org     haproxy 
192.168.85.147     
dbserver1.demo.org    haproxy 
192.168.85.148     
dbserver2.demo.org    haproxy 
# Vi  /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4
localhost4.localdomain4
::1         localhost localhost.localdomain
localhost6 localhost6.localdomain6
192.168.87.150  haserver.demo.com haserver 
192.168.87.151  dbserver1.demo.com ha2
192.168.87.152  dbserver2.demo.com ha1
Configuring
HAProxy
Edit the configuration file 
#vi
/etc/haproxy/haproxy.cfg
The first block is the global and defaults  configuration
block.
More information about each of these options are covered
in this article. As I told 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 does not need to have
a specific "mode" for MySQL so we use tcp . To 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 droplet make it listen on
0.0.0.0 or the private IP address of the application server.
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 http://<yourserver -ip -address>: 8080/
# service
haproxy  restart
To made this service to start automatically 
# chkconfig 
haproxy  on 
To check the autostart  
# chkconfig
--list haproxy 
Before going to test the HAproxy on MySQL to differentiate
with each servers  dbaserver1 is with default databases and dbserver2 has
default databases and one more database with name glpi_inventory
Checking the HAproxy run this query HAProxy server.
# mysql -h
127.0.0.1 -u haproxy_root -p -e "SHOW DATABASES"
Enter password:
+--------------------+
| Database           |
+--------------------+
|
information_schema |
| mysql               |
+--------------------+
To test the the  HAproxy go to the dbaserver1 and stop the
service 
# service
mysqld stop 
Then check the database on the HAproxy server this time
the database will point to dbaserver2
# mysql -h
127.0.0.1 -u haproxy_root -p -e "SHOW DATABASES"
Enter password:
+--------------------+
| Database           |
+--------------------+
|
information_schema |
|
glpi_inventory     |
| mysql               |
+--------------------+
If your  want to test the HAproxy as a load server then
you can run a simple test
Go to the dbserver1 and start the mysql services 
# service
mysqld start
Go to the HAproxy server and run the below code
Query for the server_id multiple times.
# for i  in `seq 
1 6`; do mysql -h 127.0.0.1 -u haproxy_root -ppassword -e "SHOW
DATABASES"; done
+--------------------+
| Database           |
+--------------------+
|
information_schema |
| mysql               |
+--------------------+
+--------------------+
| Database           |
+--------------------+
|
information_schema |
|
glpi_inventory     |
| mysql               |
+--------------------+
+--------------------+
| Database           |
+--------------------+
|
information_schema |
| mysql               |
+--------------------+
+--------------------+
| Database           |
+--------------------+
|
information_schema |
|
glpi_inventory     |
| mysql               |
+--------------------+
+--------------------+
| Database           |
+--------------------+
| information_schema
|
| mysql               |
+--------------------+
+--------------------+
| Database           |
+--------------------+
|
information_schema |
|
glpi_inventory     |
| mysql               |
+--------------------+
This will show that alternatively this will show the
databases on the both servers, 
Please give
comments and suggestions are always welcome a
Keywords: HAproxy, HAproxy database, database server load balance,
Load balance database
server , Load balance MySQL, apache proxy web load balance, Apache on load
balance 
 
Comments
Post a Comment