1 集群基本操作

 

1.1 查看当前集群状态

使用redis-trib.rb check功能查看对应的节点的状态:

[root@bogon bin]# ./redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7001: OK
Performing Cluster Check (using node 127.0.0.1:7000)
M: 36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000
slots:0-5460 (5461 slots) master
0 additional replica(s)
M: e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.

  

如上图所示,当前我们有三个节点7000到7002,默认总共16384个slot,平均一个节点5461个slot

或者我们也可以使用redis-cli连接上任意一个结点,执行cluster nodes查看结点状态:

[root@bogon bin]# ./redis-cli -c -p 7000 cluster nodes
e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440144105378 3 connected 10923-16383
36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 master - 0 1440144106398 2 connected 5461-10922

1.2 增加一个主节点

1 启动7003的redis实例:

[root@bogon bin]# ps aux | grep redis
root 4212 0.1 0.1 140836 7252 ? Ssl Aug20 0:41 ./redis-server *:7000 [cluster]
root 4214 0.1 0.1 140836 7296 ? Ssl Aug20 0:41 ./redis-server *:7001 [cluster]
root 4218 0.1 0.1 140836 7228 ? Ssl Aug20 0:41 ./redis-server *:7002 [cluster]
root 10817 0.0 0.1 20160 5180 pts/0 S+ 00:41 0:00 ./redis-cli -c -p 7000
root 11223 0.0 0.1 140836 7460 ? Ssl 00:54 0:00 ./redis-server *:7003 [cluster]

  

2 使用 redis-cli cluster nodes查看当前集群状态,跟1.1一样,7003实例还没加进来

[root@bogon bin]# ./redis-cli -c -p 7000 cluster nodes
e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440144105378 3 connected 10923-16383
36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 master - 0 1440144106398 2 connected 5461-10922

  

3 使用redis-trib.rb add-node增加结点

[root@bogon bin]# ./redis-trib.rb add-node 127.0.0.1:7003 127.0.0.1:7000
Adding node 127.0.0.1:7003 to cluster 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7001: OK
Performing Cluster Check (using node 127.0.0.1:7000)
M: 36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000
slots:0-5460 (5461 slots) master
0 additional replica(s)
M: e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
Connecting to node 127.0.0.1:7003: OK
Send CLUSTER MEET to node 127.0.0.1:7003 to make it join the cluster.
[OK] New node added correctly.

  

4 使用redis-cli查看当前cluster的状态

[root@bogon bin]# ./redis-cli -c -p 7000 cluster nodes
245d95199dcc1e01880b2876fcab8a75b1867b19 127.0.0.1:7003 master - 0 1440144104360 0 connected
e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440144105378 3 connected 10923-16383
36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 master - 0 1440144106398 2 connected 5461-10922

  

发现新节点已经连上cluster了,成为集群一份子,并且默认是master结点。这时候,我们已经可以使用redis-cli连接到该结点查询情况

[root@bogon bin]# ./redis-cli -c -p 7003
127.0.0.1:7003> set a b
-> Redirected to slot [15495] located at 127.0.0.1:7002
OK
127.0.0.1:7002>

  

不过redis cluster并不会自动把hash分片迁移到该结点

5 使用redis-trib.rb rehashed重新设置分片

- ./redis-trib.rb reshard 127.0.0.1:7003执行rehash
- 需要再命令行中输入slots个数(迁移的slots数目)
- 需要填写迁移目标结点的id,这里我们希望迁移到7003上,所以把7003的id写上去(通过redis-cli cluster nodes查看对应id)
- 需要填写source node,可以填上对应的source节点id,或者写all,表示从所有节点中抽取1000个出来,这里写all
- 这里redis集群会列出来模拟迁移的点,确认后,填写yes,那么开始进行实例迁移

6 查看新的分片

127.0.0.1:7000> cluster nodes
245d95199dcc1e01880b2876fcab8a75b1867b19 127.0.0.1:7003 master - 0 1440145009719 4 connected 0-332 5461-5794 10923-11255
e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440145009206 3 connected 11256-16383
36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 myself,master - 0 0 1 connected 333-5460
7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 master - 0 1440145010228 2 connected 5795-10922

  

