Redis Cluster的搭建(3.0.3)单机和双机

Posted qinjinhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis Cluster的搭建(3.0.3)单机和双机相关的知识,希望对你有一定的参考价值。

转发自我自己的新浪博客:​​http://blog.sina.com.cn/s/blog_4fa20e8c0102vv2z.html​

Redis Cluster的安装和配置

Centos6.5-64位minimal版

yum -y install gcc gcc-c++

redis3.0.3稳定版

1,下载后解压安装

make后报错zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory

那么就执行

sudo make MALLOC=libc

sudo yum -y install rubygems

sudo gem install redis

后来再redis/src/找到redis-trib.rb,把它copy出来到redis目录下

在/usr/local下创建redis6379-redis6384这6个目录

只把redis.conf复制进去

其中更改

daemonize yes

port 6379 #不同目录,这里端口对应不同

cluster-enabled yes #开启集群功能

cluster-config-file nodes-6379.conf #节点配置文件改成相对应的,方便区分,这个文件是服务启动时自己配置创建的

cluster-node-timeout 5000

appendonly yes

2,然后各个启动

如6379端口

sudo /usr/local/bin/redis-server /usr/local/redis6379/redis.conf

3,cd /usr/local

sudo ./redis/redis-trib.rb create --replicas 1 192.168.1.25:6379 192.168.1.25:6380 192.168.1.25:6381 192.168.1.25:6382 192.168.1.25:6383 192.168.1.25:6384

以上命令各个是一主一从

如果不要从节点就把1改为0

sudo ./redis-trib.rb check 192.168.1.25:6379

在创建集群里redis会自动执行这样的检测,当然你也可以自己运行这个check,端口(节点)也是任一其中一个

结果

Connecting to node 192.168.1.25:6382: OK

Connecting to node 192.168.1.25:6383: OK

Connecting to node 192.168.1.25:6381: OK

Connecting to node 192.168.1.25:6384: OK

Connecting to node 192.168.1.25:6379: OK

Connecting to node 192.168.1.25:6380: OK

>>> Performing Cluster Check (using node 192.168.1.25:6382)

S: a5d5cc17b2d8ec750f8ded14f65dc81cc66c5746 192.168.1.25:6382

   slots: (0 slots) slave

   replicates 08e6d391504848ba7f6cf2e9f072795f5aa82217

S: f229c055e1b91267c2e6d56b5beb0bc53651abdb 192.168.1.25:6383

   slots: (0 slots) slave

   replicates 63d6c99314fd126dc93954e75d12ff9225f5ddc9

M: 9550183cb43e9bd094650b7df7be8cc1f605b521 192.168.1.25:6381

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 2cbe081d89607b4dbdcd054d9ffd9cd59291883d 192.168.1.25:6384

   slots: (0 slots) slave

   replicates 9550183cb43e9bd094650b7df7be8cc1f605b521

M: 08e6d391504848ba7f6cf2e9f072795f5aa82217 192.168.1.25:6379

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 63d6c99314fd126dc93954e75d12ff9225f5ddc9 192.168.1.25: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.

4,#查看集群节点的相关信息

redis-cli -p 6379 -h 192.168.1.25 cluster nodes

63d6c99314fd126dc93954e75d12ff9225f5ddc9 192.168.1.25:6380 master - 0 1440649609665 2 connected 5461-10922

9550183cb43e9bd094650b7df7be8cc1f605b521 192.168.1.25:6381 master - 0 1440649608663 3 connected 10923-16383

f229c055e1b91267c2e6d56b5beb0bc53651abdb 192.168.1.25:6383 slave 63d6c99314fd126dc93954e75d12ff9225f5ddc9 0 1440649608162 5 connected

a5d5cc17b2d8ec750f8ded14f65dc81cc66c5746 192.168.1.25:6382 slave 08e6d391504848ba7f6cf2e9f072795f5aa82217 0 1440649609164 4 connected

08e6d391504848ba7f6cf2e9f072795f5aa82217 192.168.1.25:6379 myself,master - 0 0 1 connected 0-5460

2cbe081d89607b4dbdcd054d9ffd9cd59291883d 192.168.1.25:6384 slave 9550183cb43e9bd094650b7df7be8cc1f605b521 0 1440649607662 6 connected

5,#官方文档里列表参数的说明:

* Node ID

* ip:port

* flags: master, slave, myself, fail, ...

* if it is a slave, the Node ID of the master

* Time of the last pending PING still waiting for a reply.

* Time of the last PONG received.

* Configuration epoch for this node (see the Cluster specification).

* Status of the link to this node.

* Slots served...

./redis-trib.rb check 192.168.1.25:6379

6,#在创建集群里redis会自动执行这样的检测,当然你也可以自己运行这个check,端口(节点)也是任一其中一个

Connecting to node 192.168.1.25:6382: OK

Connecting to node 192.168.1.25:6383: OK

Connecting to node 192.168.1.25:6381: OK

Connecting to node 192.168.1.25:6384: OK

