[linux] redis clustering (redis-trib 이용)

redis 3.0 이상 필요.

설치하고 난 뒤에 다음과 같이 redis.conf를 변경해줍니다.

daemonize yes

pidfile /var/run/redis.pid

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 0

loglevel notice

logfile “/services/log/redis/redis.log”

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

dir /services/data/redis/

slave-serve-stale-data yes

slave-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

slave-priority 100

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

lua-time-limit 5000

cluster-enabled yes

cluster-node-timeout 2000

cluster-slave-validity-factor 1

cluster-config-file nodes.conf

slowlog-log-slower-than 10000

slowlog-max-len 128

latency-monitor-threshold 0

notify-keyspace-events “”

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

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

그 다음 루비루비젬을 설치해주고, redis 를 설치해줘야 한다그래야 클러스터 관련 스크립트가 실행됨 (include redis)

yum install ruby rubygem

gem install –http-proxy http://10.10.10.100:8080 redis

이후 레디스 소스파일이 위치한 경로에 src 폴더안에 redis-trib.rb 를 이용하면된다.

명령어

for i in $(cat centos_redis30.txt);do ssh $i “hostname;yum install ruby rubygem -y”;done

for i in $(cat centos_redis30.txt);do ssh $i “hostname;gem install –http-proxy http://10.10.10.100:8080 redis “;done

설정파일은 master / slave 구분없이 동일하므로 scp로 넣어준다.

설정 파일 복사 후 적용을 위해서 service redis restart

그다음 1번 서버에서 다음과 같은 명령어를 날려준다.

echo yes | /services/src/redis-3.0.1/src/redis-trib.rb create –replicas 1 10.10.10.21:6379 10.10.10.22:6379 10.10.10.23:6379 10.10.10.24:6379 10.10.10.25:6379 10.10.10.26:6379

(클러스터링 및 리플리카를  M M M S S S 순으로 넣어줍니다. )

레디스 클러스터링에서는

사용 포트가 서비스포트 + 10000번이 자동 할당된다.

해당 포트를 통해서 redis clustering command 가 실행되며해당 포트를열지 않으면클러스터링이 안됨.

.

Redis-trib 사용법

Redis-trib 사용법

http://download.redis.io/redis-stable/src/redis-trib.rb

명령 설명

o    create   클러스터를 생성한다. replicas 지정해서 슬레이브 개수를 지정할  있다.

o    reshard   슬롯을 노드에 할당 또는 재할당한다. Source 노드와 destination 노드를 지정한다.

o    add-node   클러스터에 노드를 추가한다마스터 또는 슬레이브로 추가할  있다.

o    del-node   클러스터에서 노드를 제거한다.

redis-trib CREATE

o    클러스터를 생성한다.

o    명령   redis-trib.rb create [–replicas n] ip1:port1 … ipn:portn

o    –replicas n   마스터  슬레이브 노드를   만들지를 정한다.   생략하거나 0으로 설정하면 슬레이브 노드를 만들지 않고 마스터로만 클러스터를 구성한다.  

o    ip1:port1 … ipn:portn   클러스터에 참여할 레디스 노드들의 ip:port 입력한다.   명령을 실행하는 머신과 같은 ip 사용해도 ip 반드시 입력해야 한다.

o    Standalone mode 시작한 레디스는 클러스터에 참여할  없다.   지정한 ip:port  standalone mode Redis-trib 종료한다.

o    DB 데이터가 있으면 클러스터를 생성하지 못하고 종료한다.   그러므로 클러스터를 생성할 노드는 AOF  RDB 부터 데이터를 읽어들이면 안된다.   Cluster mode 시작한 레디스는 클러스터가 구성되지 않은 상태에서는 set 명령같은 데이터를 입력/수정/삭제/조회하는 명령을 수행할  없다.   하지만 Flushdb 또는 flushall 명령은 실행가능하다 명령을 실행해서 데이터를 모두 삭제했어도 클러스터는 구성되지 않는다처음부터 데이터를 읽어 들이지 않는 방법이 가장 좋다.