如上所示,可以看出,7003从剩余3个节点中抽取除了1000个slots出来,整体上抽取的相对比较均匀

 

1.3 增加一个从节点

1 启动7004的redis实例

[root@bogon bin]# ps aux | grep redis
root 4212 0.1 0.1 140836 7240 ? Ssl Aug20 0:46 ./redis-server *:7000 [cluster]
root 4214 0.1 0.1 140836 7296 ? Ssl Aug20 0:46 ./redis-server *:7001 [cluster]
root 4218 0.1 0.1 140836 7292 ? Ssl Aug20 0:46 ./redis-server *:7002 [cluster]
root 10817 0.0 0.1 20160 5192 pts/0 S+ 00:41 0:00 ./redis-cli -c -p 7000
root 11223 0.2 0.1 140836 7356 ? Ssl 00:54 0:05 ./redis-server *:7003 [cluster]
root 12063 0.2 0.1 140836 7464 ? Ssl 01:34 0:00 ./redis-server *:7004 [cluster]

  

2 使用redis-trib.rb add-node增加7004节点

[root@bogon bin]# ./redis-trib.rb add-node 127.0.0.1:7004 127.0.0.1:7000
Adding node 127.0.0.1:7004 to cluster 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7001: OK
Performing Cluster Check (using node 127.0.0.1:7000)
M: 36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 slots:333-5460 (5128 slots) master 0 additional replica(s)
M: e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 slots:11256-16383 (5128 slots) master 0 additional replica(s)
M: 245d95199dcc1e01880b2876fcab8a75b1867b19 127.0.0.1:7003 slots:0-332,5461-5794,10923-11255 (1000 slots) master 0 additional replica(s)
M: 7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 slots:5795-10922 (5128 slots) master 0 additional replica(s)

  

[OK] All nodes agree about slots configuration. Check for open slots... Check slots coverage... [OK] All 16384 slots covered. Connecting to node 127.0.0.1:7004: OK Send CLUSTER MEET to node 127.0.0.1:7004 to make it join the cluster. [OK] New node added correctly. 

3 查看当前集群nodes状态

[root@bogon bin]# ./redis-cli -c -p 7000 cluster nodes
36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 myself,master - 0 0 1 connected 333-5460
e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440147387481 3 connected 11256-16383
245d95199dcc1e01880b2876fcab8a75b1867b19 127.0.0.1:7003 master - 0 1440147389014 4 connected 0-332 5461-5794 10923-11255
f26de43ddafc2e8e000132c3b53473d42906b429 127.0.0.1:7004 master - 0 1440147387991 0 connected
7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 master - 0 1440147388501 2 connected 5795-10922

  

默认在增加节点的时候都是master节点,因此刚加进去的7004节点也是master

4 将7004转成7001的slave节点


# 使用redis-cli连接到7004上,查看当前node的状态
[root@bogon bin]# ./redis-cli -p 7004 -c
127.0.0.1:7004> cluster nodes
f26de43ddafc2e8e000132c3b53473d42906b429 127.0.0.1:7004 myself,master - 0 0 0 connected
245d95199dcc1e01880b2876fcab8a75b1867b19 127.0.0.1:7003 master - 0 1440147476431 4 connected 0-332 5461-5794 10923-11255
36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 master - 0 1440147476944 1 connected 333-5460
7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 master - 0 1440147477452 2 connected 5795-10922
e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440147475924 3 connected 11256-16383
# 使用cluster replicate <id>命令将7004设置为7001的从节点,其中id为7001的节点id
127.0.0.1:7004> cluster replicate 7e46925ca9781a403facf450aa0b4b8149ad9c27
OK # 再次查看cluster nodes
127.0.0.1:7004> cluster nodes
f26de43ddafc2e8e000132c3b53473d42906b429 127.0.0.1:7004 myself,slave 7e46925ca9781a403facf450aa0b4b8149ad9c27 0 0 0 connected
245d95199dcc1e01880b2876fcab8a75b1867b19 127.0.0.1:7003 master - 0 1440147500851 4 connected 0-332 5461-5794 10923-11255
36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 master - 0 1440147499834 1 connected 333-5460
7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 master - 0 1440147500849 2 connected 5795-10922
e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440147500338 3 connected 11256-16383

