redis集群管理

来源:互联网 发布:编程用的app 编辑:程序博客网 时间:2024/05/18 13:24

本文主要讲解主节点添加,从节点添加,主节点删除,从节点删除操作。

centos 虚拟机一台192.168.1.235,上有7001,7002,7003,7004,7005,7006 ,6节点集群 (集群搭建请参见:http://blog.csdn.net/sunqingzhong44/article/details/70842936)

1.主节点添加

在/usr/etc/local/redis-cluster/ 下新建目录7007 ,复制一份redis.cnf(redis源码包中)到7007下,修改下列相关参数:

daemonize yes  //后台运行  port 7007 #端口  bind #ip地址  dir /usr/local/redis-cluster/7007/   # 指定文件存放目录  cluster-enabled yes  #启动集群模式  cluster-node-timeout 5000 #超时时间,如果5000秒没有响应,就认为挂掉了  appendonly yes     #开启aof模式  cluster-config-file neodes7007.conf    #cluster配置文件(启动自动生成  

执行/usr/local/redis/bin/redis-server  /usr/local/redis-cluster/7007/redis.cnf 启动7007

然后执行/home/redis-3.0.7/src/redis-trib.rb  add-node 192.168.1.235:7007 192.168.1.235:7001 将7007添加进集群,后面的192.168.1.235:7001可以为集群中的任意节点(redis-trib在redis源码中)

具体如下图:


7007就被加入集群中,我们可以登录集群查看


我们可以出7007没有被分配到任何的槽,我们可以使用/home/redis-3.0.7/src/redis-trib.rb reshard 192.168.1.233:7007重新分配槽


会有如下几个询问:

how many slots do you want to move (from 1 to 16384)? 是询问要分配多少槽,这里分配500

what is the receiving node ID?  分配槽的对象id 这里是7007的id,5a7c8fc70467edac1170d141afc60a6d0d9ef4c1 

please enter all the source node IDs

Type 'all' to use all the nodes as source nodes for the hash slots     从所有的节点中分配槽给要分配的对象 ,则输入all

Type 'done' once you entered all the source nodes IDs.       自己录入节点的id从哪些节点分配槽给要分配的对象   输入done结束

完成可以看到打印出信息:


主节点添加完成

2.从节点添加

创建7008节点,添加方式参照7007

启动7008节点

将7008添加入集群


此时7008节点是主节点,我们可以将7008修改为从节点

连接7008节点,执行:  /usr/local/redis/bin/redis-cli -c -h 192.168.1.233 -p 7008 

将7008设置为7007从节点,执行:cluster replicate 5a7c8fc70467edac1170d141afc60a6d0d9ef4c1 

提示ok 则成功,7008就成为7007的从节点。


3.删除从节点

执行/home/redis-3.0.7/src/redis-trib.rb del-node  删除7008节点,如图


7008就不移除集群,并且被关闭

4.删除主节点

删除主节点,如果主节点占有槽则需要,将这些槽从新分配,再做删除,

重新分配执行命令:/home/redis-3.0.7/src/redis-trib.rb reshard 192.168.1.233:7007


这里和添加主节点分配槽时基本相同,只是询问的回答不同

how many slots do you want to move (from 1 to 16384)? 这里原来分配500,回答499

what is the receiving node ID?  这里只能是主节点并且不能是7007 

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.       这里只能录入7007的id,而且只能是done不能是all

此时7007节点的槽就被分配到了其它节点

此时我们在链接7007,执行执行/home/redis-3.0.7/src/redis-trib.rb del-node


此时主节点7007被删除并关闭

0 0