首页 » Linux » CentOS » redis集群搭建

redis集群搭建

 

当前使用redis-4.0.9版本来创建集群,我这里演示的是3主3从模式,我就不开6台主机了,6个节点全部在一台机器上模拟集群了,所使用系统Centos7.4

1、下载redis-4.0.9

2、编译安装

3、创建一个目录

为了方便使用redis-trib.rb,将它复制到bin下

4、修改redis配置

修改以下内容

将redis.conf拷贝到/redis_cluster目录,并修改相应端口

5、启动redis

6、安装ruby

参考:https://blog.whsir.com/post-2659.html

7、创建集群

>>> 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、检查集群状态

9、列出集群节点

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集群搭建,转载请注明来源!

0