Redis Clustering 구성
2020/01/10 - [NoSQL/Redis] - Redis 특징
2020/01/10 - [NoSQL/Redis] - Docker Redis
2020/01/10 - [NoSQL/Redis] - Spring Boot Redis (standard)
2020/01/10 - [NoSQL/Redis] - Redis install linux
2020/01/10 - [NoSQL/Redis] - Redis Replication (Master Slave) 구성
2020/01/10 - [NoSQL/Redis] - Redis Sentinel 구성
2020/01/10 - [NoSQL/Redis] - Redis Clustering 구성
2020/01/13 - [NoSQL/Redis] - Spring boot redis Sentinel Config
2020/01/13 - [NoSQL/Redis] - spring boot redis clustering config
Cluster란 각기 다른 서버를 하나로 묶어 하나의 시스템 처럼 동작하게 함으로 클라이언트에게 고가용성(HA)을 제공하는것을 의미한다.
- 데이터를 여러대의 장비에서 처리함으로 특정데이터에 집중되는 트레픽을 서버가 나누어 처리함.
- 서버일부분이 장애가 일어나도 다른 서버의 보완을 통해 서비스를 계속 이어 나갈수 있음
- 데이터 유실 최소화
cluster mode on
클러스터 모드로 수정한 후 재시작 한다. nodes-{port}.conf 파일은 /var/lib/redis 하위에 포트별 디렉터레에 자동으로 생성된다.
1. Master 로만 구성한 Clustering
master node로 사용할 redis instance 3벌 생성
# 6379 ~ 6381
cd utils
sudo ./install_server.sh
1.1Master Node 환경 파일 수정
cd /etc/redis
sudo vi 6379.conf ~ 6381.conf
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
# 재시작
sudo service redis_6379 restart
sudo service redis_6380 restart
sudo service redis_6381 restart
1.2 Master Node clustering 설정
명령어 실행후 위구성 내용으로 설정하겠느냐는 질문에 yes
sudo redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: dc3803213aff6f279f6344559c8147198227aacc 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
M: b79c0edafd6f6c24d29ffa922105363ff9dc3737 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
M: 18b188cb18a61acaa61dc1b7e068602c1d41e308 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: dc3803213aff6f279f6344559c8147198227aacc 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
M: b79c0edafd6f6c24d29ffa922105363ff9dc3737 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
M: 18b188cb18a61acaa61dc1b7e068602c1d41e308 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
cluster로 연결하려 할 때 노드에 데이터가 들어있을 경우 아래와 같은 메시지가 나올 수 있습니다. 이때는 데이터를 전부 삭제하고 진행하면 됩니다
redis-cli --cluster create 127.0.0.1:6300 127.0.0.1:6301 127.0.0.1:6302
[ERR] Node 127.0.0.1:6300 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
redis-cli -p 6300
127.0.0.1:6300> flushall
OK
1.3 데이터 입력 및 확인
redis-cli cluster 모드로 진입하여 데이터 입력
redis-cli -c -p 6379
127.0.0.1:6379> set key1 value2
-> Redirected to slot [9189] located at 127.0.0.1:6380
OK
127.0.0.1:6380> set key2 value2
-> Redirected to slot [4998] located at 127.0.0.1:6379
OK
127.0.0.1:6379> set key3 value3
OK
127.0.0.1:6379> set key4 value4
-> Redirected to slot [13120] located at 127.0.0.1:6381
OK
127.0.0.1:6381> set key5 value5
-> Redirected to slot [9057] located at 127.0.0.1:6380
OK
127.0.0.1:6380>
참고 : Redis 설정 정보 위치
시작 종료 스크립트 – /etc/init.d/redis_{포트번호}
conf 파일 위치 – /etc/redis/{포트번호}.conf
dump파일, nodes.conf 파일 위치 – /var/lib/redis/{포트번호}
로그 파일 위치 – /var/log/redis_{포트번호}.log
2. Master / Slave clustering 설정
cluster를 master로만 사용하다보면 특정 master 가 중단되었을대 문제가 생길 수 있다. 이에 클러스터도 master/slave로 구성하며 사용한다.
클러스터 구성시 여러 대의 장비로 구성하는 경우 각 장비의 노드간 통신하기 위해 특정 포트가 열려 있어야한다. 해당 포트는 Redis 인스턴스 Port+10000 번이다. 6379 포트면 TCP Port 16379가 열려 있어야 한다.
2. Master / Slave 구조의 Clustering
2.1 Slave Node 생성
Slave Node를 생성 하기 위해 Master Node를 생성했듯이 redis Instance 를 더만들어 준다.
# 6479 ~ 6481 port
cd utils
sudo ./install_server.sh
2.2 Slave config
slave conf 파일도 cluster 설정으로 변경
cd /etc/redis
sudo vi 6479.conf ~ 6481.conf
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
#재시작
sudo service redis_6479 restart
sudo service redis_6480 restart
sudo service redis_6481 restart
2.3 Master에 Slave 추가
Master Node 에 slave Node를 추가 한다
#redis-cli --cluster add-node {slaveip}:{port} {masterIP}:{port} --cluster-slave
redis-cli --cluster add-node 127.0.0.1:6479 127.0.0.1:6379 --cluster-slave
redis-cli --cluster add-node 127.0.0.1:6480 127.0.0.1:6380 --cluster-slave
redis-cli --cluster add-node 127.0.0.1:6481 127.0.0.1:6381 --cluster-slave
...
>>> Adding node 127.0.0.1:6481 to cluster 127.0.0.1:6381
>>> Performing Cluster Check (using node 127.0.0.1:6381)
M: 18b188cb18a61acaa61dc1b7e068602c1d41e308 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
M: dc3803213aff6f279f6344559c8147198227aacc 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: e6ce132080d73f29cbc1906085e3c08a6f3fbb01 127.0.0.1:6479
slots: (0 slots) slave
replicates dc3803213aff6f279f6344559c8147198227aacc
M: b79c0edafd6f6c24d29ffa922105363ff9dc3737 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: f7feddf46163b235125d8466ccc596c890f1a8d4 127.0.0.1:6480
slots: (0 slots) slave
replicates b79c0edafd6f6c24d29ffa922105363ff9dc3737
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Automatically selected master 127.0.0.1:6381
>>> Send CLUSTER MEET to node 127.0.0.1:6481 to make it join the cluster.
Waiting for the cluster to join
...
# cluster 정보 확인
redis-cli --cluster info 127.0.0.1:6379
127.0.0.1:6379 (dc380321...) -> 4 keys | 5461 slots | 1 slaves.
127.0.0.1:6380 (b79c0eda...) -> 3 keys | 5462 slots | 1 slaves.
127.0.0.1:6381 (18b188cb...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 8 keys in 3 masters.
0.00 keys per slot on average.
6개의 Redis 노드를 가지고 Cluster를 구성하지 않았다면 다음 명령으로 한번에 Master-Slave 구조의 Cluster를 구성 할 수 있다.
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6479 127.0.0.1:6480 127.0.0.1:6481 --cluster-replicas 1
option 설졍
--cluster-replicas 1 : 각 노드마다 몇대의 Slave가 붙을지 설정 (Slave node 1개 )