redis cluster 相关研究文档

架构细节:
(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->key


一:创建集群
/usr/local/redis/8000/sbin/redis-trib.rb create --replicas 2 \
192.168.12.10:8000 192.168.12.10:8001 192.168.12.10:8002 \
192.168.12.39:8000 192.168.12.39:8001 192.168.12.39:8002 \
192.168.12.9:8000 192.168.12.9:8001 192.168.12.9:8002

#redis-trib.rb的create子命令构建
#--replicas 则指定了为Redis Cluster中的每个Master节点配备几个Slave节点

二:查看集群节点信息和slots分配情况
127.0.0.1:8000> cluster nodes
127.0.0.1:8000> cluster slots

三:查看集群节点状态
#redis-trib.rb的check子命令构建
#ip:port可以是集群的任意节点
/usr/local/redis/8000/sbin/redis-trib.rb check 192.168.12.9:8000

四:增加Master节点
1:新节点执行如下命令(默认添加为master节点):
cluster meet 192.168.12.39(已存在的任意集群节点) 8000(集群节点端口)

新节点没有包含任何数据 因为它没有包含任何slot 新加入的加点是一个主节点
当集群需要将某个从节点升级为新的主节点时 这个新节点不会被选中。

2:为新节点分配slot
/usr/local/redis/8000/sbin/redis-trib.rb reshard 192.168.12.9:8000
[OK] All 16384 slots covered.
#根据提示选择要迁移的slot数量(这里选择2000)
How many slots do you want to move (from 1 to 16384)? 2000

#选择slots接收的node id
What is the receiving node ID? d0636c506a0d13a6880e6dcac41dc3b9ed9bcef4(12.48:8001)

#选择slot来源:
#all表示从所有的master重新分配,
#或者数据要提取slot的master节点id,最后用done结束
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:all(输入all)

#输入yes开始迁移slots和数据
Do you want to proceed with the proposed reshard plan (yes/no)? yes


五:添加节点为slave节点
1:将节点加入集群
cluster meet 192.168.12.39(已存在的任意集群节点) 8000(集群节点端口)

2:将节点设置为某个master的slave cluster replicate nodes
127.0.0.1:8000> cluster replicate 381342ba2e5e5a6b3df8854d6749c892a7e83f60
OK

注意:在线添加slave 时 需要bgsave整个master数据 并传递到slave 再由slave加载rdb文件到内存
rdb生成和传输的过程中消耗Master大量内存和网络IO,以此不建议单实例内存过大。

六:删除slave节点和master节点
redis-trib del-node ip:port '<node-id>'
/usr/local/redis/8000/sbin/redis-trib.rb del-node 192.168.12.48:8000 '16b67c1b0782117050e675304e380dd8714f5912'

#删除master节点192.168.12.48:8001 并将master节点槽位迁移至192.168.12.9:8000
首先要使用reshard移除master的全部slot,然后再删除当前节点
[root@office-01 redis]# /usr/local/redis/8000/sbin/redis-trib.rb reshard 192.168.12.9:8000

[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1999 #根据提示选择要迁移的slot数量
What is the receiving node ID? 381342ba2e5e5a6b3df8854d6749c892a7e83f60 #选择要接受这些slot的node-id(192.168.12.9:8000)

Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:d0636c506a0d13a6880e6dcac41dc3b9ed9bcef4 被删除master的node-id
Source node #2:done

#打印被移动的slot后,输入yes开始移动slot以及对应的数据.
Do you want to proceed with the proposed reshard plan (yes/no)? yes

删除空的master nodes
/usr/local/redis/8000/sbin/redis-trib.rb del-node 192.168.12.48:8001 'd0636c506a0d13a6880e6dcac41dc3b9ed9bcef4'

删除ok!
192.168.12.9:8001> cluster nodes
b5e9c6c3b9bb4bf72189397773c3fa46edaa5fd8 192.168.12.10:8000 master - 0 1448867284306 1 connected 666-5460
e6cf06567841960af1f5f4fb5bbb896249f8ab19 192.168.12.10:8001 slave 83089e0f4cf00059bdf9019f8f78dc0026db4efd 0 1448867284808 4 connected
83089e0f4cf00059bdf9019f8f78dc0026db4efd 192.168.12.39:8000 master - 0 1448867285308 4 connected 6128-10922
055414ec3c2c48e2773f84230f382aa960480a6d 192.168.12.9:8001 myself,slave b5e9c6c3b9bb4bf72189397773c3fa46edaa5fd8 0 0 8 connected
0cd6917cf7a6f2309ebda5f157c569f0512f11c8 192.168.12.39:8001 slave b5e9c6c3b9bb4bf72189397773c3fa46edaa5fd8 0 1448867284808 5 connected
381342ba2e5e5a6b3df8854d6749c892a7e83f60 192.168.12.9:8000 master - 0 1448867285307 11 connected 0-665 5461-6127 10923-16383
4525e6227b197a93f976c7061d325a50600b2be2 192.168.12.9:8002 slave 381342ba2e5e5a6b3df8854d6749c892a7e83f60 0 1448867283804 11 connected
764e39eadb543b7f706ba2274728ebd9e34e72e1 192.168.12.39:8002 slave 381342ba2e5e5a6b3df8854d6749c892a7e83f60 0 1448867283805 11 connected
b8344cfbce979ce820bb10741693d6a2e8b87a94 192.168.12.10:8002 slave 83089e0f4cf00059bdf9019f8f78dc0026db4efd 0 1448867283304 4 connected

 

1:错误集锦:
127.0.0.1:8000> set 2 d
(error) MOVED 5649 192.168.12.39:8000

启动的时候没有启动集群模式 需要加 -c参数:
/usr/local/redis/8000/sbin/redis-cli -c -p 8000

2:集群命令
集群
CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。

节点
CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。
CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。
CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。

槽(slot)
CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。
CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。
CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。
CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。
CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。


CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。

3:redis-cluster客户端的一些坑.
1)cluster环境下slave默认不接受任何读写操作,在slave执行readonly命令后,可执行读操作
2)client端不支持多key操作(mget,mset等),但当keys集合对应的slot相同时支持mget操作见:hash_tag
3)不支持多数据库,只有一个db,select 0。

posted @ 2016-02-25 15:55  study-notes  阅读(172)  评论(0编辑  收藏  举报