当前使用redis-4.0.9版本来创建集群,我这里演示的是3主3从模式,我就不开6台主机了,6个节点全部在一台机器上模拟集群了,所使用系统Centos7.4
1、下载redis-4.0.9
1 2 |
cd /usr/local/src wget https://download.redis.io/releases/redis-4.0.9.tar.gz |
2、编译安装
1 2 3 4 |
tar zxf redis-4.0.9.tar.gz cd redis-4.0.9 make make install |
3、创建一个目录
1 |
mkdir -p /redis_cluster/{7000,7001,7002,7003,7004,7005} |
为了方便使用redis-trib.rb,将它复制到bin下
1 |
cp src/redis-trib.rb /usr/local/bin/ |
4、修改redis配置
1 |
cp redis.conf redis.conf.bak |
1 |
vi redis.conf |
修改以下内容
1 2 3 4 5 6 7 8 9 |
port 7000 #修改端口 bind 本机ip #默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端 口,无法创建集群 daemonize yes #redis后台运行 pidfile /var/run/redis_7000.pid #pidfile文件对应7000,7001,7002,7003,7004,7005 cluster-enabled yes #开启集群 把注释#去掉 cluster-config-file nodes_7000.conf #集群的配置,配置文件首次启动自动生成7000,7001,7002,7003,7004,7005 cluster-node-timeout 15000 #请求超时默认15秒,可自行设置 appendonly yes #aof日志开启有需要就开启,它会每次写操作都记录一条日志 |
将redis.conf拷贝到/redis_cluster目录,并修改相应端口
1 2 |
for ((i=0;i<=5;i++)); do cp redis.conf /redis_cluster/700$i/; done for ((i=0;i<=5;i++)); do sed -i s#7000#700$i#g /redis_cluster/700$i/redis.conf; done |
5、启动redis
1 |
for((i=0;i<=5;i++)); do /usr/local/bin/redis-server /redis_cluster/700$i/redis.conf; done |
6、安装ruby
参考:https://blog.whsir.com/post-2659.html
7、创建集群
1 |
redis-trib.rb create --replicas 1 192.168.0.35:7000 192.168.0.35:7001 192.168.0.35:7002 192.168.0.35:7003 192.168.0.35:7004 192.168.0.35:7005 |
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.0.35:7000
192.168.0.35:7001
192.168.0.35:7002
Adding replica 192.168.0.35:7004 to 192.168.0.35:7000
Adding replica 192.168.0.35:7005 to 192.168.0.35:7001
Adding replica 192.168.0.35:7003 to 192.168.0.35:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 717cae314299e942a3fc7d94412de1a8c2223e6c 192.168.0.35:7000
slots:0-5460 (5461 slots) master
M: 9fd24e5b7e4a538df67bc5112f30f3d01adfc1e8 192.168.0.35:7001
slots:5461-10922 (5462 slots) master
M: 0a77d9e742803eb8e8e791cfdc8c0619226209ce 192.168.0.35:7002
slots:10923-16383 (5461 slots) master
S: f23e097b9ebedcff9cbb455247fb010745f8c21b 192.168.0.35:7003
replicates 0a77d9e742803eb8e8e791cfdc8c0619226209ce
S: 2d3329d4406dc3b7b169697d95911c45aacb5d0c 192.168.0.35:7004
replicates 717cae314299e942a3fc7d94412de1a8c2223e6c
S: e82577366e8402b60b1172ca085af070d61193ea 192.168.0.35:7005
replicates 9fd24e5b7e4a538df67bc5112f30f3d01adfc1e8
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 192.168.0.35:7000)
M: 717cae314299e942a3fc7d94412de1a8c2223e6c 192.168.0.35:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 2d3329d4406dc3b7b169697d95911c45aacb5d0c 192.168.0.35:7004
slots: (0 slots) slave
replicates 717cae314299e942a3fc7d94412de1a8c2223e6c
M: 0a77d9e742803eb8e8e791cfdc8c0619226209ce 192.168.0.35:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: f23e097b9ebedcff9cbb455247fb010745f8c21b 192.168.0.35:7003
slots: (0 slots) slave
replicates 0a77d9e742803eb8e8e791cfdc8c0619226209ce
M: 9fd24e5b7e4a538df67bc5112f30f3d01adfc1e8 192.168.0.35:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: e82577366e8402b60b1172ca085af070d61193ea 192.168.0.35:7005
slots: (0 slots) slave
replicates 9fd24e5b7e4a538df67bc5112f30f3d01adfc1e8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
8、检查集群状态
1 |
redis-trib.rb check 192.168.0.35:7000 |
9、列出集群节点
1 |
redis-cli -c -h 192.168.0.35 -p 7000 |
192.168.0.35:7000> cluster nodes
10、打印集群信息
至此redis cluster搭建完成
redis cluster在设计的时候,就考虑到了去中心化、去中间件,集群中的每个节点都是平等关系,每个节点都保存各自的数据和整个集群的状态。
redis集群没有并使用传统的一致性哈希来分配数据,而是采用另外一种叫做哈希槽(hash slot)的方式来分配的。redis cluster默认分配了16384个槽(slot),当我们set一个key时,会用CRC16算法来取模得到所属的槽(slot),然后将这个key分到哈希槽区间的节点上,具体算法就是:CRC16(key)%16384。
redis集群会把数据存在一个master节点,然后在这个master和其对应的salve之间进行数据同步。当一个master挂掉之后,会启动一个对应的salve节点,充当master。
原文链接:redis集群搭建,转载请注明来源!