Redis集群搭建

1. redis 集群
1.1.  集群原理
1.1.1. redis-cluster 架构图
  架构细节 :
(1) 所有的 redis 节点彼此互联 (PING-PONG 机制 ), 内部使用二进制协议优化传输速度和带宽 .
(2) 节点的 fail 是通过集群中超过半数的节点检测失效时才生效 .
(3) 客户端与 redis 节点直连 , 不需要中间 proxy . 客户端不需要连接集群所有节点 , 连接集群中任何一个可用节点即可
(4)redis-cluster 把所有的物理节点映射到 [0-16383]slot ,cluster 负责维护 node<->slot<->value
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时, redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽, redis 会根据节点数量大致均等的将哈希槽映射到不同的节点
1.1.2.  redis-cluster 投票 : 容错
 
(1) 领着投票过程是集群中所有 master 参与 , 如果半数以上 master 节点与 master 节点通信超过 (cluster-node-timeout), 认为当前 master 节点挂掉 .
(2): 什么时候整个集群不可用 (cluster_state:fail)? 
    a: 如果集群任意 master 挂掉 , 且当前 master 没有 slave. 集群进入 fail 状态 , 也可以理解成集群的 slot 映射 [0-16383] 不完成时进入 fail 状态 . ps : redis-3.0.0.rc1 加入 cluster-require-full-coverage 参数 , 默认关闭 , 打开集群兼容部分失败 .
    b: 如果集群超过半数以上 master 挂掉,无论是否有 slave 集群进入 fail 状态 .
  ps: 当集群不可用时 , 所有对集群的操作做都不可用,收到 ((error) CLUSTERDOWN The cluster is down) 错误
 
1.2. ruby 环境
redis 集群管理工具 redis-trib.rb 依赖 ruby 环境,首先需要安装 ruby 环境:
 
安装 ruby
yum install ruby
yum install rubygems
 
安装 ruby redis 的接口程序
拷贝redis-3.0.0.gem /usr/local
执行:
gem install /usr/local/redis-3.0.0.gem
 
1.3.  创建集群:
1.3.1.  集群结点规划
这里在同一台服务器用不同的端口表示不同的 redis 服务器,如下:
主节点: 192.168.101.3: 7001 192.168.101.3:7002 192.168.101.3: 7003
从节点:192.168.101.3: 700 4 192.168.101.3: 700 5 192.168.101.3: 700 6
 
/usr/local 下创建 redis-cluster 目录,其下创建 7001 7002 。。 7006 目录,如下:
 
redis 安装目录 bin 下的文件拷贝到每个 700X 目录内,同时将 redis 源码目录 src 下的 redis-trib.rb 拷贝到 redis-cluster 目录下。
 
修改每个 700X 目录下的 redis.conf 配置文件:
 
port XXXX
#bind 192.168.101.3
cluster-enabled yes
 
1.3.2.  启动每个结点 redis 服务
 
分别进入 7001 7002 ...7006 目录,执行:
./redis-server ./redis.conf
 
查看 redis 进程:

1.3.3.  执行创建集群命令
执行 r edis-trib.rb ,此脚本是 ruby 脚本,它依赖 ruby 环境。
 
./redis-trib.rb create --replicas 1 192.168.101.3:7001 192.168.101.3:7002 192.168.101.3:7003 192.168.101.3:7004 192.168.101.3:7005  192.168.101.3:7006
./redis-trib.rb create --replicas 1 192.168.131.102:7001 192.168.131.102:7002 192.168.131.102:7003 192.168.131.102:7004 192.168.131.102:7005 192.168.131.102:7006
 
说明:
redis 集群至少需要 3 个主节点,每个主节点有一个从节点总共 6 个节点
replicas 指定为 1 表示每个主节点有一个从节点
 
注意:
如果执行时报如下错误:
[ERR] Node XXXXXX  is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
解决方法是删除生成的配置文件 nodes.conf ,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除 redis 的持久化文件后再重启 redis ,比如:appendonly.aof dump.rdb
 
 
创建集群输出如下:
>>> Creating cluster
Connecting to node 192.168.101.3:7001: OK
Connecting to node 192.168.101.3:7002: OK
Connecting to node 192.168.101.3:7003: OK
Connecting to node 192.168.101.3:7004: OK
Connecting to node 192.168.101.3:7005: OK
Connecting to node 192.168.101.3:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.101.3:7001
192.168.101.3:7002
192.168.101.3:7003
Adding replica 192.168.101.3:7004 to 192.168.101.3:7001
Adding replica 192.168.101.3:7005 to 192.168.101.3:7002
Adding replica 192.168.101.3:7006 to 192.168.101.3:7003
M: cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 192.168.101.3:7001
   slots:0-5460 (5461 slots) master
M: 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 192.168.101.3:7002
   slots:5461-10922 (5462 slots) master