Connecting to node 192.168.1.25:6379: OK

Connecting to node 192.168.1.25:6380: OK

>>> Performing Cluster Check (using node 192.168.1.25:6382)

S: a5d5cc17b2d8ec750f8ded14f65dc81cc66c5746 192.168.1.25:6382

   slots: (0 slots) slave

   replicates 08e6d391504848ba7f6cf2e9f072795f5aa82217

S: f229c055e1b91267c2e6d56b5beb0bc53651abdb 192.168.1.25:6383

   slots: (0 slots) slave

   replicates 63d6c99314fd126dc93954e75d12ff9225f5ddc9

M: 9550183cb43e9bd094650b7df7be8cc1f605b521 192.168.1.25:6381

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 2cbe081d89607b4dbdcd054d9ffd9cd59291883d 192.168.1.25:6384

   slots: (0 slots) slave

   replicates 9550183cb43e9bd094650b7df7be8cc1f605b521

M: 08e6d391504848ba7f6cf2e9f072795f5aa82217 192.168.1.25:6379

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 63d6c99314fd126dc93954e75d12ff9225f5ddc9 192.168.1.25: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.

7,以下除了测试部分,并没有亲测

#添加节点的测试,需求:把6382这个节点作为slave添加到6379这个master节点上

#命令如下:(两个命令都可行)

./redis-trib.rb add-node --slave 192.168.1.25:6382 192.168.1.25:6379

./redis-trib.rb add-node --slave master-id 5dfa7d9f2dbb4ab64b71f2713538125354c88462 192.168.1.25:6382 192.168.1.25:6379

---------------------------------------------------------------------------------------

#检测集群节点相关信息

redis-cli -p 6379 -h 192.168.1.25 cluster nodes

---------------------------------------------------------------------------------------

c31fb6f027db72876cb36e606b93817736e0ce81 192.168.1.25:6382 slave 5dfa7d9f2dbb4ab64b71f2713538125354c88462 0 1416901290565 1 connected

47b1c1df820ff5e9057a526f69e9fe4fdd9800e9 192.168.1.25:6381 master - 0 1416901293638 3 connected 10923-16383

c97f77ebcba3f48b9da07329b0e4e2943ded88c9 192.168.1.25:6380 master - 0 1416901291590 2 connected 5461-10922

5dfa7d9f2dbb4ab64b71f2713538125354c88462 192.168.1.25:6379 myself,master - 0 0 1 connected 0-5460

#根据官方文档列表参数说明的第4行说明

#5dfa7d9f2dbb4ab64b71f2713538125354c88462就是master的NodeID

#那么我们再看6379节点的NodeID正好也是5dfa7d9f2dbb4ab64b71f2713538125354c88462

#如果需要改变主从关系,比如将6383这个服务节点从master(6380)转变成master(6379),那么如下

redis-cli -p 6383 -h 192.168.1.25

192.168.1.25:6383> cluster replicate 5dfa7d9f2dbb4ab64b71f2713538125354c88462

#5dfa7d9f2dbb4ab64b71f2713538125354c88462是master(6379)的NodeID

8,测试

redis-cli -c -p 6379 -h 192.168.1.25

set fool bar

以下会有提示

-->Redirected to slot [12182] located at 192.168.1.25:6381

ok

然后可以用get fool在其他的端口下登录验证

-> Redirected to slot [12182] located at 42.62.4.83:6381

"bar"

至此已经完成

借鉴自​​http://www.oschina.net/code/snippet_1046345_44151​


二,如果是在两个主机上cluster,那么另外的主机也要装gcc、gcc-c++、ruby、gem redis、redis。

其他的配置是一样的,但是端口不要一致,比如A主机用的6379-6381,那么B主机就用6382-6384。

防火墙都要相应的开放6379-6384、16379-16384(集群发送和接收消息端口)。

在25一台机子上执行即可

sudo ./redis/redis-trib.rb create --replicas 1 192.168.1.25:6379 192.168.1.25:6380 192.168.1.25:6381 192.168.1.26:6382 192.168.1.26:6383 192.168.1.26:6384

如果报错比如[ERR] Not all 16384 slots are covered by nodes. (官方推荐redis-trib.rb fix 192.268.1.25:6379来修复,但是我试了不起作用)

尝试把cluster-config-file指向的文件都删掉,启动redis进程,再执行重建,如果依然不行,就把dump.rdp、appendonly.aof文件也删掉,启动redis再执行重建。



以上是关于Redis Cluster的搭建(3.0.3)单机和双机的主要内容,如果未能解决你的问题,请参考以下文章

redis cluster单机伪分布式搭建--- 3主3从3哨兵集群

非常适合新手的redis cluster搭建过程

虚拟机搭建redis单机版及redis-cluster,使用redis desktop manager和java(eclipse)连接redis过程遇到问题汇总

NoSQL-redis-单机多实例,伪分布式环境搭建

Redis Cluster 介绍与搭建

Redis Cluster集群的搭建