# 当前7004已经成功成为7001的从节点了

1.4 主从自动切换

  1. 查看当前集群状态

    [root@bogon bin]# ./redis-cli -p 7000 -c cluster nodes
    36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 myself,master - 0 0 5 connected 0-5794 10923-11255
    e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440402734058 3 connected 11256-16383
    245d95199dcc1e01880b2876fcab8a75b1867b19 127.0.0.1:7003 slave 36fce4bf6c829797c433c08b045a8c482e07fbc1 0 1440402733548 5 connected
    f26de43ddafc2e8e000132c3b53473d42906b429 127.0.0.1:7004 slave 7e46925ca9781a403facf450aa0b4b8149ad9c27 0 1440402734573 2 connected
    7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 master - 0 1440402735085 2 connected 5795-10922

    如图所示,当前7000,7001,7002三个为主,7003为7000的slave,7004位7001的slave

  2. 构造数据

    # 构造数据
    for ((i=0;i<1000;i++)); do ./redis-cli -p 7000 -c set "key$i" "$i";done # 查看数据分片情况:
    [root@bogon bin]# ./redis-cli -p 7000 -c dbsize
    (integer) 369
    [root@bogon bin]# ./redis-cli -p 7001 -c dbsize
    (integer) 316
    [root@bogon bin]# ./redis-cli -p 7002 -c dbsize
    (integer) 316 # 发现基本上比较均匀1000个key平均分到三个分片上了

      

  3. kill主节点

    # 我们把7000的redis-server kill掉,看看是否能自动迁移到7003
    [root@bogon bin]# ps aux | grep redis | grep 7000
    root 11223 0.2 0.2 140836 9408 ? Ssl Aug23 0:47 ./redis-server *:7000 [cluster]
    [root@bogon bin]# kill -9 11223
    [root@bogon bin]# ps aux | grep redis
    root 4212 0.2 0.2 140836 9196 ? Ssl Aug23 1:27 ./redis-server *:7003 [cluster]
    root 4214 0.2 0.1 140836 7412 ? Ssl Aug23 1:27 ./redis-server *:7001 [cluster]
    root 4218 0.2 0.1 140836 7352 ? Ssl Aug23 1:26 ./redis-server *:7002 [cluster]
    root 12649 0.2 0.2 140836 9552 ? Ssl Aug23 0:39 ./redis-server *:7004 [cluster]

      

  4. 查看当前节点状态

    [root@bogon bin]# ./redis-cli -p 7001 -c cluster nodes
    f26de43ddafc2e8e000132c3b53473d42906b429 127.0.0.1:7004 slave 7e46925ca9781a403facf450aa0b4b8149ad9c27 0 1440403755252 2 connected
    7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001 myself,master - 0 0 2 connected 5795-10922
    36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000 master,fail - 1440403749785 1440403749179 5 disconnected
    245d95199dcc1e01880b2876fcab8a75b1867b19 127.0.0.1:7003 master - 0 1440403756767 6 connected 0-5794 10923-11255
    e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002 master - 0 1440403756260 3 connected 11256-16383 # 发现7003已经自动成为主了

      

