1:redis cluster的现状
目前redis支持的cluster特性(已亲测): 1):节点自动发现
2):slave->master 选举,集群容错 3):Hot resharding:在线分片 4):进群管理:cluster xxx
5):基于配置(nodes-port.conf)的集群管理 6):ASK 转向/MOVED 转向机制.
2:redis cluster 架构 1)redis-cluster架构图node
架构细节:
(1)全部的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
(2)节点的fail是经过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不须要中间proxy层.客户端不须要链接集群全部节点,链接集群中任何一个可用节点便可
(4)redis-cluster把全部的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value 2) redis-cluster选举:容错linux
(1)领着选举过程是集群中全部master参与,若是半数以上master节点与master节点通讯超过(cluster-node-timeout),认为当前master节点挂掉.
(2):何时整个集群不可用(cluster_state:fail),当集群不可用时,全部对集群的操做作都不可用,收到((error) CLUSTERDOWN The cluster is down)错误 a:若是集群任意master挂掉,且当前master没有slave.集群进入fail状态,也能够理解成进群的slot映射[0-16383]不完成时进入fail状态.
b:若是进群超过半数以上master挂掉,不管是否有slave集群进入fail状态.
二.Redis集群安装篇(centos5.8 X64系统)
(要让集群正常工做至少须要3个主节点,在这里咱们要建立6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系以下) redis
127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
1:下载redis。
官网下载3.0.0版本,以前2.几的版本不支持集群模式 下载地址:http://download.redis.io/releases/redis-3.0.2.tar.gz
2:上传服务器,解压,编译 tar -zxvf redis-3.0.2.tar.gz.tar.gz mv redis-3.0.2.tar.gz.tar.gz redis3.0 cd /usr/local/redis3.0 make make install
3:建立集群须要的目录 mkdir -p /usr/local/cluster cd /usr/local/cluster mkdir 7000 mkdir 7001 mkdir 7002 mkdir 7003 mkdir 7004 mkdir 7005 sql
4:修改配置文件redis.conf
cp /usr/local/redis3.0/redis.conf /usr.local/cluster vi redis.conf
##修改配置文件中的下面选项 port 7000 daemonize yes cluster-enabled yes
cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes
##修改完redis.conf配置文件中的这些配置项以后把这个配置文件分别拷贝到7000/7001/7002/7003/7004/7005目录下面
cp /usr/local/cluster/redis.conf /usr/local/cluster/7000 cp /usr/local/cluster/redis.conf /usr/local/cluster/7001 cp /usr/local/cluster/redis.conf /usr/local/cluster/7002 cp /usr/local/cluster/redis.conf /usr/local/cluster/7003 cp /usr/local/cluster/redis.conf /usr/local/cluster/7004 cp /usr/local/cluster/redis.conf /usr/local/cluster/7005
##注意:拷贝完成以后要修改7001/7002/7003/7004/7005目录下面redis.conf文件中的port参数,分别改成对应的文件夹的名称
5:分别启动这6个redis实例 cd /usr/local/cluster/7000 redis-server redis.conf shell
cd /usr/local/cluster/7001 redis-server redis.conf
cd /usr/local/cluster/7002 redis-server redis.conf
cd /usr/local/cluster/7003 redis-server redis.conf
cd /usr/local/cluster/7004 redis-server redis.conf
cd /usr/local/cluster/7005 redis-server redis.conf
##启动以后使用命令查看redis的启动状况ps -ef|grep redis 以下显示则说明启动成功
# ps -ef|grep redis
root 13703 1 0 10:03 ? 00:00:00 redis-server *:7000 [cluster] root 14015 1 0 10:04 ? 00:00:00 redis-server *:7002 [cluster] root 14133 1 0 10:04 ? 00:00:00 redis-server *:7003 [cluster] root 14172 1 0 10:04 ? 00:00:00 redis-server *:7004 [cluster] root 14187 1 0 10:04 ? 00:00:00 redis-server *:7005 [cluster] root 14323 1 0 10:04 ? 00:00:00 redis-server *:7001 [cluster]
6.升级ruby 安装gemcentos
安装gem 须要ruby的版本在 1.8.7 以上,默认的centos5 上都是1.8.5 版本,因此首先你的升级你的ruby , rpm
-ivh
http://yum.puppetlabs.com/el/5/products/x86_64/puppetlabs-release-5-6.noarch.rpm
yum install ruby ruby-devel rubygems rpm-build
检查 ruby 版本:
#ruby -v
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
是否安装rubygems: # rpm -qa|grep ruby ruby-rdoc-1.8.7.374-2.el5 ruby-1.8.7.374-2.el5 ruby-devel-1.8.7.374-2.el5 ruby-devel-1.8.7.374-2.el5 ruby-mode-1.8.5-24.el5 ruby-irb-1.8.7.374-2.el5 ruby-libs-1.8.7.374-2.el5 ruby-libs-1.8.7.374-2.el5 rubygems-1.3.7-1.el5
7.gem 安装redis ruby 接口 gem install redis
8:执行redis的建立集群命令建立集群 api
#redis-trib.rb的create子命令构建
#--replicas 则指定了为Redis Cluster中的每一个Master节点配备几个Slave节点
#节点角色由顺序决定,先master以后是slave
建立方式:
cd /usr/local/redis3.0/src ./redis-trib.rb
create
--replicas
1
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 错误笔记备注:
8.1执行上面的命令的时候会报错,由于是执行的ruby的脚本,须要ruby的环境
错误内容:/usr/bin/env: ruby: No such file or directory
因此须要安装ruby的环境,这里推荐使用yum install ruby安装 yum install ruby
8.2而后再执行第6步的建立集群命令,还会报错,提示缺乏rubygems组件,使用yum安装
错误内容:
./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError) from ./redis-trib.rb:24 yum install rubygems
8.3再次执行第8步的命令,还会报错,提示不能加载redis,是由于缺乏redis和ruby的接口,使用gem 安装 错误内容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in ruby
`gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from ./redis-trib.rb:25
gem install redis
8.4 再次执行第8步的命令,正常执行 输入yes,而后配置完成。服务器
注意观察 主从的配置:
默认是前三个节点 7000 7001 7002 是主, 后3个节点 7003 7004 7005 是从
若是是部署在不一样的服务器,请根据主从分部规则,分开在不一样的服务器
至此redis集群即搭建成功! 9:使用redis-cli命令进入集群环境 redis-cli -c -p 7000
三.测试篇
#redis-trib.rb的check子命令构建 #ip:port能够是集群的任意节点 ./redis-trib.rb check 1 127.0.0.1:7000
最后输出以下信息,没有任何警告或错误,表示集群启动成功并处于ok状态
2):添加新master节点
(1)添加一个master节点:建立一个空节点(empty node),而后将某些slot移动到这个空节点上,这个过程目前须要人工干预
a):根据端口生成配置文件(ps:establish_config.sh是我本身写的输出配置脚本)
sh establish_config.sh 6386 > conf/redis-6386.conf
):启动节点
nohup redis-server /opt/redis/conf/redis-6386.conf > /opt/redis/logs/redis-6386.log 2>&1 &
c):加入空节点到集群
add-node 将一个节点添加到集群里面, 第一个是新节点ip:port, 第二个是任意一个已存在节点ip:port
redis-trib.rb add-node 10.10.34.14:6386 10.10.34.14:6381
node:新节点没有包含任何数据, 由于它没有包含任何slot。新加入的加点是一个主节点, 当集群须要将某个从节点升级为新的主节点时, 这个新节点不会被选中
d):为新节点分配slot
redis-trib.rb reshard 10.10.34.14:6386
#根据提示选择要迁移的slot数量(ps:这里选择500) How many slots do you want to move (from 1 to 16384)? 500 #选择要接受这些slot的node-id
What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf #选择slot来源:
#all表示从全部的master从新分配,
#或者数据要提取slot的master节点id,最后用done结束 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
#打印被移动的slot后,输入yes开始移动slot以及对应的数据. #Do you want to proceed with the proposed reshard plan (yes/no)? yes #结束
3):添加新的slave节点 a):前三步操做同添加master同样
b)第四步:redis-cli链接上新节点shell,输入命令:cluster replicate 对应master的node-id
cluster replicate 2b9ebcbd627ff0fd7a7bbcc5332fb09e72788835
note:在线添加slave 时,须要dump整个master进程,并传递到slave,再由 slave加载rdb文件到内存,rdb传输过程当中Master可能没法提供服务,整个过程消耗大量io,当心操做.
例如本次添加slave操做产生的rdb文件
-rw-r--r-- 1 root root 34946 Apr 17 18:23 dump-6386.rdb -rw-r--r-- 1 root root 34946 Apr 17 18:23 dump-7386.rdb
4):在线reshard 数据:
对于负载/数据均匀的状况,能够在线reshard slot来解决,方法与添加新master的reshard同样,只是须要reshard的master节点是老节点. 5):删除一个slave节点
#redis-trib del-node ip:port '<node-id>' redis-trib.rb
del-node
10.10.34.14:7386
'c7ee2fca17cb79fe3c9822ced1d4f6c5e169e378' 6):删除一个master节点
a):删除master节点以前首先要使用reshard移除master的所有slot,而后再删除当前节点(目前只能把被删除 master的slot迁移到一个节点上)
#把10.10.34.14:6386当前master迁移到10.10.34.14:6380上 redis-trib.rb reshard 10.10.34.14:6380
#根据提示选择要迁移的slot数量(ps:这里选择500)
How many slots do you want to move (from 1 to 16384)? 500(被删除master的全部slot数量)
#选择要接受这些slot的node-id(10.10.34.14:6380)
What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2 (ps:10.10.34.14:6380的node-id) 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:f51e26b5d5ff74f85341f06f28f125b7254e61bf(被删除master的node-id)
Source node #2:done
#打印被移动的slot后,输入yes开始移动slot以及对应的数据. #Do you want to proceed with the proposed reshard plan (yes/no)? yes
b):删除空master节点
redis-trib.rb del-node 10.10.34.14:6386
'f51e26b5d5ff74f85341f06f28f125b7254e61bf' 四:redis cluster 客户端(Jedis) 1:客户端基本操做使用
<span style="color: #333333; font-family: Arial, sans-serif;"><span style="color: #333333; font-family: Arial, sans-serif;"> private static BinaryJedisCluster jc; static {
//只给集群里一个实例就能够
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>(); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6380)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6381)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6382)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6383)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6384)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7380)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7381)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7382)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7383)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7384)); jc = new BinaryJedisCluster(jedisClusterNodes); } @Test
public void testBenchRedisSet() throws Exception { final Stopwatch stopwatch = new Stopwatch(); List list = buildBlogVideos(); for (int i = 0; i < 1000; i++) {
String key = "key:" + i; stopwatch.start();
byte[] bytes1 = protostuffSerializer.serialize(list); jc.setex(key, 60 * 60, bytes1); stopwatch.stop(); }
System.out.println("time=" + stopwatch.toString()); }</span></span> 2:jedis客户端的坑.
1)cluster环境下redis的slave不接受任何读写操做,
2)client端不支持keys批量操做,不支持select dbNum操做,只有一个db:select 0 3)JedisCluster 的info()等单机函数没法调用,返回(No way to dispatch this command to Redis Cluster)错误,.
4)JedisCluster 没有针对byte[]的API,须要本身扩展(附件是我加的基于byte[]的BinaryJedisCluster api)
redis集群维护节点操做
一,redis cluster命令行
- //集群(cluster)
- CLUSTER INFO 打印集群的信息
- CLUSTER NODES 列出集群当前已知的全部节点(node),以及这些节点的相关信息。
- //节点(node)
- CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
- CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。
- CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。
- CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。
- //槽(slot)
- CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。
- CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。
- CLUSTER FLUSHSLOTS 移除指派给当前节点的全部槽,让当前节点变成一个没有指派任何槽的节点。
- CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,若是槽已经指派给另外一个节点,那么先让另外一个节点删除该槽>,而后再进行指派。
- CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。
- CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。
- CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。
- //键 (key)
- CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪一个槽上。
- CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。
- CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。
二,添加节点
1,新配置二个测试节点
- # cd /etc/redis
- //新增配置
- # cp redis-6379.conf redis-6378.conf && sed -i "s/6379/6378/g" redis-6378.conf
- # cp redis-6382.conf redis-6385.conf && sed -i "s/6382/6385/g" redis-6385.conf
- //启动
- # redis-server /etc/redis/redis-6385.conf > /var/log/redis/redis-6385.log 2>&1 &
- # redis-server /etc/redis/redis-6378.conf > /var/log/redis/redis-6378.log 2>&1 &
2,添加主节点
- # redis-trib.rb add-node 192.168.10.219:6378 192.168.10.219:6379
注释:
192.168.10.219:6378是新增的节点
192.168.10.219:6379集群任一个旧节点
3,添加从节点
- # redis-trib.rb add-node --slave --master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.220:6385 192.168.10.219:6379
注释:
--slave,表示添加的是从节点
--master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2,主节点的node id,在这里是前面新添加的6378的node id
192.168.10.220:6385,新节点
192.168.10.219:6379集群任一个旧节点
4,从新分配slot
- # redis-trib.rb reshard 192.168.10.219:6378 //下面是主要过程
- How many slots do you want to move (from 1 to 16384)? 1000 //设置slot数1000
- What is the receiving node ID? 03ccad2ba5dd1e062464bc7590400441fafb63f2 //新节点node id
- 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 //表示所有节点从新洗牌
- Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认从新分
新增长的主节点,是没有slots的,
M: 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.219:6378
slots:0-332,5461-5794,10923-11255 (0 slots) master
主节点若是没有slots的话,存取数据就都不会被选中。
能够把分配的过程理解成打扑克牌,all表示你们从新洗牌;输入某个主节点的node id,而后在输入done的话,就比如从某个节点,抽牌。
5,查看一下,集群状况
- [root@slave2 redis]# redis-trib.rb check 192.168.10.219:6379
- Connecting to node 192.168.10.219:6379: OK
- Connecting to node 192.168.10.220:6385: OK
- Connecting to node 192.168.10.219:6378: OK
- Connecting to node 192.168.10.220:6382: OK
- Connecting to node 192.168.10.220:6383: OK
- Connecting to node 192.168.10.219:6380: OK
- Connecting to node 192.168.10.219:6381: OK
- Connecting to node 192.168.10.220:6384: OK
- >>> Performing Cluster Check (using node 192.168.10.219:6379)
- M: 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 192.168.10.219:6379
- slots:5795-10922 (5128 slots) master
- 1 additional replica(s)
- S: 9c240333476469e8e2c8e80b089c48f389827265 192.168.10.220:6385
- slots: (0 slots) slave
- replicates 03ccad2ba5dd1e062464bc7590400441fafb63f2
- M: 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.219:6378
- slots:0-332,5461-5794,10923-11255 (1000 slots) master
- 1 additional replica(s)
- M: 19b042c17d2918fade18a4ad2efc75aa81fd2422 192.168.10.220:6382
- slots:333-5460 (5128 slots) master
- 1 additional replica(s)
- M: b2c50113db7bd685e316a16b423c9b8abc3ba0b7 192.168.10.220:6383
- slots:11256-16383 (5128 slots) master
- 1 additional replica(s)
- S: 6475e4c8b5e0c0ea27547ff7695d05e9af0c5ccb 192.168.10.219:6380
- slots: (0 slots) slave
- replicates 19b042c17d2918fade18a4ad2efc75aa81fd2422
- S: 1ee01fe95bcfb688a50825d54248eea1e6133cdc 192.168.10.219:6381
- slots: (0 slots) slave
- replicates b2c50113db7bd685e316a16b423c9b8abc3ba0b7
- S: 9a2a1d75b8eb47e05eee1198f81a9edd88db5aa1 192.168.10.220:6384
- slots: (0 slots) slave
- replicates 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
三,改变从节点的master
- //查看一下6378的从节点
- # redis-cli -p 6378 cluster nodes | grep slave | grep 03ccad2ba5dd1e062464bc7590400441fafb63f2
- //将6385加入到新的master
- # redis-cli -c -p 6385 -h 192.168.10.220
- 192.168.10.220:6385> cluster replicate 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 //新master的node id
- OK
- 192.168.10.220:6385> quit
- //查看新master的slave
- # redis-cli -p 6379 cluster nodes | grep slave | grep 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052
四,删除节点
1,删除从节点
- # redis-trib.rb del-node 192.168.10.220:6385 '9c240333476469e8e2c8e80b089c48f389827265'
2,删除主节点
若是主节点有从节点,将从节点转移到其余主节点
若是主节点有slot,去掉分配的slot,而后在删除主节点
- # redis-trib.rb reshard 192.168.10.219:6378 //取消分配的slot,下面是主要过程
- How many slots do you want to move (from 1 to 16384)? 1000 //被删除master的全部slot数量
- What is the receiving node ID? 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 //接收6378节点slot的master
- 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:03ccad2ba5dd1e062464bc7590400441fafb63f2 //被删除master的node-id
- Source node #2:done
- Do you want to proceed with the proposed reshard plan (yes/no)? yes //取消slot后,reshard
新增master节点后,也进行了这一步操做,当时是分配,如今去掉。反着的。
- # redis-trib.rb del-node 192.168.10.219:6378 '03ccad2ba5dd1e062464bc7590400441fafb63f2'
新的master节点被删除了,这样就回到了,就是这篇文章开头,尚未添加节点的状态