M: 1a8420896c3ff60b70c716e8480de8e50749ee65 192.168.101.3:7003
   slots:10923-16383 (5461 slots) master
S: 69d94b4963fd94f315fba2b9f12fae1278184fe8 192.168.101.3:7004
   replicates cad9f7413ec6842c971dbcc2c48b4ca959eb5db4
S: d2421a820cc23e17a01b597866fd0f750b698ac5 192.168.101.3:7005
   replicates 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841
S: 444e7bedbdfa40714ee55cd3086b8f0d5511fe54 192.168.101.3:7006
   replicates 1a8420896c3ff60b70c716e8480de8e50749ee65
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.101.3:7001)
M: cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 192.168.101.3:7001
   slots:0-5460 (5461 slots) master
M: 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 192.168.101.3:7002
   slots:5461-10922 (5462 slots) master
M: 1a8420896c3ff60b70c716e8480de8e50749ee65 192.168.101.3:7003
   slots:10923-16383 (5461 slots) master
M: 69d94b4963fd94f315fba2b9f12fae1278184fe8 192.168.101.3:7004
   slots: (0 slots) master
   replicates cad9f7413ec6842c971dbcc2c48b4ca959eb5db4
M: d2421a820cc23e17a01b597866fd0f750b698ac5 192.168.101.3:7005
   slots: (0 slots) master
   replicates 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841
M: 444e7bedbdfa40714ee55cd3086b8f0d5511fe54 192.168.101.3:7006
   slots: (0 slots) master
   replicates 1a8420896c3ff60b70c716e8480de8e50749ee65
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
 
 
1.4.  查询集群信息
集群创建成功登陆任意 redis 结点查询集群中的节点情况。
 
客户端以集群方式登陆:

说明:
./redis-cli -c -h 192.168.101.3 -p 7001 ,其中 -c 表示以集群方式连接 redis -h 指定 ip 地址, -p 指定端口号
cluster nodes 查询集群结点信息
 
cluster info 查询集群状态信息

1.5. 
添加主节点
集群创建成功后可以向集群中添加节点,下面是添加一个 master 主节点
添加 7007 结点,参考集群结点规划章节添加一个“ 7007 ”目录作为新节点。
 
执行下边命令:
./redis-trib.rb add-node  192.168.101.3:7007 192.168.101.3:7001

查看集群结点发现 7007 已添加到集群中:

1.5.1. hash
槽重新分配
添加完主节点需要对主节点进行 hash 槽分配这样该主节才可以存储数据。
redis 集群有16384 个槽,集群中的每个结点分配自已槽,通过查看集群结点可以看到槽占用情况。

给刚添加的 7007 结点分配槽:
 
第一步:连接上集群
./redis-trib.rb reshard 192.168.101.3:700 1 (连接集群中任意一个可用结点都行)
第二步:输入要分配的槽数量
 
输入 500 表示要分配 500 个槽
第三步:输入接收槽的结点 id

这里准备给 7007 分配槽,通过 cluster nodes 查看 7007 结点 id 15b809eadae88955e36bcdbb8144f61bbbaf38fb
输入:15b809eadae88955e36bcdbb8144f61bbbaf38fb
 
第四步:输入源结点 id

这里输入 all
 
第五步:输入 yes 开始移动槽到目标结点 id
  1.6.  添加从节点
 
集群创建成功后可以向集群中添加节点,下面是添加一个 slave 从节点。
添加 7008 从结点,将 7008 作为 7007 的从结点。
  ./redis-trib.rb add-node --slave  --master-id 主节点 id   添加节点的 ip 和端口   集群中已存在节点 ip 和端口
执行如下命令:
./redis-trib.rb add-node --slave --master-id cad9f7413ec6842c971dbcc2c48b4ca959eb5db4  192.168.101.3:700 8 192.168.101.3:700 1
cad9f7413ec6842c971dbcc2c48b4ca959eb5db4   7007 结点的 id ,可通过 cluster nodes 查看。
 

注意:如果原来该结点在集群中的配置信息已经生成cluster-config-file 指定的配置文件中(如果cluster-config-file 没有指定则默认为 nodes.conf ),这时可能会报错:
[ERR] Node XXXXXX  is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
解决方法是删除生成的配置文件 nodes.conf ,删除后再执行 ./redis-trib.rb add-node 指令
 
查看集群中的结点,刚添加的 7008 7007 的从节点:
 

1.7.  删除结点:
 
./redis-trib.rb del-node 127.0.0.1:7005 4b45eb75c8b428fbd77ab979b85080146a9bc017
 
删除已经占有 hash 槽的结点会失败,报错如下:
[ERR] Node 127.0.0.1:7005 is not empty! Reshard data away and try again.
 
需要将该结点占用的 hash 槽分配出去(参考 hash 槽重新分配章节)。
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值