Redis 시작하기 - Redis Sentinel & HAProxy
Redis Sentinel을 구성하고, slave의 loadbalance를 위해 haproxy를 구성합니다. haproxy의 UI화면에서 redis의 master/slave의 active/deactive를 상태를 한눈에 파악하기 쉽습니다.
Haproxy 환경 구성
HAProxy 설치를 위한 의존성 패키지 설치
# HAProxy 설치를 위한 의존성 패키지 설치
yum install -y gcc openssl openssl-devel pcre-static pcre-devel systemd-devel
Haproxy 설치
haproxy를 구성하기 위해 haproxy 최근 버전을 download하여 build 및 install합니다.
wget http://www.haproxy.org/download/2.8/src/devel/haproxy-2.8-dev7.tar.gz
tar xvzf haproxy-2.8-dev7.tar.gz
cd haproxy-2.8-dev7
make TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_ZLIB=1 USE_SYSTEMD=1
## Install haproxy
make install
아래의 명령어를 입력하여 설치된 HAProxy 버전을 확인합니다.
/usr/local/sbin/haproxy -v
OS가 reboot되어도 재시작하도록 service 파일을 작서합니다. 아래의 명령어를 입력하여 HAProxy 서비스 예제 파일을 다운로드합니다.
curl "http://git.haproxy.org/?p=haproxy-2.3.git;a=blob_plain;f=contrib/systemd/haproxy.service.in" -o /etc/systemd/system/haproxy.service
# 서비스파일 편집
vi /etc/systemd/system/haproxy.service
아래와 같이 서비스 파일에서 아래의 3개의 내용을 편집합니다.
ExecStartPre=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q -S /run/haproxy-master.sock
ExecStart=/usr/local/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
ExecReload=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q -S /run/haproxy-master.soc용
서비프파일 전체 내용
[Unit]
Description=HAProxy Load Balancer
After=network-online.target
Wants=network-online.target
[Service]
EnvironmentFile=-/etc/default/haproxy
EnvironmentFile=-/etc/sysconfig/haproxy
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid" "EXTRAOPTS=-S /run/haproxy-master.sock"
ExecStartPre=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q -S /run/haproxy-master.sock
ExecStart=/usr/local/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
ExecReload=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q -S /run/haproxy-master.soc용
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify
# The following lines leverage SystemD's sandboxing options to provide
# defense in depth protection at the expense of restricting some flexibility
# in your setup (e.g. placement of your configuration files) or possibly
# reduced performance. See systemd.service(5) and systemd.exec(5) for further
# information.
# NoNewPrivileges=true
# ProtectHome=true
# If you want to use 'ProtectSystem=strict' you should whitelist the PIDFILE,
# any state files and any other files written using 'ReadWritePaths' or
# 'RuntimeDirectory'.
# ProtectSystem=true
# ProtectKernelTunables=true
# ProtectKernelModules=true
# ProtectControlGroups=true
# If your SystemD version supports them, you can add: @reboot, @swap, @sync
# SystemCallFilter=~@cpu-emulation @keyring @module @obsolete @raw-io
[Install]
WantedBy=multi-user.target
haproxy 설정파일 수정
아래의 명령어를 입력하여 HAProxy 폴더를 생성합니다.
sudo mkdir -p /etc/haproxy
sudo mkdir -p /var/log/haproxy
sudo mkdir -p /etc/haproxy/certs
sudo mkdir -p /etc/haproxy/errors/
HAProxy 설정 파일에 아래와 같이 편집합니다.
#---------------------------------------------------------------------
# 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 localhost:6382 check
server tcpapp2 localhost:6383 check
server tcpapp3 localhost:6384 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 localhost:6382 check
server tcpapp2 localhost:6383 check
server tcpapp3 localhost:6384 check
#---------------------------------------------------------------------
# admin uI
#---------------------------------------------------------------------
listen stats
bind :80
mode http
balance roundrobin
maxconn 10
timeout client 5000
timeout connect 4000
timeout server 30000
# Enable stats page
stats enable
# Hide HAProxy version
stats hide-version
#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 admin:admin
#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를 서비스를 실행합니다.
systemctl daemon-reload
systemctl enable haproxy
systemctl start haproxy
HAProxy UI for redis
HAProxy UI를 접속하기 위해 아래의 Url http://localhost/haproxy_stats 에 접속하면 Redis master와 slave의 상태를 확인할 수 있습니다.
![haproxy]](07-cache-haproxy.png)
HAProxy 연결 테스트
Redis master에 접속합니다.
$redis-cli -h localhost -p 8000
localhost:8000> set k1 v1
Redis slave에 접속합니다.
$redis-cli -h localhost -p 8001
localhost:8001> get k1
Redis master 를 강제로 종료시키고 HAProxy UI화면을 refresh하면 master/slave정보가 변경된 것을 확인할 수 있습니다. redis-cli를 사용해 slave에 연결해서 계속해서 조회가 가능합니다.
댓글남기기