본문 바로가기
리눅스

Redis Master/Slave + HAProxy

by Justin입니다. 2025. 3. 18.

아키텍처 일부분

위와 같은 아키텍처에서 로드밸런서는 Redis Cluster에 연결되어 있다. 하지만 쓰기 작업은 Redis Cluster의 Master 노드에서만 처리할 수 있다. 그렇다면 로드밸런서는 어떻게 이를 감지하고 해당 요청을 Master 노드에만 전달할 수 있을까?

 

HAProxy를 이용한 로드밸런싱 설정

HAProxy 는 지속적으로 상황을 모니터링 하면 Master 노드로만 쓰기작업을, Slave 노드로만 읽기 작업을 라우팅 할수있다. 

 

 

HAProxy 설치

dnf install haproxy

 

 

HAProxy 설정파일 /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    tcp
    log                     127.0.0.1 local0
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         5s
    timeout client          5s
    timeout server          5s
    timeout http-keep-alive 10s
    timeout check           1s
    maxconn                 10000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend ft_redis_master
    bind *:8000
    default_backend bk_redis_master
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend bk_redis_master
    balance roundrobin
    log global
    option tcp-check
    tcp-check send info\ replication\r\n
    tcp-check expect string role:master
    server tcpapp1 10.10.45.9:6379 check
    server tcpapp2 10.10.45.11:6379 check
    server tcpapp3 10.10.45.12:6379 check
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend ft_redis_slave
    bind *:8001
    default_backend bk_redis_slave
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend bk_redis_slave
    balance roundrobin
    log global
    option tcp-check
    tcp-check send info\ replication\r\n
    tcp-check expect string role:slave
    server tcpapp1 10.10.45.9:6379 check
    server tcpapp2 10.10.45.11:6379 check
    server tcpapp3 10.10.45.12:6379 check

 

frontend 섹션

 

  • frontend ft_redis_master: ft_redis_master라는 이름의 frontend를 정의. 클라이언트로부터 요청을 받는 부분.
  • *bind :8000: HAProxy가 8000 포트에서 모든 IP 주소로 요청을 수신하도록 설정. 클라이언트는 이 포트를 통해 HAProxy에 연결.
  • default_backend bk_redis_master: 이 frontend에서 요청을 받은 뒤, bk_redis_master라는 백엔드로 요청을 전달.

 

backend 섹션

 

  • backend bk_redis_master: bk_redis_master라는 이름의 backend를 정의. 이 부분은 실제 Redis 마스터 서버로 요청을 분배한다.
  • balance roundrobin: roundrobin 방식으로 요청을 서버에 분배.
  • log global: 전역 로그 설정을 사용하여 로그를 기록.
  • option tcp-check: tcp-check 옵션을 활성화하여 서버 상태를 체크.
  • tcp-check send info\ replication\r\n: Redis 서버에 INFO replication 명령을 보내서 서버의 복제 상태를 조회.
  • tcp-check expect string role:master: Redis 서버의 응답에서 role:master가 포함되어 있는지 확인하여, 마스터 서버인지 검사.
  • server tcpapp1 10.10.45.9:6379 check: tcpapp1이라는 이름의 서버를 10.10.45.9:6379로 설정하고, check 옵션을 추가하여 서버 상태를 점검.
  • server tcpapp2 10.10.45.11:6379 check: tcpapp2 서버를 10.10.45.11:6379로 설정하고, 상태 체크를 활성화.
  • server tcpapp3 10.10.45.12:6379 check: tcpapp3 서버를 10.10.45.12:6379로 설정하고, 상태 체크를 활성화.

 

HAProxy 재시작

systemctl restart haproxy

 

 

구동 테스트

 

 

Master Down -> Redis Sentinel 이 새로운 마스터 선출 -> HAProxy 가 이를 감지하고 라우팅

 

'리눅스' 카테고리의 다른 글

ls 명령어를 하면 무슨일이 일어날까  (0) 2025.04.02
systemctl enable  (0) 2025.04.02
Nginx SSL/TLS ssl_preread & stream  (1) 2025.03.17
Redis Failover  (0) 2025.03.13
Vmware 네트워크  (0) 2025.01.12