码迷,mamicode.com
首页 > 其他好文 > 详细

redis(9)集群搭建

时间:2018-07-06 23:28:32      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:4.0   time   编译   install   启动进程   显示   节点   for   red   

一、搭建流程

redis集群至少需要3个redis实例,不过官方推荐采用6个实例;3个master,3个slave。

那么我们需要做以下几件事:

1)编译一个redis-server

2)创建6个redis.conf配置文件

3)启动6个redis实例

4)redis-trib.rb创建集群

 

二、创建集群

1)编译一个redis-server

$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
$ tar xzf redis-4.0.10.tar.gz
$ cd redis-4.0.10
$ make

详细可以参考:https://www.cnblogs.com/lay2017/p/9248454.html

 

2)创建6个redis.conf配置文件

我们先创建以下文件夹目录:

mkdir redis-cluster
cd redis-cluster
mkdir 7000 7001 7002 7003 7004 7005

然后我们将第一步编译出来的src/redis-server拷贝一份到redis-cluster下,然后把redis.conf拷贝6份分别放入到上面创建的6个文件夹内。

分别修改每个redis.conf的内容:

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes 

这里的port分别设置为:7000、7001、7002、7003、7004、7005

 

3)启动6个redis实例

./redis-server 7000/redis.conf

分别执行启动命令,加载redis.conf配置文件

查看一下启动进程:

ps -ef | grep redis

显示如下:

  501 37933     1   0  9:10PM ??         0:03.85 ../redis-server 127.0.0.1:7000 [cluster] 
  501 37941     1   0  9:11PM ??         0:03.82 ../redis-server 127.0.0.1:7001 [cluster] 
  501 37949     1   0  9:11PM ??         0:03.82 ../redis-server 127.0.0.1:7002 [cluster] 
  501 37968     1   0  9:11PM ??         0:03.75 ../redis-server 127.0.0.1:7003 [cluster] 
  501 37977     1   0  9:11PM ??         0:03.76 ../redis-server 127.0.0.1:7004 [cluster] 
  501 37988     1   0  9:12PM ??         0:03.76 ../redis-server 127.0.0.1:7005 [cluster] 

则表示每个实例已经启动成功

 

4)redis-trib.rb创建集群

在src目录下自带了redis-trib.rb可以用于创建集群

但是在使用之前 需要安装ruby,以及redis和ruby连接

yum -y install ruby ruby-devel rubygems rpm-build
gem install redis

然后采用redis-trib.rb创建集群

redis-trib.rb create --replicas 1  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 127.0.0.1:7006

如果你最终看到:

[OK] All 16384 slots covered

说明集群创建完成

我们可以用命令检查一下目前的集群情况

./redis-trib.rb check 127.0.0.1:7000

输出内容如下:

>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: ca34d6741d5b3f5bfc7941c652a09961fddb6d0a 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: e04be0db2d11fb98133c163bd97426e0f3f2f540 127.0.0.1:7003
   slots: (0 slots) slave
   replicates ca34d6741d5b3f5bfc7941c652a09961fddb6d0a
M: 048c96b56c043aab8d297b147cfe7068c74498c9 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: b711233443a7a097511fc1681403f2d83b5c8773 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: cb6ee10ec8a45f68efddc84b4acaf01fbd244627 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 048c96b56c043aab8d297b147cfe7068c74498c9
S: b630796514a48438d177fbe8f89610f1afe9b41f 127.0.0.1:7004
   slots: (0 slots) slave
   replicates b711233443a7a097511fc1681403f2d83b5c8773
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

我们看到:

  master主节点是:7000、7001、7002,7003是7000的从节点、7004是7001的从节点、7005是7002的从节点;

  7000分配的哈希槽[0,5460],7001分配的哈希槽[5461,10922],7002分配的哈希槽[10923,16383]

 

5)测试

5-1、测试设置值、取值

我们使用redis-cli命令连接7000,并设置一个值:

lay-macdeAir:redis-cluster lay$ ./redis-cli -c -p 7000
127.0.0.1:7000> set name lay
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK

我们连接7001,执行获取这个值:

lay-macdeAir:redis-cluster lay$ ./redis-cli -c -p 7001
127.0.0.1:7001> get name
"lay"

我们发现我们从7001中获取7000设置的值,集群的数据共享成功。

 

5-2、测试主从切换

当前有6个实例

  501 37933     1   0  9:10PM ??         0:03.85 ../redis-server 127.0.0.1:7000 [cluster] 
  501 37941     1   0  9:11PM ??         0:03.82 ../redis-server 127.0.0.1:7001 [cluster] 
  501 37949     1   0  9:11PM ??         0:03.82 ../redis-server 127.0.0.1:7002 [cluster] 
  501 37968     1   0  9:11PM ??         0:03.75 ../redis-server 127.0.0.1:7003 [cluster] 
  501 37977     1   0  9:11PM ??         0:03.76 ../redis-server 127.0.0.1:7004 [cluster] 
  501 37988     1   0  9:12PM ??         0:03.76 ../redis-server 127.0.0.1:7005 [cluster] 

我们先杀死7000

lay-macdeAir:redis-cluster lay$ kill -9 37933
lay-macdeAir:redis-cluster lay$ ps -ef|grep redis
  501 37941     1   0  9:11PM ??         0:05.08 ../redis-server 127.0.0.1:7001 [cluster] 
  501 37949     1   0  9:11PM ??         0:05.08 ../redis-server 127.0.0.1:7002 [cluster] 
  501 37968     1   0  9:11PM ??         0:05.02 ../redis-server 127.0.0.1:7003 [cluster] 
  501 37977     1   0  9:11PM ??         0:05.00 ../redis-server 127.0.0.1:7004 [cluster] 
  501 37988     1   0  9:12PM ??         0:05.01 ../redis-server 127.0.0.1:7005 [cluster]

检查现在集群情况

lay-macdeAir:redis-cluster lay$ ./redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: b711233443a7a097511fc1681403f2d83b5c8773 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: cb6ee10ec8a45f68efddc84b4acaf01fbd244627 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 048c96b56c043aab8d297b147cfe7068c74498c9
M: 048c96b56c043aab8d297b147cfe7068c74498c9 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: e04be0db2d11fb98133c163bd97426e0f3f2f540 127.0.0.1:7003
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
S: b630796514a48438d177fbe8f89610f1afe9b41f 127.0.0.1:7004
   slots: (0 slots) slave
   replicates b711233443a7a097511fc1681403f2d83b5c8773
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

我们看到7000挂掉以后它的从节点7003提升为了主节点,并且所有16384个哈希槽都覆盖了

 

以上我们简单实现了redis-cluster模块的集群效果,参考官方文档:https://redis.io/topics/cluster-tutorial

 

redis(9)集群搭建

标签:4.0   time   编译   install   启动进程   显示   节点   for   red   

原文地址:https://www.cnblogs.com/lay2017/p/9275677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!