该文使用centos6.5 64位  redis3.2.8

一、  redis-cluster架构图

集群通信:所有redis节点之间通过PING-PONG机制彼此互联,内部使用二进制鞋子优化传输速度和带宽。

集群数据存储(哈希槽):

(1)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value
(2)Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。

举例说明数据如何选择存储在哪台redis服务器上:

现在有3台redis服务器的集群:redis Server1(0-5000)  redis Server1(5001-10000)  redis Server1(10001-16384)

存储数据:set key A

A存储在哪个节点上:

计算A的hash值,例如值为98,98这个槽在redis Server1上,所以A应该放到redis Server1。

节点之间如何确定某个节点挂掉:容错机制

判断节点是否挂掉:某个节点(黄色节点)给红色节点发送PING命令,但未得带PONG回应,于是该节点怀疑红色节点已经挂掉,于是他把这个猜想通知给其他节点,其他节点就会向红色节点发送PING命令,如果没有PONG回应,则也认为红色节点已经挂掉,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉。

什么时候整个集群不可用(cluster_state:fail):

a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完整时进入fail状态,所以一般我们会为每一个master节点做从节点slave 。ps : redis-3.0.0.rc1加入cluster-require-full-coverage参数,默认关闭,打开集群兼容部分失败.

b:如果集群超过半数以上master挂掉,无论是否有slave,集群进入fail状态.

ps:当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误

二、搭建redis集群

集群中有三个节点的集群,每个节点有一主一备。需要6台虚拟机。

在这里我们搭建一个伪分布式的集群,使用6个redis实例来模拟。

1、集群需要ruby环境:

安装ruby
yum install ruby
yum install rubygems

2.查看redis集群管理工具redis-trib.rb是否存在

[root@localhost src]# pwd
/tools/redis-3.2.8/src
[root@localhost src]# ll *.rb
-rwxrwxr-x. 1 root root 60852 2月 12 23:14 redis-trib.rb
[root@localhost src]#

3、安装redis-trib.rb脚本运行依赖的环境 redis-3.2.2.gem

   安装ruby的包:gem install redis-3.2.2.gem

4、创建6个redis实例

[root@localhost local]# pwd
/usr/local
[root@localhost local]# mkdir redis-cluster
[root@localhost local]# cp -r redis redis-cluster/redis01 //复制我们安装的单机版redis实例

//删除复制后的 dump.rdb 文件

[root@localhost bin]# pwd
  /usr/local/redis-cluster/redis01/bin

[root@localhost bin]# rm -rf dump.rdb

//修改配置文件 port    与 cluster-enabled
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 7001
################################ REDIS CLUSTER  ###############################
#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
# in order to mark it as "mature" we need to wait for a non trivial percentage
# of users to deploy it in production.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes //开启集群功能

将redis01 复制5份

[root@localhost redis-cluster]# cp -r redis01/ redis02
[root@localhost redis-cluster]# cp -r redis01/ redis03
[root@localhost redis-cluster]# cp -r redis01/ redis04
[root@localhost redis-cluster]# cp -r redis01/ redis05
[root@localhost redis-cluster]# cp -r redis01/ redis06

修改他们的端口为 redis02:7002    redis03:7003    redis04:7004    redis05:7005    redis06:7006

5、将集群依赖的ruby脚本redis-trib.rb 移动到  /usr/local/redis-cluster 目录下便于使用

  [root@localhost src]# pwd
/tools/redis-3.2.8/src
[root@localhost src]# cp *.rb /usr/local/redis-cluster

6、启动6各redis实例 使用脚本启动

[root@localhost redis-cluster]# vim startRedis.sh

cd redis01/bin
./redis-server ../etc/redis.conf
cd ../../
cd redis02/bin
./redis-server ../etc/redis.conf
cd ../../
cd redis03/bin
./redis-server ../etc/redis.conf
cd ../../
cd redis04/bin
./redis-server ../etc/redis.conf
cd ../../
cd redis05/bin
./redis-server ../etc/redis.conf
cd ../../
cd redis06/bin
./redis-server ../etc/redis.conf
cd ../../

赋予该文件可执行权限 :[root@localhost redis-cluster]# chmod +x startRedis.sh

7、启动脚本并查看6各redis实例是否启动成功