redis3.0 集群实战2 - 集群功能实战的更多相关文章

  1. Redis3.0.1 Stable版本的集群部署(Mac)

    本文档基于如下原始文档(CentOS)创建: http://blog.csdn.net/xu470438000/article/details/42971091 修改了一些路径的错误,补全了一些命令执 ...

  2. redis3.0 集群实战1 -- 安装和配置

    本文主要是在centos7上安装和配置redis集群实战 参考: http://hot66hot.iteye.com/blog/2050676 集群教程: http://redisdoc.com/to ...

  3. Dubbo入门到精通学习笔记(十八):使用Redis3.0集群实现Tomcat集群的Session共享

    文章目录 1.单节点访问http://192.168.1.61:8082/pay-web-boss/: 2.增加多一个消费者节点:192.168.1.62,以同样的方式部署pay-web-boss工程 ...

  4. Redis3.0.7 cluster/集群 安装配置教程

    1.前言 环境:CentOS-6.7-i386-LiveDVD 安装的CentOs系统 节点: 6个节点,3个主节点.3个从节点(由于redis默认需要3个主节点,如果想每个主节点有一个从节点,这是最 ...

  5. CentOS完美搭建Redis3.0集群并附测试

    线上的统一聊天和推送项目使用的是redis主从,redis版本2.8.6 redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口:slaveof ...

  6. redis3.0.0 集群安装详细步骤

    Redis集群部署文档(centos6系统) Redis集群部署文档(centos6系统) (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对 ...

  7. redis3.0.5集群部署安装详细步骤

    Redis集群部署文档(centos6系统) (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下) ...

  8. redis-3.0.0集群的安装及使用

    redis集群需要至少6个节点(偶数节点),3个主节点,3个从节点.注意:集群模式最好不要keys *查询数据. 1 下载redis,官网下载3.0.0版本,之前2.几的版本不支持集群模式.下载地址: ...

  9. redis3.0集群搭建

    生产环境中准备使用redis3.0集群了,花了一天时间研究了一下,下面记录一下集群搭建的过程. 服务器规划: 192.168.116.129    7000,7003 192.168.116.130 ...

  10. redis3.0.7集群部署手册

    1.用root登录主机2.将redis-3.0.7.tar.gz传送到主机3.将rubygems-update-2.5.2.gem,redis-3.0.0.gem传送到主机4.解压redis-3.0. ...

随机推荐

  1. DVI-A、DVI-D、DVI-I接口定义、DVI接口图和DVI接口标准介绍

    http://blog.csdn.net/cd520yy/article/details/16993179

  2. ubuntu下解决wireshark权限问题

    wireshark要监控eth0,但是必须要root权限才行.但是,直接用root运行程序是相当危险,也是非常不方便的. 解决方法如下: 1.添加wireshark用户组 sudo groupadd ...

  3. MyEclipse自动生成hibernate实体类和配置文件攻略

    步骤1:找到导航栏里面的window--showView然后输入db brower,打开数据库浏览窗口步骤2:在数据库浏览窗口里只有一个Myeclipse自带的数据库,该数据没有用,我们在空白的地方右 ...

  4. 深入理解图优化与g2o:图优化篇

    前言 本节我们将深入介绍视觉slam中的主流优化方法——图优化(graph-based optimization).下一节中,介绍一下非常流行的图优化库:g2o. 关于g2o,我13年写过一个文档,然 ...

  5. SNF开发平台WinForm之十一-程序打包-SNF快速开发平台3.3-Spring.Net.Framework

    原来我们用的是微软自带的打包工具去打包,但感觉好像也是第三方做的打包并且很是麻烦,还有时不成功报错.那综合考虑就找一个简单实用的打包工具吧,就找到了NSIS这个.具体打包步骤如下: 1.安装NSIS ...

  6. 转:Transform Web.Config when Deploying a Web Application Project

    Introduction One of the really cool features that are integrated with Visual Studio 2010 is Web.Conf ...

  7. Javascript this 关键字

    Javascript 的 this 关键字总是指向当前被执行函数的所有者. 换句话说,如果当前函数可以视为某个对象的一个方法,那么 this 就指向该对象. 例如有这么一个函数 doSomething ...

  8. xdebug调试一直等待连接

    调试php时一般会启动浏览器,地址栏里一般是 index.php?XDEBUG_SESSION_START=xxx xxx表示调试的ide_key. 开了代理没有关,结果调试时一直无法连上,折腾了好久 ...

  9. String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)

    本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...

  10. Communication - 02.Call U

    App层 从大拇哥Click CallButton开始手机便已明白,主人这是要打电话.当然,你可以选择直接拨号,也可以通过ContactList,或者从通话记录着手.这些都只是UI的设计不同而已,终归 ...