프로그램/LINUX

[Redis] 레디스 클러스터, Slave 세팅 및 설정하기 (Master/Slave)

프뚜 2023. 1. 11. 10:00
728x90
반응형
SMALL

안녕하세요! 프뚜입니다.

 

우분투에 redis 설치하는 방법은 링크를 통해 참고하시면 됩니다.

Redis7000은 port 7000을 사용하고 있습니다. Redis7011(Replica) 연결되어있습니다.

Redis7001은 port 7001을 사용하고 있습니다. Redis7012(Replica) 연결되어있습니다.

Redis7002은 port 7002을 사용하고 있습니다. Redis7010(Replica) 연결되어있습니다.

Redis7010은 port 7010을 사용하고 있습니다. Redis7002(Master) 연결되어있습니다.

Redis7011은 port 7011을 사용하고 있습니다. Redis7000(Master) 연결되어있습니다.

Redis7012은 port 7012을 사용하고 있습니다. Redis7001(Master) 연결되어있습니다.


[개발환경]

 - OS: Ubuntu 22.04.1 LTS


# redis.conf를 복사해서 redis-70??.conf 생성하기 (00, 01, 02, 10, 11, 12)

# 2023.01.06[프뚜]: 디렉토리 지정
dir /etc/redis/node/

# 2023.01.06[프뚜]: 로그파일 생성
logfile redis-7000.log

# 2023.01.03[프뚜]: 모든 IP 접속 허용
bind 0.0.0.0 ::1

# 2023.01.03[프뚜]: 접속 PORT 지정
port 7000

# 2023.01.03[프뚜]: 클러스터 여부
cluster-enabled yes

# 2023.01.03[프뚜]: conf
cluster-config-file nodes_7000.conf

# 2023.01.03[프뚜]: 클러스터 timeout
cluster-node-timeout 3000

# 2023.01.03[프뚜]: cluster 일부가 다운했을 때, 운영 여부
cluster-require-full-coverage no

redis.conf를 복사해서 redis-7000.conf, redis-7001.conf, redis-7002.conf, redis-7010.conf, redis-7011.conf, redis-7012.conf 생성합니다. logfile, port, cluster-config-file 옵션은 각 port에 맞게 수정합니다.


# Redis(7000, 7001, 7002, 7010, 7011, 7012) 구동하기

redis-server redis-7000.conf
redis-server redis-7001.conf
redis-server redis-7002.conf
redis-server redis-7010.conf
redis-server redis-7011.conf
redis-server redis-7012.conf

클러스터로 6개 구동을 확인할 수 있습니다.


# Cluster 연결하기

redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7010 127.0.0.1:7011 127.0.0.1:7012 --cluster-replicas 1

--cluster-replicas n: 마스터 당 슬레이브 노드를 몇 개 만들지를 정합니다.  생략하거나 0으로 설정하면 슬레이브 노드를 만들지 않고 마스터로만 클러스터를 구성합니다. 레디스 노드는 최소 3개 이상을 지정해야합니다. Replicas를 0으로 지정하면 참여한 노드가 모두 마스터가 됩니다. Replicas를 1로 지정하면 최소 마스터-슬레이브 3쌍으로 구성되어 6노드 이상이 있어야 한다. Cluster 명령을 직접 사용하면 이러한 제한은 없어서, 1개 노드로도 클러스터를 만들 수 있습니다.


# 값 추가하기

# 2023.01.06[프뚜]: 1번 master에 접속
redis-cli -c -p 7000

# 2023.01.06[프뚜]: 값 바인딩
set name pddu

# 2023.01.06[프뚜]: 5798 슬롯에 저장됨
# Redirected to slot [5798] located at 127.0.0.1:7001

# 2023.01.06[프뚜]: 2번 master에 값 확인
get name


# master shutdown 후 slave 확인

# 2023.01.06[프뚜]: 5798 슬롯에 있는 master 서버 다운
redis-cli -p 7001 shutdown

# 2023.01.06[프뚜]: 1번 master 접속
redis-cli -c -p 7000

# 2023.01.06[프뚜]: 2번 master에 있던 값 찾기
get name

# 2023.01.06[프뚜]: 3번 replica가 master를 대신함
# Redirected to slot [5798] located at 127.0.0.1:7012

redis-7001(master) 서버가 다운된 후 2번 서버에 있던 값을 찾게 되면 3번 replica가 master를 대신하게 됩니다.


docker pull ghcr.io/jeongseongsoo/redis:1.2

https://github.com/users/JeongSeongSoo/packages/container/package/redis

728x90
반응형
LIST