83
83

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HaProxyでMySQLを負荷分散する!!

Posted at

MySQLのロードバランサーとしては、LVS (KeepAlived)が有名ですが、AWS上でこれを設定するのは困難なので、Haproxyでロードバランシングが実現できるか検証する。。っていうか出来たので、作業ログになるかな。

HAPROXYのインストール

インストール

# yum install haproxy
haproxy.x86_64                                     1.4.22-5.3.amzn1                                      amzn-main 

MySQLのインストール

yum --enablerepo=wing install mysql55 mysql55-server

MySQLの開始

# /etc/init.d/mysqld start

MySQLの停止

# /etc/init.d/mysqld stop
# /sbin/chkconfig mysqld off

ログ出力設定

# vi /etc/rsyslog.conf
--- 以下を追記
$ModLoad imudp
$UDPServerRun 514
# haproxy setting
local2.*                                                /var/log/haproxy.log
---

rsyslogの再起動

# /etc/init.d/rsyslog restart

対象のDBサーバすべてに、HAPROXY用監視ユーザーを作成する(作成済みの場合は作業の必要はありません)

$ mysql -u root -prootpasswd  -h [対象のホスト名] -e "grant usage on *.* to 'haproxy'@'%';"

HAPROXY 設定ファイル編集

# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
# vi /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1 local2
    chroot /var/lib/haproxy
    pidfile /var/run/haproxy.pid
    maxconn 4000
    user haproxy
    group haproxy
    daemon
    stats socket /tmp/haproxy-cli.sock user root group wheel level admin
    stats maxconn 1
    stats timeout 120s
defaults
    mode tcp
    log global
    retries 3
    timeout connect 10s
    timeout client 1m
    timeout server 1m
    timeout check 10s
    maxconn 3000
listen mysql
    bind 0.0.0.0:3306
    mode tcp
    option mysql-check user haproxy

#   ラウンドロビン方式での振り分け。こちらは重み付けも可能で、他には
#   static-rr、leastconn、source等色々用意されている。

    balance roundrobin

#   server ホスト名 IP:Port check inter 2000 fall 2
#   WebサーバのIPとPortを指定。checkはヘルスチェックの有効化でinter
#   でヘルチェック間隔の指定。単位はミリ秒なので今回は2秒。fallは2回
#   ヘルチェックが落ちたら振り分け対象から切り離すという意味。
    server read1 ip-10-XXX-166-XXX.ap-northeast-1.compute.internal:3306 check port 3306 inter 2000 fall 5 
    server read2 ip-10-XXX-131-XXX.ap-northeast-1.compute.internal:3306 check port 3306 inter 2000 fall 5
#    server master xxx.xxxx.xxx:3306 check port 3306 backup

HAPROXYの開始

# /etc/init.d/haproxy start

Haproxy コマンドラインツールのインストール

# yum install socat

動作確認

情報の確認

# echo "show info" | socat stdio /tmp/haproxy-cli.sock
Name: HAProxy
Version: 1.4.22
Release_date: 2012/08/09
Nbproc: 1
Process_num: 1
Pid: 18311
Uptime: 0d 0h15m56s
Uptime_sec: 956
Memmax_MB: 0
Ulimit-n: 8005
Maxsock: 8005
Maxconn: 4000
Maxpipes: 0
CurrConns: 1
PipesUsed: 0
PipesFree: 0
Tasks: 3
Run_queue: 1
node: ip-10-134-207-147
description: 

ステータスの確認

# echo "show stat" | socat stdio /tmp/haproxy-cli.sock
# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,
mysql,FRONTEND,,,0,1,3000,27,4375,10696,0,0,0,,,,,OPEN,,,,,,,,,1,1,0,,,,0,0,0,2,,,,,,,,,,,0,0,0,,,
mysql,read1,0,0,0,1,,14,2279,5644,,0,,0,0,0,0,UP,1,1,0,0,0,1061,0,,1,1,1,,14,,2,0,,1,L7OK,0,1,,,,,,,0,,,,0,0,
mysql,read2,0,0,0,1,,13,2096,5052,,0,,0,0,0,0,UP,1,1,0,0,0,1061,0,,1,1,2,,13,,2,0,,1,L7OK,0,33,,,,,,,0,,,,0,0,
mysql,BACKEND,0,0,0,1,3000,27,4375,10696,0,0,,0,0,0,0,UP,2,2,0,,0,1061,0,,1,1,0,,27,,1,0,,2,,,,,,,,,,,,,,0,0,

重みの確認

# echo " get weight mysql/read1" | socat stdio /tmp/haproxy-cli.sock
1 (initial 1)

# echo " get weight mysql/read2" | socat stdio /tmp/haproxy-cli.sock
1 (initial 1)

コマンドラインからの設定

重みを設定する

# echo " set weight mysql/read1 50" | socat stdio /tmp/haproxy-cli.sock
# echo " set weight mysql/read2 50" | socat stdio /tmp/haproxy-cli.sock
# echo " get weight mysql/read1" | socat stdio /tmp/haproxy-cli.sock
50 (initial 1)
# echo " get weight mysql/read2" | socat stdio /tmp/haproxy-cli.sock
50 (initial 1)

read2のサーバをメンテナンスモードにする

# echo " disable server mysql/read2" | socat stdio /tmp/haproxy-cli.sock
# echo "show stat" | socat stdio /tmp/haproxy-cli.sock
# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,
mysql,FRONTEND,,,0,1,3000,27,4375,10696,0,0,0,,,,,OPEN,,,,,,,,,1,1,0,,,,0,0,0,2,,,,,,,,,,,0,0,0,,,
mysql,read1,0,0,0,1,,14,2279,5644,,0,,0,0,0,0,UP,50,1,0,0,0,1985,0,,1,1,1,,14,,2,0,,1,L7OK,0,11,,,,,,,0,,,,0,0,
mysql,read2,0,0,0,1,,13,2096,5052,,0,,0,0,0,0,MAINT,50,1,0,0,1,9,9,,1,1,2,,13,,2,0,,1,L7OK,0,1,,,,,,,0,,,,0,0,
mysql,BACKEND,0,0,0,1,3000,27,4375,10696,0,0,,0,0,0,0,UP,50,1,0,,0,1985,0,,1,1,0,,27,,1,0,,2,,,,,,,,,,,,,,0,0,
83
83
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
83
83

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?