Redis Cluster

本文基于 Redis 5.0.5

首先安装 Redis

wget http://download.redis.io/releases/redis-5.0.5.tar.gz

tar xzf redis-5.0.5.tar.gz

cd redis-5.0.5/

sudo make && make install

修改配置文件 redis.conf

# 同一台机器上,每个 Redis 实例的端口要不一样
port 7000

# 开启实例的集群模式
cluster-enabled yes

# 保存节点配置文件的路径
cluster-config-file nodes.conf

# 节点间通信的超时时间
cluster-node-timeout 5000

# 采用 AOF 
appendonly yes

拷贝并配置集群实例

cd ..
mkdir -p redis-cluster/{7000,7001,7002,7003,7004,7005}

把 redis.conf 拷贝到上面建立的数字目录下,修改相应的端口号为目录名。

建立一个启动集群的脚本 start-cluster.sh

#!/bin/bash

cd ./7000 && redis-server redis.conf

cd ../7001 && redis-server redis.conf

cd ../7002 && redis-server redis.conf

cd ../7003 && redis-server redis.conf

cd ../7004 && redis-server redis.conf

cd ../7005 && redis-server redis.conf

cd ..

再建立一个粗暴停止集群的脚本shutdown-cluster.sh

#!?bin/bash

ps -ef | grep redis-serve[r] | awk '{print $2}' | xargs -exec kill -9

启动所有实例后创建集群,三个主节点各带一个从节点:

ubuntu@VM-15-78-ubuntu:~/redis/redis-cluster$ redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
>>> 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:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: f1a5db8416619c93b96669edd630841e851a9edf 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
M: bcd86a4637ccde99d097f1ed02785ae8099a8094 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: e62850a3b7bee767e5f599480483705b4743bb5b 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: 51d1f1f81ab83ab335e774035518d0a41b3307cd 127.0.0.1:7003
   replicates f1a5db8416619c93b96669edd630841e851a9edf
S: 774d705cabd67755f4e95841240b52b9504f28f6 127.0.0.1:7004
   replicates bcd86a4637ccde99d097f1ed02785ae8099a8094
S: eaab05cf216b0948017c8f055b1cf1c8f8b0f1aa 127.0.0.1:7005
   replicates e62850a3b7bee767e5f599480483705b4743bb5b
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:7000)
M: f1a5db8416619c93b96669edd630841e851a9edf 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 51d1f1f81ab83ab335e774035518d0a41b3307cd 127.0.0.1:7003
   slots: (0 slots) slave
   replicates f1a5db8416619c93b96669edd630841e851a9edf
M: bcd86a4637ccde99d097f1ed02785ae8099a8094 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eaab05cf216b0948017c8f055b1cf1c8f8b0f1aa 127.0.0.1:7005
   slots: (0 slots) slave
   replicates e62850a3b7bee767e5f599480483705b4743bb5b
M: e62850a3b7bee767e5f599480483705b4743bb5b 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 774d705cabd67755f4e95841240b52b9504f28f6 127.0.0.1:7004
   slots: (0 slots) slave
   replicates bcd86a4637ccde99d097f1ed02785ae8099a8094
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

然后查看下集群节点的状态 redis-cli -c -p 7000 cluster nodes

ubuntu@VM-15-78-ubuntu:~/redis/redis-cluster$ redis-cli -c -p 7000 cluster nodes
51d1f1f81ab83ab335e774035518d0a41b3307cd 127.0.0.1:7003@17003 slave f1a5db8416619c93b96669edd630841e851a9edf 0 1561520522545 4 connected
bcd86a4637ccde99d097f1ed02785ae8099a8094 127.0.0.1:7001@17001 master - 0 1561520523000 2 connected 5461-10922
eaab05cf216b0948017c8f055b1cf1c8f8b0f1aa 127.0.0.1:7005@17005 slave e62850a3b7bee767e5f599480483705b4743bb5b 0 1561520522000 6 connected
f1a5db8416619c93b96669edd630841e851a9edf 127.0.0.1:7000@17000 myself,master - 0 1561520521000 1 connected 0-5460
e62850a3b7bee767e5f599480483705b4743bb5b 127.0.0.1:7002@17002 master - 0 1561520523046 3 connected 10923-16383
774d705cabd67755f4e95841240b52b9504f28f6 127.0.0.1:7004@17004 slave bcd86a4637ccde99d097f1ed02785ae8099a8094 0 1561520522044 5 connected

— 向集群添加新的节点
redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7007 --cluster-replicas 1

— 迁移slot到新节点
redis-cli --cluster reshard 127.0.0.1:7006 --cluster-from bcd86a4637ccde99d097f1ed02785ae8099a8094 --cluster-to 12758e5525bd853f834a6d8f437211b5c91d31a1 --cluster-slots 108

— 对于 master,需要先把slot迁移到其他节点
redis-cli --cluster del-node 127.0.0.1:7006 12758e5525bd853f834a6d8f437211b5c91d31a1


欢迎关注我的微信公众号: coderbee笔记,可以更及时回复你的讨论。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据