Ossian Story
article thumbnail

[DB] Redis + HAProxy를 활용한 FailOver 구성


HAProxy 란?


HAProxy는 소프트웨어 로드 밸런서로 L4 / L7의 기능 및 로드 밸런서를 제공합니다.

지난 포스팅에서 Redis Master에게 장애 발생 시 Redis Sentinel에 의해 Redis Slave가 Master 역할을 할 수 있도록 하여 FailOver에 대한

장애처리를 할 수 있는 방법을 알아보았습니다.


# "[DB] Redis Sentinel 구성" 포스팅 참조


이번 포스팅에서는 Redis Slave가 Master가 되었을 때 Client는 여전히 기존의 장애가 발생한 Redis Master 참조할 것입니다.

이러한 부분을 HAProxy를 활용하여 Client가 변경된 Redis Master를 정상적으로 참조할 수 있도록 구성하는 방법을 알아보겠습니다.


HAProxy의 HA구성 및 상세한 활용은 "HAProxy" 포스팅을 통해 따로 습득해보겠습니다.



Redis + HAProxy 구성


아래의 구성과 같이 Client는 HAProxy를 통해 Redis와 통신을 합니다.

HAProxy는 Redis Master와 5000 Port로 통신을 Redis Slave와 5001 Port로 통신을 하게 됩니다.

즉, Redis Master에 장애가 발생하여 Redis Slave가 새로운 Redis Master로 선출되어도 HAProxy는 새롭게 선출 된 Redis Master와 5000 Port를 통해

통신할 수 있으므로 Client 또한 새롭게 선출 된 Redis Master와 통신할 수 있습니다.




HAProxy 설치 준비


yum을 사용해 "make / gcc / gcc-c++ / pcre-devl /openssl-devel" 을 설치합니다.

$> sudo yum -y install make gcc gcc-c++ pcre-devel openssl-devel



HAProxy Download

$> wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.9.tar.gz




HAProxy 설치


다운로드 받은 HAProxy 압축을 해제합니다.

$> tar -zxvf haproxy-1.7.9.tar.gz


압축을 해제한 haproxy-1.7.9 디렉토리로 이동하여 "README" 파일을 확인합니다.

$> cd haproxy-1.7.9
$> ls -l
total 540
-rw-rw-r--.  1 ossian ossian 406393 Aug 18 06:33 CHANGELOG
drwxrwxr-x. 13 ossian ossian    175 Aug 18 06:33 contrib
-rw-rw-r--.  1 ossian ossian  41508 Aug 18 06:33 CONTRIBUTING
drwxrwxr-x.  5 ossian ossian   4096 Aug 18 06:33 doc
drwxrwxr-x.  2 ossian ossian   4096 Aug 18 06:33 ebtree
drwxrwxr-x.  3 ossian ossian   4096 Aug 18 06:33 examples
drwxrwxr-x.  6 ossian ossian     60 Aug 18 06:33 include
-rw-rw-r--.  1 ossian ossian   2029 Aug 18 06:33 LICENSE
-rw-rw-r--.  1 ossian ossian   2310 Aug 18 06:33 MAINTAINERS
-rw-rw-r--.  1 ossian ossian  33764 Aug 18 06:33 Makefile
-rw-rw-r--.  1 ossian ossian  15292 Aug 18 06:33 README
-rw-rw-r--.  1 ossian ossian   2922 Aug 18 06:33 ROADMAP
drwxrwxr-x.  2 ossian ossian    101 Aug 18 06:33 scripts
drwxrwxr-x.  2 ossian ossian   4096 Aug 18 06:33 src
-rw-rw-r--.  1 ossian ossian     14 Aug 18 06:33 SUBVERS
drwxrwxr-x.  2 ossian ossian   4096 Aug 18 06:33 tests
-rw-rw-r--.  1 ossian ossian     24 Aug 18 06:33 VERDATE
-rw-rw-r--.  1 ossian ossian      6 Aug 18 06:33 VERSION



