Study/Redis

[Redis] Redis Cluster

black6765 2022. 11. 15. 21:17

클러스터 관련 명령

  • 클러스터 관련 명령은 redis-cli --cluster [명령]과 같은 형식으로 하게 된다.
  • 클러스터에 접속하기 위해 IP:PORT 정보를 입력받는데, 입력한 IP:PORT에 해당하는 노드에게만 영향을 주는 것이 아닌, 그 노드에 접속해서 클러스터에 영향을 주는 것이라고 생각하면 된다.
    • ex) redis-cli --cluster del-node [IP:PORT] [노드 ID]
      • 이 명령어는 IP:PORT에 해당하는 노드를 삭제하는 것이 아닌, 노드 ID에 해당하는 노드를 삭제함
      • 즉, IP:PORT는 클러스터에 접속하기 위한 정보
    • redis-cli --cluster info [IP:PORT]와 같은 명령어도 해당하는 노드 뿐만 아니라 연결되어 있는 클러스터의 모든 정보를 가져오게 됨

 

info

  • redis-cli --cluster info [IP:PORT]
  • 클러스터의 정보를 확인한다.

 

reshard

  • redis-cli --cluster reshard [IP:PORT]
  • 슬롯을 재할당 한다.
  • 명령을 입력하면 옮길 slot의 개수, slot을 받을(receive) node의 ID, slot을 가져올 node(source node)의 ID(여러 개 입력 가능)를 입력받는다.
    • 가져올 노드의 ID를 “all”로 설정하면 모든 노드에서 일정 부분을 reshard 한다.

 

add-node

  • redis-cli --cluster add-node [새로 등록할 노드의 IP:PORT] [기존의 노드 IP:PORT]
    • 새로운 노드를 마스터로 클러스터에 등록함
  • redis-cli --cluster add-node [새로 등록할 노드의 IP:PORT] [기존의 노드 IP:PORT] –cluser-slave
    • 새로운 노드를 기존의 노드의 슬레이브로 등록함

 

del-node

  • redis-cli --cluster del-node [IP:PORT] [노드 ID]
    • 노드 ID에 해당하는 노드를 클러스터에서 삭제
    • 마스터 노드라면 해당 노드에 슬롯이 존재하지 않는 상태일 때 삭제 가능
      • reshard 명령을 통해 슬롯을 비워 놓아야 함
    • 슬레이브 노드는 바로 삭제 가능

 


Redis Cluster 환경 설정 - install_server.sh

Step 1.

  • Redis를 설치 후 “utils” 디렉토리로 이동하여 install_server.sh를 실행
  • 이때 systemd를 사용하는 경우 아래와 같은 문구가 출력되며 종료됨
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
  • 이 경우 해당 부분을 변경해야 함
#bail if this system is managed by systemd
_pid_1_exe="$(readlink -f /proc/1/exe)"
if [ "${_pid_1_exe##*/}" = systemd ]
then
        echo "This systems seems to use systemd."
        echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
        exit 1
fi
  • 이 부분을 모두 주석처리 하면 정상 실행됨

 

Step 2. install_server.sh에서의 설정

  • 스크립트를 실행시키면 여러 설정을 하게 된다.
  • Please select the redis port for this instance: [6379] 6500
    • 기본 포트는 6379로 설정되어 있고, 다른 포트를 입력 시 그 포트로 사용할 수 있음
    • 여기서는 “6500”으로 설정
  • Please select the redis config file name [/etc/redis/6500.conf]
    • 설정 파일의 이름과 위치를 설정함
    • 기본 이름은 “포트번호”.conf
  • Please select the redis log file name [/var/log/redis_6500.log]
    • 로그 파일의 이름과 위치를 설정함
    • 기본 이름은 redis_“포트번호”.log
  • Please select the data directory for this instance [/var/lib/redis/6500]
    • 데이터가 저장될 이름과 위치를 설정함
    • 기본 이름은 “포트번호”
  • Please select the redis executable path []
    • redis를 실행시킬 위치를 설정함
    • /usr/local/bin/”실행 명령어”로 두면 어느 위치에서나 “실행 명령어”를 통해 해당 인스턴스를 실행시킬 수 있음
    • 보통 “/usr/local/bin/redis-server”로 설정

 

Step 3. 클러스터링 할 인스턴스 설정

  • Step 2.의 과정을 반복하여 클러스터링 할 다수의 인스턴스를 생성한다.
    • 여기서는 6500, 6501, 6502 포트를 사용하여 클러스터링 설정
  • 그 후 /etc/redis/"포트번호".conf 파일의 설정을 다음과 같이 변경한다.
    • appendonly no → appendonly yes
      • 위의 세 개의 설정은 비슷한 위치에 모여있지만, appendonly는 조금 떨어져 있음
# cluster-enabled yes → cluster-enabled yes
# cluster-config-file nodes-6379.conf → cluster-config-file nodes.conf
# cluster-node-timeout 15000 → cluster-node-timeout 15000
  • 클러스터를 구성할 모든 포트 번호를 위와 같이 설정한다.
  • sudo service redis_”포트번호” restart 명령어로 재시작 시켜준다.
    • 이 과정을 실행하지 않으면 클러스터 생성 시 아래와 같은 문구가 출력되며 실패할 수 있음
      • [ERR] Node 127.0.0.1:6500 is not configured as a cluster node.

 

Step 4. 라이브러리 설치

  • Redis cluster 명령어를 사용하기 위해 다음과 같은 라이브러리를 설치한다.
    • ruby, ruby-devel, rubygems, rpm-build
    • sudo apt install ruby rubygems
    • gem install redis
    • 설치하지 않아도 문제 없이 클러스터 명령어가 사용된다면, 해당 부분은 넘어간다. (리눅스 환경에 따라 설치)

 