o    레디스 노드는 최소 3 이상을 지정해야 한다.   Replicas 0으로 지정하면 참여한 노드가 모두 마스터가 된다.   Replicas 1 지정하면 최소 마스터슬레이브 3쌍으로 구성되어 6노드 이상이 있어야 한다.   Cluster 명령을 직접 사용하면 이러한 제한은 없어서, 1 노드로도 클러스터를 만들  있다.

o    IP 같은 경우 클러스터 구성   IP 같으면 명령에 지정된 순서대로 마스터와 슬레이브가 정해진다.

o    다음과 같이 replicas 1 지정하고 5001부터 5006까지 포트를 지정하면 아래와 같이 구성된다.

$ src/redis-trib.rb create –replicas 1 127.0.0.1:5001 127.0.0.1:5002 127.0.0.1:5003 127.0.0.1:5004 127.0.0.1:5005 127.0.0.1:5006

설명: Redis Cluster Create redis_trib

o    replicas 2 지정하고 9 노드를 지정하면 다음과 같이 구성된다.

설명: Redis Cluster Create redis-trib replicas 2

o    3 IP 레디스 노드 2개씩 지정하고 replicas 1 하면 다음과 같이 구성된다.   세번째 박스(IP 105)  박스안에 마스터슬레이브가 구성되었으므로  박스가 다운되면 클러스터가 다운되는 상황이 된다.

설명: Redis Cluster Create redis_trib 3boxes 6nodes

o    redis-trib 클러스터 구성을   하려고 하지만완벽하지 않아서 위와 같은 상황이 발생할  있으므로세밀한 구성을 위해서는 클러스터 명령으로 직접 구성하는 것이 좋은 방법이다.

o    redis-trib create했을때 구성 형태에 대한 자세한 내용은 여기를 보세요.   레디스 클러스터 자동 구성 형태

o    다음은 6 노드로 마스터 3, 슬레이브 3으로 구성했을때 나오는 메시지이다.   redis 3.0.2 포한된 redis-trib.rb 실행한 것이다.    길지만 생략하지 않고 그대로 보여준다.

$ src/redis-trib.rb create –replicas 1 127.0.0.1:5001 127.0.0.1:5002 127.0.0.1:5003 127.0.0.1:5004 127.0.0.1:5005 127.0.0.1:5006

>>> Creating cluster

Connecting to node 127.0.0.1:5001: OK

Connecting to node 127.0.0.1:5002: OK

Connecting to node 127.0.0.1:5003: OK

Connecting to node 127.0.0.1:5004: OK

Connecting to node 127.0.0.1:5005: OK

Connecting to node 127.0.0.1:5006: OK

>>> Performing hash slots allocation on 6 nodes…

Using 3 masters:

127.0.0.1:5001

127.0.0.1:5002

127.0.0.1:5003

Adding replica 127.0.0.1:5004 to 127.0.0.1:5001

Adding replica 127.0.0.1:5005 to 127.0.0.1:5002

Adding replica 127.0.0.1:5006 to 127.0.0.1:5003

M: a2c1abd1e2db46fe31beed7e4d40f81cc8abe28d 127.0.0.1:5001

     slots:0-5460 (5461 slots) master

M: ed6274c72384c208154e9f06e3c40a7f893197f6 127.0.0.1:5002

     slots:5461-10922 (5462 slots) master

M: 4c385777933bf54eb0636ebe7bda4903e0da0ade 127.0.0.1:5003

     slots:10923-16383 (5461 slots) master

S: 813e57347d2de9f3a9f36f7846642a5732c1519e 127.0.0.1:5004

     replicates a2c1abd1e2db46fe31beed7e4d40f81cc8abe28d

S: cfd9827e915e79c4550afddad2b546c3d1298872 127.0.0.1:5005

     replicates ed6274c72384c208154e9f06e3c40a7f893197f6

S: d324144ecfb33f75ef5e90b47388423f0656ab95 127.0.0.1:5006

     replicates 4c385777933bf54eb0636ebe7bda4903e0da0ade

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:5001)

