본문 바로가기

시스템/REDIS

[REDIS] Sentinel + Haproxy + Keepalived를 이용한 Redis HA LoadBalance 구축 (2)

반응형

구축 정보


Spec

  • 서버 3대 CentOS7 x86_64 
  • TEST1 : 10.10.10.240, Redis Slave
  • TEST2 : 10.10.10.241, Redis Master
  • TEST3 : 10.10.10.242, Redis Slave
  • VIP : 10.10.10.243
  • Redis Write Port 5000
  • Redis Read Port 5001

 버전

  • Keepalived : keepalived-1.3.5-19.el7.x86_64
  • Haproxy : haproxy-1.5.18-9.el7_9.1.x86_64
  • Redis : 4.0.9

 설치

  • Redis설치는 여기 참조.
  • Keepalived, Haproxy는 yum 패킷지 관리자로 설치
yum install keepalived haproxy

 

설정


Keepalived

  • TEST1, TEST2, TEST3 동일 구성
cat /etc/keepalived/keepalived.conf
# Snmp 모니터링
global_defs {
  enable_snmp_checker
  enable_snmp_rfcv3
  enable_snmp_rfcv2
  enable_snmp_vrrp
  enable_snmp_rfc
  enable_traps
}
# Haproxy Process Tracking
vrrp_script check_haproxy {
	script "/bin/killall -0 haproxy"
	 # script 체크 간격
    interval 2
	weight 2
     # down 확정을 위한 트래킹 시도 횟수
	fall 3
    # up 확정을 위한 트래킹 시도 횟수
	rise 3 
}
vrrp_instance VI_01 {
	#nopreempt
	state EQUAL
    # 설정할 인터페이스
	interface eno16780032 
    # VI_01 id
	virtual_router_id 41
    # 우선순위
	priority 1 
    # 광고 간격
	advert_int 2 
    # router id 에 속한 멤버 인증
	authentication {
        auth_type PASS
		auth_pass 1234
    	}
    # vip
	virtual_ipaddress {
		10.10.10.243
	}
    # haproxy tracking 설정
	track_script {
		check_haproxy
	}
}

 

Haproxy

  • TEST1 + TEST2 + TEST3 동일 구성
global
	# syslog 설정
	log		127.0.0.1:514 local6
    # max connection 설정
	maxconn		65536
	chroot		/var/lib/haproxy
	pidfile		/run/haproxy.pid
	user		haproxy
	group		haproxy
	daemon
	#stats	socket	/run/haproxy-master.sock mode 777 level admin expose-fd listeners
	#nbproc 2
        #nbthread       4
        #cpu-map auto:1/1-4      32-35

defaults
	log		global
	maxconn		65536
	mode		tcp
	timeout	connect	5s
	timeout	client	3600s
	timeout	server	3600s
#	option		dontlognull
	option		tcplog
	option		clitcpka
	option		tcpka
	option 		log-health-checks
	rate-limit	sessions	200
	#balance	roundrobin

# haproxy 모니터링 설정
frontend stats
    bind 10.10.10.240:8414
    mode http
    stats enable
    stats uri /stats
    stats refresh 10s
    stats hide-version
    #stats auth admin:1234
    no log

frontend ft_redis_master
	bind *:5000 name redis
	default_backend bk_redis_master

backend bk_redis_master
	# tcp-check의 expect 를 통한 redis의 master 여부 체크
	option tcp-check
	tcp-check connect port 6379
	tcp-check send AUTH\ *****\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.10.10.240:6379 check inter 5s
	server R2 10.10.10.241:6379 check inter 5s
	server R3 10.10.10.242:6379 check inter 5s

frontend ft_redis_slave
	bind *:5001 name redis
	default_backend bk_redis_slave

backend bk_redis_slave
	# tcp-check의 expect 를 통한 redis의 slave 여부 체크
	option tcp-check
	tcp-check connect port 6379
	tcp-check send AUTH\ *****\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.10.10.240:6379 check inter 5s
	server R2 10.10.10.241:6379 check inter 5s
	server R3 10.10.10.242:6379 check inter 5s

 

Sentinel

  • TEST1 + TEST2 + TEST3 동일 구성
bind 0.0.0.0
port 26379
sentinel monitor redis-cluster 10.10.10.241 6379 2
sentinel down-after-milliseconds redis-cluster 5000
sentinel failover-timeout redis-cluster 10000
sentinel auth-pass redis-cluster ******
daemonize yes
pidfile "/var/run/redis_sentinel.pid"
dir "/var/lib/redis/sentinel"
logfile "/var/log/sentinel.log"

 

systemd 등록

cat /usr/lib/systemd/system/redis-sentinel.service 
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
Wants=network-online.target
After=network-online.target

[Service]
Environment="CONFFILE=/etc/redis/sentinel.conf"
EnvironmentFile=-/etc/sysconfig/redis-sentinel
Type=forking
Restart=on-failure
PIDFile=/var/run/redis_sentinel.pid
KillMode=control-group
ExecStart=/usr/local/bin/redis-sentinel $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s


[Install]
WantedBy=multi-user.target

 

Redis

  • TEST1 + TEST2 + TEST3 Replication 제외 동일 구성
  • 설정은 여기 참조.
### General ###
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis_6379.log"
databases 16
always-show-logo yes

### RDB ###

save 900 1
save 300 10
save 60 10000

stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/var/lib/redis/6379"

### REPLICATION ###
...
### SECURITY ###

requirepass "*****"

### CLIENTS ###

maxclients 10000

### LAZY ###

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no

### AOF ###

appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no

### LUA ###

lua-time-limit 5000

### SLOW LOG ###

slowlog-log-slower-than 10000
slowlog-max-len 128

### LATENCY MONITOR ###
latency-monitor-threshold 0

### ADVANCE ###
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

 

  • TEST1 + TEST3 Replication : Slave
### REPLICATION ###
slaveof 10.10.10.241 6379
masterauth "*****"
slave-serve-stale-data yes
slave-read-only yes

repl-diskless-sync no
slave-priority 10

repl-ping-slave-period 10
repl-timeout 60

 

  • TEST2 Replication : Master
### REPLICATION ###

masterauth "*****"
slave-serve-stale-data yes
slave-read-only yes

repl-diskless-sync no
slave-priority 90

repl-ping-slave-period 10
repl-timeout 60

 

 

 

상태 확인


  • Redis : TEST2 Master
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.10.10.240,port=6379,state=online,offset=4784414,lag=0
slave1:ip=10.10.10.242,port=6379,state=online,offset=4784414,lag=0
master_replid:ce3317cd239dc44d82e412e154543a68f7b774e1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4784414
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3735839
repl_backlog_histlen:1048576

 

  • Sentinel : TEST2 Master
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=redis-cluster,status=ok,address=10.10.10.241:6379,slaves=2,sentinels=3

 

반응형