[root@localhost redis-cluster]# ./startRedis.sh
[root@localhost redis-cluster]# ps -ef|grep redis
root 1634 1 0 17:11 ? 00:00:00 ./redis-server 0.0.0.0:7001 [cluster]
root 1637 1 0 17:11 ? 00:00:00 ./redis-server 0.0.0.0:7003 [cluster]
root 1644 1 0 17:11 ? 00:00:00 ./redis-server 0.0.0.0:7005 [cluster]
root 1939 1 0 17:15 ? 00:00:00 ./redis-server 0.0.0.0:7002 [cluster]
root 1943 1 0 17:15 ? 00:00:00 ./redis-server 0.0.0.0:7004 [cluster]
root 1951 1 0 17:15 ? 00:00:00 ./redis-server 0.0.0.0:7006 [cluster]
root 1955 1463 0 17:15 pts/0 00:00:00 grep redis

8、创建集群

[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.6.190:7001 192.168.6.190:7002 192.168.6.190:7003 192.168.6.190:7004 192.168.6.190:7005 192.168.6.190:7006

[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.6.190:7001 192.168.6.190:7002 192.168.6.190:7003 192.168.6.190:7004 192.168.6.190:7005 192.168.6.190:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.6.190:7001
192.168.6.190:7002
192.168.6.190:7003
Adding replica 192.168.6.190:7004 to 192.168.6.190:7001
Adding replica 192.168.6.190:7005 to 192.168.6.190:7002
Adding replica 192.168.6.190:7006 to 192.168.6.190:7003
M: 6e8c35d55d3699afe0b10317cf9c199e7df1ae50 192.168.6.190:7001
slots:0-5460 (5461 slots) master //slots槽的分配
M: 4cc06e1060369a16f70d4f8097c065bf886162a3 192.168.6.190:7002
slots:5461-10922 (5462 slots) master
M: 532355022ea3473834726e7512270025b7eddae3 192.168.6.190:7003
slots:10923-16383 (5461 slots) master
S: de789ed503705e7a5a2e5fc41adb7ed3290de08c 192.168.6.190:7004
replicates 6e8c35d55d3699afe0b10317cf9c199e7df1ae50
S: c1261800c3a7e74ff3b2fda22b07ef994f7db4a7 192.168.6.190:7005
replicates 4cc06e1060369a16f70d4f8097c065bf886162a3
S: 07910c0d839e4fcd6139598edd4953fcd32adb29 192.168.6.190:7006
replicates 532355022ea3473834726e7512270025b7eddae3
Can I set the above configuration? (type 'yes' to accept): yes 输入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.6.190:7001)
M: 6e8c35d55d3699afe0b10317cf9c199e7df1ae50 192.168.6.190:7001 M:主节点 S:从节点
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 532355022ea3473834726e7512270025b7eddae3 192.168.6.190:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: de789ed503705e7a5a2e5fc41adb7ed3290de08c 192.168.6.190:7004
slots: (0 slots) slave
replicates 6e8c35d55d3699afe0b10317cf9c199e7df1ae50
S: c1261800c3a7e74ff3b2fda22b07ef994f7db4a7 192.168.6.190:7005
slots: (0 slots) slave
replicates 4cc06e1060369a16f70d4f8097c065bf886162a3
M: 4cc06e1060369a16f70d4f8097c065bf886162a3 192.168.6.190:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 07910c0d839e4fcd6139598edd4953fcd32adb29 192.168.6.190:7006
slots: (0 slots) slave
replicates 532355022ea3473834726e7512270025b7eddae3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

9、测试集群

使用6各redis实例中任意一个客户端链接集群:

[root@localhost redis-cluster]# redis02/bin/redis-cli -h 192.168.6.190 -p 7002 -c

[root@localhost redis-cluster]# redis02/bin/redis-cli -h 192.168.6.190 -p 7002 -c
192.168.6.190:7002> set age 100
-> Redirected to slot [741] located at 192.168.6.190:7001
OK

在这里我们登陆了集群的7002redis服务器,age的hash值是741于是就把该数据存储在了7001节点上,应为该节点的槽范围是 (slots:0-5460)。

10、关闭集群

我们创建一个脚本文件redisShutdown.sh

redis01/bin/redis-cli -p 7001 shutdown
redis02/bin/redis-cli -p 7002 shutdown
redis03/bin/redis-cli -p 7003 shutdown
redis04/bin/redis-cli -p 7004 shutdown
redis05/bin/redis-cli -p 7005 shutdown
redis06/bin/redis-cli -p 7006 shutdown

到此集群搭建完毕。 ps:单机版redis默认有16个数据库,集群中只有一个数据库即0号库

 

Redis 学习之集群的更多相关文章

  1. redis学习五 集群配置

    redis集群配置 0,整体概述      整体来说就是:      1,安装redis      2,配置多个redis实例      3,安装 ruby和rubygems      4,启动red ...

  2. redis学习之集群报错Node is not empty

    遇到的问题及解决办法 在redis.conf里bind 真机ip后,接着重新执行每个redis.conf,最后再创建集群,但报错,如下图所示: 图中报的错即: [ERR] Node 192.168.1 ...

  3. redis学习六 集群的原理(转载)

    转载自 http://shift-alt-ctrl.iteye.com/blog/2285470 一.Redis Cluster主要特性和设计     集群目标 1)高性能和线性扩展,最大可以支撑到1 ...

  4. 【Redis学习专题】- Redis主从+哨兵集群部署

    集群版本: redis-4.0.14 集群节点: 节点角色 IP redis-master 10.100.8.21 redis-slave1 10.100.8.22 redis-slave2 10.1 ...

  5. Redis 高可用集群

    Redis 高可用集群 Redis 的集群主从模型是一种高可用的集群架构.本章主要内容有:高可用集群的搭建,Jedis连接集群,新增集群节点,删除集群节点,其他配置补充说明. 高可用集群搭建 集群(c ...

  6. Redis进阶实践之十 Redis主从复制的集群模式

    一.引言        Redis的基本数据类型,高级特性,与Lua脚本的整合等相关知识点都学完了,说是学完了,只是完成了当前的学习计划,在以后的时间还需继续深入研究和学习.从今天开始来讲一下有关Re ...

  7. Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】

    No cross,no crown . 不经历风雨,怎么见彩虹. Redis哨兵模式,用现在流行的话可以说就是一个"哨兵机器人",给"哨兵机器人"进行相应的配置 ...

  8. redis入门与集群部署

    redis入门 redis入门级教程非常多,如http://www.runoob.com/redis/redis-backup.html,作为入门其实已经十分详细了,主要学习内容有如下几个方面吧 1. ...

  9. Redis存储Tomcat集群的Session

    Redis存储Tomcat集群的Session 如何 做到把新开发的代码推送到到生产系统中部署,生产系统要能够零宕机.对使用用户零影响. 设想 是使用集群来搞定,通过通知负载均衡Nginx,取下集群中 ...

随机推荐

  1. 详解CSS中的几种长度px、em、pt

    说说css的几种距离吧,大致有px.em.pt.pc.in.mm.cm.ex八种,其中最常见到的是px,我还见到过的有ex和mm.cm,当然后两个在当年见的更多. 其实px,我们最熟悉,而在电脑上也应 ...

  2. Spark入门(Python版)

    Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到”大数据(Big Data)”广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰富生态系统,允许使用 ...

  3. IAR调试cc2541串口遇到的Warning : Possible IDATA stack overflow detected

    1. 遇到的错误如下,似乎是栈空间不够使用 2. 修改界面如下,增加IDATA的大小,不过最大似乎是0XFF.

  4. python操作字符串内容并重新输出

    今天在做一个函数的作业,题目如下: 编写一个函数实现大写转小写,小写变大写,并且转换为镜像字符串,并且将字符串变为镜像字符串. 例如:’A’变为’Z’,’b’变为’y 示范字符串: ”sdSdsfdA ...

  5. docker官网安装

    最近发现一些同学居然不会安装docker,难,不难,只是“网络不好”! 如果是学习的话,目前我发现的比较好的方法还是到清华的开源镜像网站: https://mirror.tuna.tsinghua.e ...

  6. C#冒泡排序法及优化

    冒泡排序法及优化: static void Main(string[] args) { , , , , , }; ; //冒泡排序法 ; i < sums.Length - ; i++) //总 ...

  7. Bootstrap栅格系统基本使用

    1.什么是栅格系统: 在Bootstrap中,它提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列.栅格系统用于通过一系列的行(row ...

  8. * 197. Permutation Index【LintCode by java】

    Description Given a permutation which contains no repeated number, find its index in all the permuta ...

  9. 142. O(1) Check Power of 2【LintCode by java】

    Description Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return t ...

  10. 如何区别cookie和token?---测试cookie和token接口时先看。

    cookie 是什么? cookie--------------在浏览器中的长相?火狐浏览器 ----------------------------------------------------- ...