M: a2c1abd1e2db46fe31beed7e4d40f81cc8abe28d 127.0.0.1:5001

     slots:0-5460 (5461 slots) master

M: ed6274c72384c208154e9f06e3c40a7f893197f6 127.0.0.1:5002

     slots:5461-10922 (5462 slots) master

M: 4c385777933bf54eb0636ebe7bda4903e0da0ade 127.0.0.1:5003

     slots:10923-16383 (5461 slots) master

M: 813e57347d2de9f3a9f36f7846642a5732c1519e 127.0.0.1:5004

     slots: (0 slots) master

     replicates a2c1abd1e2db46fe31beed7e4d40f81cc8abe28d

M: cfd9827e915e79c4550afddad2b546c3d1298872 127.0.0.1:5005

     slots: (0 slots) master

     replicates ed6274c72384c208154e9f06e3c40a7f893197f6

M: d324144ecfb33f75ef5e90b47388423f0656ab95 127.0.0.1:5006

     slots: (0 slots) master

     replicates 4c385777933bf54eb0636ebe7bda4903e0da0ade

[OK] All nodes agree about slots configuration.

>>> Check for open slots…

>>> Check slots coverage…

[OK] All 16384 slots covered.

redis-trib RESHARD

o    슬롯을 재할당한다슬롯과 데이터를 같이 옮겨준다.

o    명령   redis-trib.rb reshard ip:port

o    ip:port 접속해서 작업할 노드이다. Reshard  source 노드나 destination 노드로 접속할 필요는 없다.

o    Reshard  노드를 추가했을때  노드에 슬롯을 할당하기 위해 사용하거나기존 노드의 슬롯을 다른 노드로 옮길때 사용한다.   Reshard 명령은 destination 노드로 데이터를 migrate 명령으로 옮긴다.

o    Source 노드는 1 이상 또는 모두를 지정할  있고, destination 노드는 하나만 지정한다.   옮길 슬롯의 개수를 지정한다특정 슬롯을 지정하거나 특정 범위를 지정할  없다.

o    다음은 reshard할때 나오는 메시지이다옮길 슬롯의 갯수만큼 메시지가 나오므로여기서는 5 슬롯만 지정했다.   Source node #1 에서 all 입력하면 모든 노드로 부터 슬롯을 받는다.   특정 노드 id 여러개 입력할  있다모두 입력했으면 done 입력한다.   Source node  Destination(Receiving) node 모두 마스터 노드만 지정해야 된다슬레이브 노드는 지정할  없다.   입력해야  부분은 굵게 표시했다.

redis-trib.rb reshard 127.0.0.1:5001

>>> Performing Cluster Check (using node 127.0.0.1:5001)

[OK] All nodes agree about slots configuration.

>>> Check for open slots…

>>> Check slots coverage…

[OK] All 16384 slots covered.

How many slots do you want to move (from 1 to 16384)? 5

What is the receiving node ID? 4c385777933bf54eb0636ebe7bda4903e0da0ade

Please enter all the source node IDs.

  Type ‘all’ to use all the nodes as source nodes for the hash slots.

  Type ‘done’ once you entered all the source nodes IDs.

Source node #1:a2c1abd1e2db46fe31beed7e4d40f81cc8abe28d

Source node #2:done

Ready to move 5 slots.

Do you want to proceed with the proposed reshard plan (yes/no)? yes

Moving slot 0 from 127.0.0.1:5001 to 127.0.0.1:5003:

Moving slot 1 from 127.0.0.1:5001 to 127.0.0.1:5003:

Moving slot 2 from 127.0.0.1:5001 to 127.0.0.1:5003:

Moving slot 3 from 127.0.0.1:5001 to 127.0.0.1:5003:

Moving slot 4 from 127.0.0.1:5001 to 127.0.0.1:5003:

redis-trib ADD-NODE

o    클러스터에 노드를 추가한다마스터 또는 슬레이브로 추가할  있다.

o    명령   redis-trib.rb add-node [–slave] [–master-id id] new_ip:port existing_ip:port

o    –slave    노드를 슬레이브로 추가할  사용한다지정하지 않으면 마스터로 추가된다.