README 파일에는 아래와 같은 내용이 있으며, 현재 설치된 Kernel에 맞춰 컴파일 해야 합니다.

To build haproxy, you have to choose your target OS amongst the following ones
and assign it to the TARGET variable :
 
  - 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.283.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 5.7 and above
  - aix51       for AIX 5.1
  - aix52       for AIX 5.2
  - cygwin      for Cygwin
  - haiku       for Haiku
  - generic     for any other OS or version.
  - custom      to manually adjust every setting



아래의 명령어로 현재 설치된 Kernel을 확인합니다.

$> uname -r
3.10.0-693.5.2.el7.x86_64



현재 저의 Kernel은 3.x 버전이므로 아래와 같이 컴파일 합니다.

$> sudo make TARGET=linux2628



컴파일이 완료 되었으면 설치를 진행합니다.

$> sudo make install



"etc"디렉토리 하위에 "haproxy" 디렉토리를 생성 합니다.

$> sudo mkdir /etc/haproxy



"haproxy.init" 스크립트 파일을 "/etc/init.d/haproxy"경로에 복사 후 실행권한을 부여합니다.

$> pwd
/home/ossian/haproxy-1.7.9
 
$> sudo cp ./examples/haproxy.init /etc/init.d/haproxy
$> sudo chmod 755 /etc/init.d/haproxy



"haproxy" 실행 파일을 "/usr/sbin/haproxy"경로에 복사합니다.

$> sudo cp /usr/local/sbin/haproxy /usr/sbin/haproxy



"/etc/haproxy" 디렉토리에 "haproxy.cfg" 파일을 생성합니다.

$> sudo vi /etc/haproxy/haproxy.cfg



아래의 내용과 같이 "haproxy.cfg" 파일 내용을 작성 후 저장합니다.

defaults REDIS
 mode tcp
 timeout connect  4s
 timeout server  15s
 timeout client  15s
 timeout tunnel 365d
 
frontend ft_redis_master
 bind *:5000 name redis
 default_backend bk_redis_master
 
backend bk_redis_master
 option tcp-check
 tcp-check send AUTH\ mypassword\r\n
 tcp-check expect string +OK
 tcp-check send PING\r\n
 tcp-check expect string +PONG
 tcp-check send info\ replication\r\n
 tcp-check expect string role:master
 tcp-check send QUIT\r\n
 tcp-check expect string +OK
 
 server R1 10.146.0.8:6379 check inter 1s    # Redis-Master / Redis-Slave 정보를 입력합니다.
 server R2 10.146.0.9:6379 check inter 1s
 server R3 10.146.0.10:6379 check inter 1s
 server R4 10.146.0.11:6379 check inter 1s
 
frontend ft_redis_slave
 bind *:5001 name redis
 default_backend bk_redis_slave
 
backend bk_redis_slave
 option tcp-check
 tcp-check send AUTH\ mypassword\r\n
 tcp-check expect string +OK
 tcp-check send PING\r\n
 tcp-check expect string +PONG
 tcp-check send info\ replication\r\n
 tcp-check expect string role:slave
 tcp-check send QUIT\r\n
 tcp-check expect string +OK
 
 server R1 10.146.0.8:6379 check inter 1s    # Redis-Master / Redis-Slave 정보를 입력합니다.
 server R2 10.146.0.9:6379 check inter 1s
 server R3 10.146.0.10:6379 check inter 1s
 server R4 10.146.0.11:6379 check inter 1s
 
listen stats
bind 0.0.0.0:80 #Listen on all IP's on port 9000
    mode http
    balance
    timeout client 5000
    timeout connect 4000
    timeout server 30000
 
    #This is the virtual URL to access the stats page
    stats uri /haproxy_stats
 
    #Authentication realm. This can be set to anything. Escape space characters with a backslash.
    stats realm HAProxy\ Statistics 
    
    #The user/pass you want to use. Change this password!
    stats auth ossian:password   # 모니터링 웹페이지에서 사용 할 ID/PW를 입력합니다.
 
    #This allows you to take down and bring up back end servers.
    #This will produce an error on older versions of HAProxy.
    stats admin if TRUE
 




