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

[经验分享] Redis Cluster集群节点管理

[复制链接]

尚未签到

发表于 2018-11-3 08:04:13 | 显示全部楼层 |阅读模式
如何管理
  Redis集群的管理涉及的主要就是针对集群中的主次节点进行新增、删除以及对节点重新分片操作,而这些操作我们就可以使用redis-trib.rb工具来实现,具体如下:

一、新增Master节点
  Redis集群中新增节点需要新创建一个空节点,然后将该空节点加入到集群中,最后为这个新的空节点分配slot哈希槽值即可。具体如下:

  A、新建空节点

  

//使用脚本创建redis 7006节点:  
#cd /usr/local/redis-3.2.8
  
#./utils/install_server.sh
  
Welcome to the redis service installer
  
This script will help you easily set up a running redis server
  

  
Please select the redis port for this instance: [6379] 7006      # 端口为7006
  
Please select the redis config file name [/etc/redis/7006.conf]
  
Selected default - /etc/redis/7006.conf
  
Please select the redis log file name [/var/log/redis_7006.log]
  
Selected default - /var/log/redis_7006.log
  
Please select the data directory for this instance [/var/lib/redis/7006]
  
Selected default - /var/lib/redis/7006
  
Please select the redis executable path [/usr/local/bin/redis-server]
  
Selected config:
  
Port           : 7006
  
Config file    : /etc/redis/7006.conf
  
Log file       : /var/log/redis_7006.log
  
Data dir       : /var/lib/redis/7006
  
Executable     : /usr/local/bin/redis-server
  
Cli Executable : /usr/local/bin/redis-cli
  
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  
Copied /tmp/7006.conf => /etc/init.d/redis_7006
  
Installing service...
  
Successfully added to chkconfig!
  
Successfully added to runlevels 345!
  
Starting Redis server...
  
Installation successful!
  

//手动修改redis配置文件端口(脚本无法成功配置):  
# sed -i 's/6379/7006/g' /etc/redis/7006.conf
  
//修改redis启动脚本(如果修改过监听端口地址)
  
# sed -i 's/$CLIEXEC -p/$CLIEXEC -h 192.168.0.2 -p/g' /etc/rc.d/init.d/redis_7006
  
//关闭默认端口
  
# /usr/local/redis-3.2.8/src/redis-cli -h 192.168.0.2 -p 6379 shutdown
  
//启动这个新节点:
  
# /etc/init.d/redis_7006 start
  

  B、加入空节点到集群

  

#cd /usr/local/redis-3.2.8/src  
#./redis-trib.rb add-node 192.168.0.2:7006 192.168.0.2:7000
  
NOTE:
  
192.168.0.2:7006为集群新增的空节点;
  
192.168.0.2:7000为集群中的任意节点;
  

报错: [ERR] Node 192.168.30.198:16381 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0  

  
解决方法:
  
1)、将需要新增的节点下aof、rdb等本地备份文件删除;
  
2)、同时将新Node的集群配置文件 nodes_7006.conf 删除,即:删除你redis.conf里面cluster-config-file所在的文件;
  
3)、再次添加新节点如果还是报错,则登录新Node,./redis-cli–h x –p对数据库进行清除:
  
192.168.0.2:7006>  flushdb      #清空当前数据库
  

  执行完成之后,结果显示如下:
DSC0000.jpg

  可以使用redis-trib.rb验证新增节点的类型是否为主节点:
  

# ./redis-trib.rb check 192.168.0.2:7006  

  
//结果显示如下:
  
>>> Performing Cluster Check (using node 192.168.0.2:7006)
  
M: 7765fdc83ea8859a0d2398bfff8c633415d12777 192.168.0.2:7006
  slots: (0 slots) master
  0 additional replica(s)
  
S: f40ea2a234f7251fa5ee32f31cb987334b104263 192.168.0.2:7005
  slots: (0 slots) slave
  replicates 920d755a8dbd2b62cbdf6053d62102c7379140d8
  
S: 45be7a1f9a2a8f891fa5807ecd7bb6ae37d95eab 192.168.0.2:7003
  slots: (0 slots) slave
  replicates 01c944eb66564d41e355388dc468ee79e71fe789
  
M: 01c944eb66564d41e355388dc468ee79e71fe789 192.168.0.2:7000
  slots:0-5460 (5461 slots) master
  1 additional replica(s)
  
M: 357a9140f1b836afda4a623a757cfa54c3ab932b 192.168.0.2:7001
  slots:5461-10922 (5462 slots) master
  1 additional replica(s)
  
M: 920d755a8dbd2b62cbdf6053d62102c7379140d8 192.168.0.2:7002
  slots:10923-16383 (5461 slots) master
  1 additional replica(s)
  
S: c9335f2ea4c8d2bfa1766d59aa4631d9a8df36b3 192.168.0.2:7004
  slots: (0 slots) slave
  replicates 357a9140f1b836afda4a623a757cfa54c3ab932b
  
