[DB/MySQL] Active - Active 간 HAProxy 활용을 통한 로드밸런싱 구현

 

Active - Active

Name Settings
Active - pc1 sqld, pc2와 미러링
Active - pc2 sqld, pc1과 미러링
Haproxy - pc3  HAProxy

 

 

# Network Setting : 기본적인 내용이라 생략

# pc1 & pc2 - Replication 설명은 앞 문서를 참고

# HAProxy는 DB1과 DB2에 연결되어 로드밸런싱을 진행한다.

# DB1과 DB2는 Mirror Site 관계인 동시에 Active - Active 관계이다.


1. Setting

1) 네트워크 설정 및 mysql-server , haproxy 설치
yum install -y mysql-server
yum install haproxy

2) 설정
vi /etc/haproxy/haproxy.cfg


63번 줄 다음 내용은 전부 삭제 후 
마지막 줄에 추가

listen stats # haproxy 모니터링 페이지 설정
    bind :9000 # 웹브라우저를 이용해서 9000포트로 접속하면 
    stats enable # haproxy 모니터링 페이지로 접속 됨
    stats realm Haproxy\ Statistics # http://[프록시서버IP]:9000/haproxy_stats
    stats uri /haproxy_stats


3) 서비스 재시작
systemctl restart haproxy // 에러 발생, 리눅스는 기본적으로 



2. Test
1) systemctl stop firewalld, setenforce 0 // 방화벽, 로컬 방화벽 종료


2) haproxy 모니터링 페이지에서 확인시
http://[프록시 PC IP]:9000/haproxy_stats 접속했을 때 웹 페이지가 나와야 함



3. mysql 서버 추가
1) Mysql 서버에 계정 추가
haproxy라는 이름의 계정 추가
mysql -u root -p
CREATE USER 'haproxy'@'192.168.55.5';

1-1) Mysql 사용자 조회
use mysql
select user, host from user;

1-2) Mysql 사용자 삭제
mysql> delete from user where user='haproxy';
mysql> flush privileges;

2) haproxy 설정
vi /etc/haproxy/haproxy.cfg

마지막 줄에 추가

listen mysqld-ha
bind :3306
mode tcp
balance roundrobin
option mysql-check user haproxy

server mysqld1 192.168.55.3:3306 check
server mysqld2 192.168.55.4:3306 check


4. DB 서버 부하 분산 확인

1) DB 서버에 접속할 수 있는 사용자 생성
CREATE USER 'test'@'%' IDENTIFIED BY 'qwer1234';

 

2) workbench에서 접속 테스트
hostname : [haproxy가 설치된 컴퓨터의 IP주소]
username : [위에서 생성한 계정]

접속 후 

show variables LIKE '%server_id%'; 

실행해서 server-id를 확인 , 접속할 때마다 server-id가 바껴야 정상



5. 오류 발생 
3-2)번 진행시 

오류 발생 -> Job for haproxy.service failed because the control process exited with error code.
         -> See "systemctl status haproxy.service" and "journalctl -xe" for details.
         
원인 -> centos 로컬 방화벽을 꺼주지 않아 발생하는 문제
해결 방안 -> setenforce 0