Redis cluster集群搭建

了解准备

        Redis集群至少需要3个主节点,每个节点1个副本就至少需要6个节点。所以我们至少需要启动6个redis服务,
下面的操作在一个服务器上进行,redis版本为3.2.9。

创建服务

1,我们这里就用端口号来命令,分别存放各个端口的redis服务配置,这样方便我们管理区分,下面的配置基
于redis安装路径为/home/redis/redis-3.2.9

 

2,编辑6379端口的redis服务的配置,修改下面几项

编辑内容:

 

3,修改好第一个配置后,分别复制到其他几个文件夹中,然后修改好对应的端口

 

4,因为启动会产生对应的appendonly.aof和nodes.conf文件,所以到各自的文件夹中去启动他们

 

5,查看redis启动情况

输出:
root 13566 1 0 11:16 ? 00:00:01 redis-server 127.0.0.1:6380 [cluster]
root 13572 1 0 11:17 ? 00:00:01 redis-server 127.0.0.1:6381 [cluster]
root 13576 1 0 11:17 ? 00:00:01 redis-server 127.0.0.1:6379 [cluster]
root 13613 1 0 11:26 ? 00:00:00 redis-server 127.0.0.1:6384 [cluster]
root 13617 1 0 11:26 ? 00:00:00 redis-server 127.0.0.1:6383 [cluster]
root 13621 1 0 11:26 ? 00:00:01 redis-server 127.0.0.1:6382 [cluster]
root 13636 11880 0 11:49 pts/0 00:00:00 grep redis

安装ruby环境

 

 

创建集群

使用redis-trib.rb创建集群,在设置replicas参数的时候要注意,我们这里总共只有6个节点,所以设置主节点副本数量为1,否则节点不够用

输出:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes…
Using 3 masters:
127.0.0.1:6379
127.0.0.1:6380
127.0.0.1:6381
Adding replica 127.0.0.1:6382 to 127.0.0.1:6379
Adding replica 127.0.0.1:6383 to 127.0.0.1:6380
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
M: c6cc7be121ebb6c6e807688abf2c834fd89de335 127.0.0.1:6379
slots:0-5460 (5461 slots) master
M: c9f0e4e39033f6b3ee1d12d8aefe08146b355649 127.0.0.1:6380
slots:5461-10922 (5462 slots) master
M: 5c94aa30c66c19613463056a29ea5ec9163d6616 127.0.0.1:6381
slots:10923-16383 (5461 slots) master
S: bf63838dabd7f4c9b0dfb8d31f396f5b6f8d9896 127.0.0.1:6382
replicates c6cc7be121ebb6c6e807688abf2c834fd89de335
S: 52c94582500eba1bf119525660b63932ccb20ba3 127.0.0.1:6383
replicates c9f0e4e39033f6b3ee1d12d8aefe08146b355649
S: fb847c146679f59c22caa5b2c3023699ce307a6d 127.0.0.1:6384
replicates 5c94aa30c66c19613463056a29ea5ec9163d6616
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:6379)
M: c6cc7be121ebb6c6e807688abf2c834fd89de335 127.0.0.1:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 52c94582500eba1bf119525660b63932ccb20ba3 127.0.0.1:6383
slots: (0 slots) slave
replicates c9f0e4e39033f6b3ee1d12d8aefe08146b355649
M: 5c94aa30c66c19613463056a29ea5ec9163d6616 127.0.0.1:6381
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: fb847c146679f59c22caa5b2c3023699ce307a6d 127.0.0.1:6384
slots: (0 slots) slave
replicates 5c94aa30c66c19613463056a29ea5ec9163d6616
S: bf63838dabd7f4c9b0dfb8d31f396f5b6f8d9896 127.0.0.1:6382
slots: (0 slots) slave
replicates c6cc7be121ebb6c6e807688abf2c834fd89de335
M: c9f0e4e39033f6b3ee1d12d8aefe08146b355649 127.0.0.1:6380
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

检测集群

1,查看各个节点状态是否正常

>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: c6cc7be121ebb6c6e807688abf2c834fd89de335 127.0.0.1:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 52c94582500eba1bf119525660b63932ccb20ba3 127.0.0.1:6383
slots: (0 slots) slave
replicates c9f0e4e39033f6b3ee1d12d8aefe08146b355649
M: 5c94aa30c66c19613463056a29ea5ec9163d6616 127.0.0.1:6381
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: fb847c146679f59c22caa5b2c3023699ce307a6d 127.0.0.1:6384
slots: (0 slots) slave
replicates 5c94aa30c66c19613463056a29ea5ec9163d6616
S: bf63838dabd7f4c9b0dfb8d31f396f5b6f8d9896 127.0.0.1:6382
slots: (0 slots) slave
replicates c6cc7be121ebb6c6e807688abf2c834fd89de335
M: c9f0e4e39033f6b3ee1d12d8aefe08146b355649 127.0.0.1:6380
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

 

2,进入集群环境,使用创建key值测试 分配槽是否正常,下面测试的内容,已经将我设置的name=1234343放到了端口为6380的槽5798上了,访问集群的任何一台服务器都能获取这个槽的值

127.0.0.1:6379> set name 1234343
-> Redirected to slot [5798] located at 127.0.0.1:6380
OK
127.0.0.1:6380> get name
“1234343”

127.0.0.1:6380> get name
“1234343”

127.0.0.1:6381> get name
-> Redirected to slot [5798] located at 127.0.0.1:6380
“1234343”

3,检测主机宕机情况,kill掉一个主机,看是否有备机切换为主机,并登录到集群检查数据是否正常。

 

测试通过,那么一个简单的集群就完成了!

 

官网:https://redis.io/topics/cluster-tutorial


guolin

guolin

相信世界,向往美好,记录成长过程,分享个人心得,充实平凡生活。 网站信条:因为喜欢,所以热爱。

发表评论

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