유명한 로드밸런서 오픈소스인 ha-proxy를 사용하여 

mariadb 를 select 용 db / dml 용 db로 나눠서 로드밸런싱 하는 방법을 공유드리겠습니다.

 

-1. ha-proxy 설치 

$ wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.4.tar.gz
$ 압축 해제 후
$ make TARGET=linux2628
$ make install
 
 
* TARGET 옵션 *
- linux22     for Linux 2.2
- linux24     for Linux 2.4 and above (default)  
- linux24e    for Linux 2.4 with support for a working epoll (> 0.21)  
- linux26     for Linux 2.6 and above  
- linux2628   for Linux 2.6.28, 3.x, and above (enables splice and tproxy)  
- solaris     for Solaris 8 or 10 (others untested)  
- freebsd     for FreeBSD 5 to 10 (others untested)  
- netbsd      for NetBSD   - osx         for Mac OS/X  
- openbsd     for OpenBSD 3.1 and above  
- aix51       for AIX 5.1  
- aix52       for AIX 5.2  
- cygwin      for Cygwin  
- generic     for any other OS or version.  
- custom      to manually adjust every setting
 
 
-2. ha-proxy -> db 연결 설정
 
$vi /engn001/haproxy/db-haproxy.cfg
 
 
frontend mysql-select
        bind *:3326
        default_backend select
backend select
        balance roundrobin
        server mysql_slave1 11.111.111.111:3311  check
        server mysql_slave2 22.222.222.222:3310  check

frontend mysql-dml
        bind *:3336
        default_backend dml
backend dml
        balance roundrobin
        server mysql-master 33.33.333.33:3311  check
 
=> 3326 port 로 들어오는 요청은 mysql-select 라는 이름의 그룹으로 설정 후
select 용도 db 두대에  round robin 방식으로 뿌려줌
 
3336 port 로 들어오는 요청은 mysql-dml 라는 이름의 그룹으로 설정 후
master용도 db에 뿌려줌

 

-3. 로드밸런싱 테스트

 

$ mysql -uha_test -ptest -h 'ha-proxy구성 서버ip' -P3326 -e "show variables like 'server_id'"

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 5     |
+---------------+-------+

$ mysql -uha_test -ptest -h 'ha-proxy구성 서버ip' -P3326 -e "show variables like 'server_id'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 4     |
+---------------+-------+
 
=> 위에서 설정한대로 3326 포트로 요청할시 select용도 db 두대에 round robin 식으로 쿼리가 전달되는 것 확인가능