Step 5. 클러스터 생성

  • redis-cli --cluster create 127.0.0.1:6500 127.0.0.1:6501 127.0.0.1:6502
    • yes를 타이핑하면 클러스터링 완료
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 47741c09d75d72728b623554801aa98568b9dbdc 127.0.0.1:6500
   slots:[0-5460] (5461 slots) master
M: 59a9214e94f69a7b106478009c1332b3d1fc6394 127.0.0.1:6501
   slots:[5461-10922] (5462 slots) master
M: 27eb7dd5a54199a88ab0e3cb56fe663199ea75ed 127.0.0.1:6502
   slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept):
  • Redis 설정 정보의 위치는 아래와 같이 구성됨
    • 시작 및 종료 스크립트
      • /etc/init.d/redis_"포트번호"
    • 설정 파일 위치
      • /etc/redis/"포트번호".conf
    • dump 파일, nodes.conf 파일 위치
      • /var/lib/redis/"포트번호"
    • 로그 파일 위치
      • /var/log/redis_"포트번호".log

create-cluster utils를 이용하여 클러스터 설치

  • utils/create-cluster에 클러스터를 빠르게 설치할 수 있도록 하는 도구가 존재한다.

Step 1. create-cluster Settings 수정

  • 해당 파일에서 아래 부분을 수정한다
# Settings
BIN_PATH="$SCRIPT_DIR/../../src/"
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""
  • 위는 기본 설정으로, 이대로 진행 시 6개의 노드를 생성하게 된다.
  • REPLICAS는 각 노드 당 몇 개의 레플리카를 형성할 것인지 설정한다.
    • 6개의 노드에서 1로 설정되어 있을 때 3개는 마스터, 3개는 슬레이브로 설정된다.
    • 만약 6개의 노드에서 2로 설정되어 있다면, 각 노드 당 슬레이브가 2개로 설정되므로 마스터 2, 슬레이브 4로 구성된다
      • Redis Cluster는 최소 3개의 마스터가 필요하기 때문에 유효하지 않은 설정으로 에러가 발생한다
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 6 nodes and 2 replicas per node.
*** At least 9 nodes are required.

 

Step 2. create-cluster start

  • ./create-cluster start 명령을 통해 생성을 시작한다.
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
  • ps -ef | grep redis로 확인 시 방금 생성한(또는 실행 중인) Redis 인스턴스가 출력된다.

 

Step 3. create-cluster create

  • create 명령을 이용해 클러스터를 생성한다.
  • 위와 같은 기본 설정으로 실행 시 최종적으로 아래와 같은 로그가 출력된다.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 9d51a810038c84ab71428863bfaf453a18067109 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: 631007283d9b65dd20f55a099fa566952837e9a5 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: 322740e3a1b1b858b1258bd4e8b793fa7f578583 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: ac21998d354ae6fa3204647f0b500bec6a15a137 127.0.0.1:30004
   replicates 9d51a810038c84ab71428863bfaf453a18067109
S: 48f533c9867185605b15c1fff274282d073f6fe9 127.0.0.1:30005
   replicates 631007283d9b65dd20f55a099fa566952837e9a5
S: 5895b8d9b992eba2faf9aad5ab8364d6429d6e31 127.0.0.1:30006
   replicates 322740e3a1b1b858b1258bd4e8b793fa7f578583
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:30001)
M: 9d51a810038c84ab71428863bfaf453a18067109 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 48f533c9867185605b15c1fff274282d073f6fe9 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 631007283d9b65dd20f55a099fa566952837e9a5
M: 631007283d9b65dd20f55a099fa566952837e9a5 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 5895b8d9b992eba2faf9aad5ab8364d6429d6e31 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 322740e3a1b1b858b1258bd4e8b793fa7f578583
M: 322740e3a1b1b858b1258bd4e8b793fa7f578583 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ac21998d354ae6fa3204647f0b500bec6a15a137 127.0.0.1:30004
   slots: (0 slots) slave
   replicates 9d51a810038c84ab71428863bfaf453a18067109
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 총 6개의 노드에 3개의 마스터와 3개의 슬레이브로 구성된 클러스터 환경이 구성되었다.
  • 포트는 30000으로 지정했을 때 30001 ~ 30006으로 구성되었다.
  • 30001, 30002, 30003은 마스터, 나머지는 슬레이브로 구성된다.

 

Append. create-cluster의 여러 명령어

  • clean
    • create_cluster를 이용해 만들어진 클러스터는 utils/create-cluster 디렉토리에 설정파일이 존재한다.
    • /create-cluster clean 명령은 이러한 설정 파일들을 삭제하여 초기화 한다.
  • stop
    • 모든 클러스터를 정지시킨다.
  • start
    • 앞서 사용한 명령어로, 모든 클러스터를 시작한다.
  • 그 외
Usage: ./create-cluster [start|create|stop|watch|tail|tailall|clean|clean-logs|call]
start       -- Launch Redis Cluster instances.
create [-f] -- Create a cluster using redis-cli --cluster create.
stop        -- Stop Redis Cluster instances.
watch       -- Show CLUSTER NODES output (first 30 lines) of first node.
tail <id>   -- Run tail -f of instance at base port + ID.
tailall     -- Run tail -f for all the log files at once.
clean       -- Remove all instances data, logs, configs.
clean-logs  -- Remove just instances logs.
call <cmd>  -- Call a command (up to 7 arguments) on all n

참고 자료