[OK] All nodes agree about slots configuration.
  
>>> Check for open slots...
  
>>> Check slots coverage...
  
[OK] All 16384 slots covered.
  

  C、为空节点分配slot

  

#./redis-trib.rb reshard 192.168.0.2:7006  

  
//执行结果显示,并按提示输入:
  
How many slots do you want to move (from 1 to 16384)? 500   //被删除master的slot数量

  
What is the receiving node>
  
Please enter all the source node>  Type 'all' to use all the nodes as source nodes for the hash slots.

  Type 'done' once you entered all the source nodes>  
Source node #1:01c944eb66564d41e355388dc468ee79e71fe789   //被删除slot的7000主节点
  
Source node #2:done
  
Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认重新分
  

  注意:source node 要用 ‘done’,如果用’all’则会将其他每个master节点的slot 分给接收slot的节点。最好别用‘all’, 会造成slot 分割碎片。
  执行结果显示:
DSC0001.jpg

  从上图可以知道,新的主节点已经添加并分配好slots,slots值的范围为500.

  D、验证使用该节点
DSC0002.jpg

  从上图知道,新节点已经添加并配置成功,并且使用正常无问题。

  如果在迁移过程遇到下面这样的错误:
  

>>> Check for open slots...  
[WARNING] Node 192.168.0.2:7000 has slots in importing state (0).
  
[WARNING] Node 192.168.0.2:7006 has slots in migrating state (0).
  
[WARNING] The following slots are open: 0
  

  

  可以考虑使用命令“redis-trib.rb fix 192.168.0.2:7000”尝试修复。需要显示有节点处于migrating或importing状态,可以登录到相应的节点,使用命令“cluster setslot 0 stable”修改,参数0为问题显示的slot的ID。

二、为主节点新增Slave节点
  新增Slave节点与新增Master的前三步骤相同,这里不再介绍,不同的是需要登录到从节点的redis-cli,使用replicate为该节点指定主节点,具体如下:
  方式一:
  

// 登录到从节点的redis-cli,使用replicate为该节点指定主节点  
192.168.0.2:7006> cluster replicate 01c944eb66564d41e355388dc468ee79e71fe789
  

  方式二:
  

#./redis-trib.rb add-node --slave --master-id '01c944eb66564d41e355388dc468ee79e71fe789' 192.168.0.2:7006 192.168.0.2:7001  

  
注释:
  
--slave,表示添加的是从节点

  
--master-id 01c944eb66564d41e355388dc468ee79e71fe789 ,主节点的node>  
192.168.0.2:7006,新节点
  
192.168.0.2:7001,集群任一个旧节点
  

  结果显示:
  

(error) ERR To set a master the node must be empty and without assigned slots.  
NOTE:
  
上面的错误意思是不能为一个非空并且分配了slot的主节点继续添加从节点。
  

  由于上面的问题,我们现在将7006主节点置空并且清除为其指定的slots,具体如下:

  A、先删除7006主节点的slots

  #./redis-trib.rb  reshard 192.168.0.2:7006
DSC0003.jpg

  

How many slots do you want to move (from 1 to 16384)? 500           // 删除指定的500的slots
  
What is the receiving node>  
Source node #1:7765fdc83ea8859a0d2398bfff8c633415d12777      //指定slots的来源为当前的node-id(7006)
  
Source node #2:done
  
Do you want to proceed with the proposed reshard plan (yes/no)? yes            # 开始转移
  

  B、指定主节点

  

#./redis-cli -c -h 192.168.0.2 -p 7006  
193.192.168.0.2:7006> cluster replicate 01c944eb66564d41e355388dc468ee79e71fe789
  
OK
  
193.192.168.0.2:7006> exit
  

  C、查看下主从节点关系
  ./redis-trib.rb check 192.168.0.2:7006

  执行结果:
DSC0004.jpg


三、删除节点

  1,删除从节点
  

#cd /usr/local/redis-3.2.8/src  
#./redis-trib.rb del-node 192.168.0.2:7006 '7765fdc83ea8859a0d2398bfff8c633415d12777'
  

  执行结果显示:
DSC0005.jpg

  2,删除主节点
  A、如果主节点有从节点,将从节点转移到其他主节点在删除
  B、转移主节点slots
  这里的操作与上面的操作相同,这里不赘述。
  C、删除主节点
  $redis-trib.rb  del-node  192.168.0.2:7000
  8d8589fd7e9d140442e06b06e9e810d5d0f5e257
  注:
  192.168.0.2:7000 代表cluster的一个node
  920d755a8dbd2b62cbdf6053d62102c7379140d8 为要删除的7002这个节点的id.




运维网声明 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-630031-1-1.html 上篇帖子: Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis+KafK-blingbling123的博客 下篇帖子: 为redis安装配置twemproxy
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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