HAProxy 실행


아래의 명령어로 HAProxy를 실행합니다.

$> sudo /etc/init.d/haproxy start
Reloading systemd:                                         [  OK  ]
Starting haproxy (via systemctl):                          [  OK  ]



정상적으로 실행이 되었다면 Web Client로 HAProxy Server에 접속해 봅니다.

저는 GCP(Google Cloud Platform)에서 Instance를 생성하여 HAProxy Server를 구성하였음에 따라

공인 IP로 HAProxy Server에 접속 할 것입니다.


접속해야 하는 URL은 아래와 같으며 [myserver] 부분에 IP 또는 DNS에 등록된 Host명을 입력해주면 됩니다.

http://myserver/haproxy_stats




모니터링 페이지에 로그인합니다. ID / PW는 "haproxy.cfg" 작성 시 입력했던 ID / PW를 입력하면 됩니다.


모니터링 페이지에 정상적으로 로그인을 하면 아래와 같은 현황을 볼 수 있습니다.

현재 R1(10.146.0.8)이 Redis Master 역할을 하고 있으며 R2 ~ 4(10.146.0.9 ~ 11)이 Redis Slave 역할을 하고 있는 것을 확인 할 수 있습니다.


이제 R1(10.146.0.8) Redis Master 정지하여 상태를 확인해 보겠습니다.

Redis Master에서 아래와 같은 명령어로 Redis를 정지합니다.

$> sudo /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped



모니터링 페이지에서 확인 시 R1이 Down 되고 R4가 새로운 Master가 된 것을 확인할 수 있습니다.



HAProxy를 통한 Redis Data 입력 및 출력


Redis + HAProxy 구성 시 HAProxy는 Redis Master와 5000 Port로 통신합니다. 따라서 Client에서 Redis Master와 통신할 때

HAProxy의 5000 Port을 사용하여 통신 할 수 있습니다.


HAProxy로 접속하여 Redis Master에 정상적으로 데이터 입력 및 출력이 가능한지 확인합니다.
테스트를 위해 아래의 명령어를 현재의 Redis slave에 입력하여 HAProxy로 접속합니다. HAProxy로 접속하게 되면 HAProxy는 Redis Master로 리다이렉트 합니다.

$> redis-cli -10.146.0.12 -5000 -a mypassword
10.146.0.12:5000> info Replication
# Replication
role:master
connected_slaves:3
slave0:ip=10.146.0.9,port=6379,state=online,offset=2844848,lag=1
slave1:ip=10.146.0.10,port=6379,state=online,offset=2844712,lag=1
slave2:ip=10.146.0.8,port=6379,state=online,offset=2844848,lag=0
master_replid:8da97a8d28e5a1b6aaa3ef0995295098f364867f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2844985
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2645933
repl_backlog_histlen:199053
10.146.0.12:5000> 



Redis slave에서 데이터 입력 후 데이터가 정상적으로 출력이 되는지 확인합니다.

$> redis-cli -10.146.0.12 -5000 -a mypassword
10.146.0.12:5000> set myKey4 good!
OK
10.146.0.12:5000> get myKey4
"good!"



위와 같이 정상적으로 출력이 된다면 Redis + HAProxy 구성이 완료된 것 입니다.




참고 사이트

 + Redis에 대해 자세히 알게 해주셔서 너무나 감사합니다.




'[DB] > Redis' 카테고리의 다른 글

[DB] Redis Persistance - 데이터 저장  (0) 2017.11.13
[DB] Redis Sentinel 구성  (5) 2017.11.08
[DB] Redis Replication 구성  (0) 2017.11.07
[DB] Redis 설치  (0) 2017.11.06
profile

Ossian Story

@ossians