设为首页 收藏本站
查看: 1057|回复: 0

[经验分享] Redis Cluster 的安装和配置(2)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-9-17 10:58:06 | 显示全部楼层 |阅读模式
51的内容长度限制,这里继续 redis cluster 的简单使用和维护。

7,下面简单测试一下 cluster
   
7.1 测试数据分布

使用 redis-cli 来测试 cluster shard,可以看到 foo->bar 被存储到了 7002 节点上,并且自动跳转到了 7002 上,然后 7002 上 hello-world 是存储在 7000 上,再次重定向到 7000 节点上了。
  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -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 节点的情形:
  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ ./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 节点)

1
2
3
4
5
6
7
8
9
10
  $ 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 节点

1
2
3
4
5
6
7
8
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 节点

1
2
3
4
5
6
7
8
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 节点
  

1
2
3
4
5
6
7
8
9
  $ ./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 节点为例
   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ ./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.
[iyunv@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 节点,如下:
  

1
2
# 将 7007 节点添加为 NodeID=bfc910f924d772fe03d9fe6a19aabd73d5730d26 的 slave 节点
127.0.0.1:7007> cluster replicate bfc910f924d772fe03d9fe6a19aabd73d5730d26



---------

7.4 删除节点

  7.4.1 删除 slave 节点
  
  对于删除 slave 节点,可以直接使用 redis-trib 的 del-node 命令来删除,如:

1
./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 节点。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-24945-1-1.html 上篇帖子: Redis Cluster 的安装和配置(1) 下篇帖子: redis限速器设计(不使用lua脚本及事物)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表