funasaki memo

このブログ上の投稿は個人のものであり、所属する企業を代表する投稿ではありません。所属:AWSのSolutions Architect

HAProxyを使って複数のRDS MySQLへ負荷分散させてみる。

今回は、バックエンドのRDSの中身が異なるものを使うことで、違うRDSに接続していることを確認したい。
RDS Read Replicaだと、すぐにDBの中身が同期されてしまい、中身が同じになってしまう。
ので、敢えて異なるRDS2つを立ち上げて、それらの全面にhaproxyを置くことにした。
f:id:kenjifunasaki:20130208182926p:plain
haproxy側はAmazon Linux 2012.09で試している。
RDS MySQLは5.5のバージョンを使用。

まず、Amazon Linuxインスタンスにhaproxyをインストール。

sudo rpm -Uvh http://ftp-stud.hs-esslingen.de/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
sudo yum install --enablerepo=epel haproxy

次に、/etc/haproxy/haproxy.cfgを以下のように編集

listen mysql
        bind    0.0.0.0:3306
        mode    tcp
        timeout connect 10s
        timeout server 1m
        timeout client 1m
        option  mysql-check
        balance leastconn
        server  master1 mydbinstance5.cm4rmjkwcnlv.ap-northeast-1.rds.amazonaws.com:3306 check port 3306 inter 10000 fall 2
        server  master2 mydbinstance7.cm4rmjkwcnlv.ap-northeast-1.rds.amazonaws.com:3306 check port 3306 inter 10000 fall 2

haproxy起動

sudo /etc/init.d/haproxy start

別のインスタンスからhaproxyが動くAmazon Linuxインスタンスmysqlコマンドで接続できるか確認する。

mysql -u awsuser -p -h ec2-xx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com

上記コマンドでmysqlコマンドプロンプトが実行されればOK。
あとは、違うRDSに毎回接続されることを確認する。

1回目。

$ mysql -u awsuser -p -h ec2-54-249-145-177.ap-northeast-1.compute.amazonaws.com
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 70
Server version: 5.5.27-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| innodb             |
| mydb               |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)

2回目。
>|sh|
$ mysql -u awsuser -p -h ec2-xx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 154
Server version: 5.5.27-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| innodb             |
| mydb               |
| mysql              |
| performance_schema |
|<b> test </b>              |
+--------------------+
6 rows in set (0.00 sec)

1回目はtestデータベースが存在しないRDS、2回目は存在するRDSに接続された。
これで、負荷分散できていることが確認できた。
(補足)
haproxyからmysql dbへ接続が10回以上失敗してしまうと下記のようなエラーが表示されてしまい、接続できなくなる。
Host 'ip-10-132-81-110.ap-northeast-1.compute.internal' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'Connection closed by foreign host.

http://dev.mysql.com/doc/refman/5.1/ja/blocked-host.html

そこで、max_connect_errorsの値を増やしておくと、上記エラーが出なくなる。
この値はRDS MySQLでは、Parameter Groupの値を編集することで変更できる。