51的内容长度限制,这里继续 redis cluster 的简单使用和维护。
7,下面简单测试一下 cluster
7.1 测试数据分布
使用 redis-cli 来测试 cluster shard,可以看到 foo->bar 被存储到了 7002 节点上,并且自动跳转到了 7002 上,然后 7002 上 hello-world 是存储在 7000 上,再次重定向到 7000 节点上了。
# -c 选项为进入 cluster mode $ redis-cli -c -p 7000 127.0.0.1:7000> set foo bar -> Redirected to slot [12182] located at 127.0.0.1:7002 OK 127.0.0.1:7002> set hello world -> Redirected to slot [866] located at 127.0.0.1:7000 OK 127.0.0.1:7000> get foo -> Redirected to slot [12182] located at 127.0.0.1:7002 "bar" 127.0.0.1:7002> get hello -> Redirected to slot [866] located at 127.0.0.1:7000 "world" 127.0.0.1:7000>
7.2 reshard
重新分布,这里测试reshard 1000 个 slots 到 7000 节点的情形:
$ ./redis-trib.rb reshard 127.0.0.1:7000 ... ... # 需要迁移的 slots 数目 How many slots do you want to move (from 1 to 16384)? 1000 # 接收这 1000 个 slots 的节点,这里必须填写为 NodeID What is the receiving node ID? 123ed65d59ff22370f2f09546f410d31207789f6 # 这里需要设置从哪些 源节点 中选择 slots,这里 all 表示从所有节点中选择 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 # 下面列出了 Source nodes 以及 Destination node(即 7000 节点) Ready to move 1000 slots. Source nodes: M: 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001 slots:5962-10922 (4961 slots) master 1 additional replica(s) M: f5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002 slots:11422-16383 (4962 slots) master 1 additional replica(s) Destination node: M: 123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 slots:0-5961,10923-11421 (6461 slots) master 1 additional replica(s) # 下面为 redis-trib 列出的 reshard 的 slot 规划: Resharding plan: Moving slot 11422 from f5bdda1518cd3826100a30f5953ed82a5861ed48 Moving slot 11423 from f5bdda1518cd3826100a30f5953ed82a5861ed48 Moving slot 11424 from f5bdda1518cd3826100a30f5953ed82a5861ed48 # 如果接收 redis-trib 的 slot reshard plan,则录入 yes Do you want to proceed with the proposed reshard plan (yes/no) yes # 开始执行 resharding Moving slot 11422 from 127.0.0.1:7002 to 127.0.0.1:7000: Moving slot 11423 from 127.0.0.1:7002 to 127.0.0.1:7000: Moving slot 11424 from 127.0.0.1:7002 to 127.0.0.1:7000: # 下面我们校验一下,确认 resharding OK $ ./redis-trib.rb check 127.0.0.1:7000 >>> Performing Cluster Check (using node 127.0.0.1:7000) M: 123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 slots:0-6460,10923-11922 (7461 slots) master 1 additional replica(s)
7.3 failover 测试(这里没有做一致性检查,只是看一下是否 master-slave 做了切换)
首先通过 debug segfault 命令停止了 7002 节点(从上面的运行日志中,可以看到该节点为 master 节点)
$ redis-cli -p 7002 debug segfault Error: Server closed the connection # 下面为 7002 节点的日志 === REDIS BUG REPORT END. Make sure to include from START to END. === Please report the crash opening an issue on github: http://github.com/antirez/redis/issues Suspect RAM error? Use redis-server --test-memory to verify it. Segmentation fault $
然后从 cluster nodes 命令输出可以看到 7002 节点 fail,同时它的 slave 7005 节点升级为了 master 节点
127.0.0.1:7000> cluster nodes 35e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003 slave 123ed65d59ff22370f2f09546f410d31207789f6 0 1410838367042 7 connected 61dfb1055760d5dcf6519e35435d60dc5b207940 127.0.0.1:7004 slave 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 0 1410838368047 5 connected 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001 master - 0 1410838368547 2 connected 6461-10922 123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 myself,master - 0 0 7 connected 0-6460 10923-11922 bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005 master - 0 1410838366541 8 connected 11923-16383 f5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002 master,fail - 1410838288398 1410838286795 3 disconnected 127.0.0.1:7000>
把 7002 节点再次启动后,7002 节点自动成为了 7005 节点的 slave 节点
127.0.0.1:7000> cluster nodes 35e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003 slave 123ed65d59ff22370f2f09546f410d31207789f6 0 1410838468336 7 connected 61dfb1055760d5dcf6519e35435d60dc5b207940 127.0.0.1:7004 slave 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 0 1410838468336 5 connected 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001 master - 0 1410838468837 2 connected 6461-10922 123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 myself,master - 0 0 7 connected 0-6460 10923-11922 bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005 master - 0 1410838467334 8 connected 11923-16383 f5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002 slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410838469337 8 connected 127.0.0.1:7000>
7.4 新增节点
7.4.1 新增 master 节点
默认是新增一个 master 节点,同样的,这里测试是先新增 7006/ 目录,再编辑 redis.conf 配置文件
在 7006 目录下启动 7006 节点 redis-server redis.conf
下面通过 redis-trib 工具在当前的 cluster 中新增 7006 节点
$ ./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000 >>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000 ... >>> Performing Cluster Check (using node 127.0.0.1:7000) ... Connecting to node 127.0.0.1:7006: OK // 这里发送 CLUSTER MEET 命令给 7006 节点,要求其加入 cluster >>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster. [OK] New node added correctly.
此时,7006 节点已经加入 cluster,可以接收客户端的指令,并执行 redirection
7.4.2 新增 slave 节点
有两种方式来添加 slave 节点:
1) 通过 --slave 选项添加 slave 节点,这里以新增的 7007 节点作为 7005(当前已经为master节点)的 slave 节点为例
$ ./redis-trib.rb add-node --slave --master-id bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7007 127.0.0.1:7000 >>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7000 Connecting to node 127.0.0.1:7000: OK ... ... >>> Performing Cluster Check (using node 127.0.0.1:7000) S: 35e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003 slots: (0 slots) slave replicates 123ed65d59ff22370f2f09546f410d31207789f6 M: bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005 slots:11923-16383 (4461 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. // 将 7007 加入 cluster,并配置为 7005 的 slave 节点 Connecting to node 127.0.0.1:7007: OK >>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster. Waiting for the cluster to join. >>> Configure node as replica of 127.0.0.1:7005. [OK] New node added correctly. [root@sasd redis-cluster]# 127.0.0.1:7000> cluster nodes 8868592d98d84b7cf5752cc0b97af4ac807d1a12 127.0.0.1:7007 slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410844154906 8 connected 6d1ebedad33bb31ffbaa99bad095eef4a5920857 127.0.0.1:7006 master - 0 1410844155909 0 connected 35e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003 slave 123ed65d59ff22370f2f09546f410d31207789f6 0 1410844154405 7 connected 61dfb1055760d5dcf6519e35435d60dc5b207940 127.0.0.1:7004 slave 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 0 1410844153905 5 connected 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001 master - 0 1410844153905 2 connected 6461-10922 123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 myself,master - 0 0 7 connected 0-6460 10923-11922 bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005 master - 0 1410844155408 8 connected 11923-16383 f5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002 slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410844154406 8 connected 127.0.0.1:7000>
2) 通过 cluster replicate 命令来添加 slave 节点,如下:
# 将 7007 节点添加为 NodeID=bfc910f924d772fe03d9fe6a19aabd73d5730d26 的 slave 节点 127.0.0.1:7007> cluster replicate bfc910f924d772fe03d9fe6a19aabd73d5730d26
---------
7.4 删除节点
7.4.1 删除 slave 节点
对于删除 slave 节点,可以直接使用 redis-trib 的 del-node 命令来删除,如:
./redis-trib del-node 127.0.0.1:7000 <node-id>
node-id 为待删除的slave节点的 NodeID。
7.4.2 删除 master 节点
也可以使用 del-node 命令来删除 master 节点,但是必须为空,因此需要在删除 master 节点之前先通过 reshard 将数据迁移至其他的 master 节点上。
当然也可以使用上面的 CLUSTER FAILOVER 命令来删除 master 节点,并选择一个 slave 作为新的 master 节点。
本文出自 “安静的疯子” 博客,请务必保留此出处http://quietmadman.blog.51cto.com/3269500/1553238
原文地址:http://quietmadman.blog.51cto.com/3269500/1553238