o    –master-id id   slave 옵션을 사용했을때 마스터 노드 ID 지정한다생략할  있다생략하면 existing_ip:port 노드의 슬레이브가 된다.

o    new_ip:port   추가할 ip:port 지정한다. Cluster mode 시작되어 있어야 하고데이터가 없어야 한다.

o    existing_ip:port   접속해서 작업할 기존 노드를 지정한다.

o    다음은 5007 노드를 마스터로 클러스터에 등록하는 명령과 메시지이다.   중간에 입력해야 하는 것은 없다.   이후에 reshard 명령으로 슬롯을 할당해야 마스터로서 역할을 수행할  있다.

redis-trib.rb add-node 127.0.0.1:5007 127.0.0.1:5001

>>> Adding node 127.0.0.1:5007 to cluster 127.0.0.1:5001

>>> Performing Cluster Check (using node 127.0.0.1:5001)

[OK] All nodes agree about slots configuration.

>>> Check for open slots…

>>> Check slots coverage…

[OK] All 16384 slots covered.

Connecting to node 127.0.0.1:5007: OK

>>> Send CLUSTER MEET to node 127.0.0.1:5007 to make it join the cluster.

[OK] New node added correctly.

o    다음은 5007 노드를 5002 슬레이브로 등록하는 명령이다마지막에 다른 부분만 표시한다.  

redis-trib.rb add-node –slave 127.0.0.1:5007 127.0.0.1:5002

>>> Adding node 127.0.0.1:5007 to cluster 127.0.0.1:5002

….. 중간 생략 …..

[OK] All 16384 slots covered.

Automatically selected master 127.0.0.1:5002

Connecting to node 127.0.0.1:5007: OK

>>> Send CLUSTER MEET to node 127.0.0.1:5007 to make it join the cluster.

Waiting for the cluster to join.

>>> Configure node as replica of 127.0.0.1:5002.

[OK] New node added correctly.

o    다음은 master-id 옵션을 지정한 명령 예이다.

$ redis-trib.rb add-node –slave –master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 127.0.0.1:5007 127.0.0.1:5001

redis-trib DEL-NODE

o    클러스터에서 노드를 제거한다.   슬롯이 할당되어 있으면 제거할  없다.   Reshard 명령으로 슬롯과 데이터를 다른 노드에 할당한  del-node 명령을 실행한다.   제거할 노드가 슬레이브면 reshard  필요없다.

o    명령   redis-trib.rb del-node ip:port node-id

o    ip:port   접속해서 작업할 노드를 지정한다.

o    node-id   제거할 노드 ID 지정한다.

o     명령은 마지막에 레디스 인스턴스를 shutdown 한다.

o    다운된 노드에 대해서는  명령을 실행할  없다.   왜냐하면  명령은 해당 노드에 접속해서 확인  진행하는데접속할  없기 때문이다.   이때는 redis-cli 접속해서 cluster forget node-id 명령으로 제거한다.

o    다음은 5001번에 접속해서 5007 노드를 제거하는 명령과 메시지다.

redis-trib.rb del-node 127.0.0.1:5001 b8b8b72fb2b5305ad63290bb1aa24cf81fce1bc2

>>> Removing node b8b8b72fb2b5305ad63290bb1aa24cf81fce1bc2 from cluster 127.0.0.1:5001

>>> Sending CLUSTER FORGET messages to the cluster…

>>> SHUTDOWN the node.

원본 위치 <http://www.redisgate.com/redis/cluster/redis-trib.php>

확인 작업

아래 생성된 슬롯수 및 ok가 일치하면 (클러스터링 개당 슬롯이 분리되어있음)

Nodes 수가 설정값과 일치하는지 known nodes 확인

Size 에서 클러스터링 값을 확인

[root@ip-10-24-20-62 redis]# redis-cli -h localhost

localhost:6379> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:4

cluster_stats_messages_sent:4481

cluster_stats_messages_received:4440

localhost:6379>

클러스터 노드수는 아래와 같이 확인

localhost:6379> cluster nodes

글쓴이