Redis3.0集群部署

Redis集群部署文档(centos6系统)

基本是按照官方文档去做的。

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

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.2稳定版本,之前2.几的版本不支持集群模式

下载地址:http://download.redis.io/releases/redis-3.0.2.tar.gz

2:上传服务器,解压,编译

首先安装依赖包。。。。

yum install -y gcc gcc-c++ kernel-devel
yum install –y gcc*
yum install -y tcl
yum install -y jemalloc jemalloc-devel
yum install -y ruby
yum install -y rubygems

解压安装包,

tar -zxvf redis-3.0.0-rc2.tar.gz 

mv redis-3.0.0-rc2.tar.gz redis3.0

cd /usr/local/redis3.0

make

如果报错 zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录

请换成 make MALLOC=libc

make test 

make install

3:创建集群需要的目录

mkdir -p /data0/redis_cluster

cd /data0/redis_cluster

mkdir 7000

mkdir 7001

mkdir 7002

mkdir 7003

mkdir 7004

mkdir 7005

 

4:修改配置文件redis.conf

cp /usr/local/redis3.0/redis.conf  /data0/redis_cluster

vi redis.conf

##修改配置文件中的下面选项

port 7000

cluster-config-file nodes-7004.conf

daemonize yes

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

appendonly yes

cluster-require-full-coverage no 默认是yes,只要有结点宕机导致16384个槽没全被覆盖,整个集群就全部停止服务,所以一定要改为no

dir /data0/redis/data


##修改完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:分别启动这6redis实例

cd /usr/local/cluster/7000

redis-server redis.conf

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

如下图显示则说明启动成功

 

6:执行redis的创建集群命令创建集群

cd /usr/local/redis3.0/src

./redis-trib.rb  create --replicas 1 192.168.0.30:7000 192.168.0.30:7001 192.168.0.30:7002 192.168.0.31:7003 192.168.0.31:7004 192.168.0.31:7005

6.1执行上面的命令的时候会报错,因为是执行的ruby的脚本,需要ruby的环境

错误内容:/usr/bin/env: ruby: No such file or directory

所以需要安装ruby的环境,这里推荐使用yum install ruby安装

yum install ruby

 

6.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

6.3再次执行第6步的命令,还会报错,提示不能加载redis,是因为缺少redisruby的接口,使用gem 安装

错误内容:

/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `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

此命令安装时会提示超时。根本无法下载。很明显被和谐了。。。

修改gem源,命令如下

$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l

# 请确保只有 ruby.taobao.org

下次执行

gem install redis

应该会成功。

 

6.4 再次执行第6步的命令,正常执行

cd /usr/local/redis3.0/src

./redis-trib.rb  create --replicas 1 192.168.0.30:7000 192.168.0.30:7001 192.168.0.30:7002 192.168.0.31:7003 192.168.0.31:7004 192.168.0.31:7005

##replicas 1 表示为每个主节点增加一个从节点


 

输入yes,然后配置完成。

 

至此redis集群即搭建成功!

7:使用redis-cli命令进入集群环境

测试代码   收藏代码
  1. $ redis-cli -c -p 7000  
  2. redis 127.0.0.1:7000> set foo bar  
  3. -> Redirected to slot [12182] located at 127.0.0.1:7002  
  4. OK  
  5. redis 127.0.0.1:7002> set name lixiaomng  
  6. -> Redirected to slot [866] located at 127.0.0.1:7000  
  7. OK  
  8. redis 127.0.0.1:7000> get foo  
  9. -> Redirected to slot [12182] located at 127.0.0.1:7002  
  10. "bar"  
  11. redis 127.0.0.1:7000> get name
  12. -> Redirected to slot [866] located at 127.0.0.1:7000  
  13. "lixiaomeng"  


 查看所有节点 

# redis-cli -h 127.0.0.1 -p 7000 cluster nodes

到此,redis 集群完成。


3:cluster 操作

cluster集群相关命令,更多redis相关命令见文档:http://redis.readthedocs.org/en/latest/

 

Java代码   收藏代码
  1. 集群  
  2. CLUSTER INFO 打印集群的信息  
  3. CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。  
  4. 节点  
  5. CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。  
  6. CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。  
  7. CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。  
  8. CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。  
  9. 槽(slot)  
  10. CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。  
  11. CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。  
  12. CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。  
  13. CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。  
  14. CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。  
  15. CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。  
  16. CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。  
  17. 键  
  18. CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。  
  19. CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。  
  20. CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。  
 

4:redis cluster 运维操作

1)初始化并构建集群

(1)启动集群相关节点(必须是空节点,beta3后可以是有数据的节点),指定配置文件和输出日志

 

Java代码   收藏代码
  1. redis-server /opt/redis/conf/redis-6380.conf > /opt/redis/logs/redis-6380.log 2>&1 &  
  2. redis-server /opt/redis/conf/redis-6381.conf > /opt/redis/logs/redis-6381.log 2>&1 &  
  3. redis-server /opt/redis/conf/redis-6382.conf > /opt/redis/logs/redis-6382.log 2>&1 &  
  4. redis-server /opt/redis/conf/redis-7380.conf > /opt/redis/logs/redis-7380.log 2>&1 &  
  5. redis-server /opt/redis/conf/redis-7381.conf > /opt/redis/logs/redis-7381.log 2>&1 &  
  6. redis-server /opt/redis/conf/redis-7382.conf > /opt/redis/logs/redis-7382.log 2>&1 &  

 

(2):使用自带的ruby工具(redis-trib.rb)构建集群

 

Java代码   收藏代码
  1. #redis-trib.rb的create子命令构建  
  2. #--replicas 则指定了为Redis Cluster中的每个Master节点配备几个Slave节点  
  3. #节点角色由顺序决定,先master之后是slave(为方便辨认,slave的端口比master大1000)  
  4. redis-trib.rb create --replicas 1 10.10.34.14:6380 10.10.34.14:6381 10.10.34.14:6382 10.10.34.14:7380 10.10.34.14:7381 10.10.34.14:7382  

(3):检查集群状态

Java代码   收藏代码
  1. #redis-trib.rb的check子命令构建  
  2. #ip:port可以是集群的任意节点  
  3. redis-trib.rb check 10.10.34.14:6380  
 最后输出如下信息,没有任何警告或错误,表示集群启动成功并处于ok状态
Java代码   收藏代码
  1. [OK] All nodes agree about slots configuration.  
  2. >>> Check for open slots...  
  3. >>> Check slots coverage...  
  4. [OK] All 16384 slots covered.  
2):添加新master节点

(1)添加一个master节点:创建一个空节点(empty node),然后将某些slot移动到这个空节点上,这个过程目前需要人工干预

a):根据端口生成配置文件(ps:establish_config.sh是我自己写的输出配置脚本)

 

Java代码   收藏代码
  1. sh establish_config.sh 6386 > conf/redis-6386.conf  
 

b):启动节点

 

Java代码   收藏代码
  1. redis-server /opt/redis/conf/redis-6386.conf > /opt/redis/logs/redis-6386.log 2>&1 &  

c):加入空节点到集群
add-node  将一个节点添加到集群里面, 第一个是新节点ip:port, 第二个是任意一个已存在节点ip:port

 

Java代码   收藏代码
  1. redis-trib.rb add-node 10.10.34.14:6386 10.10.34.14:6381  

node:新节点没有包含任何数据, 因为它没有包含任何slot。新加入的加点是一个主节点, 当集群需要将某个从节点升级为新的主节点时, 这个新节点不会被选中,同时新的主节点因为没有包含任何slot,不参加选举和failover。

 

d):为新节点分配slot

 

Java代码   收藏代码
  1. redis-trib.rb reshard 10.10.34.14:6386  
  2. #根据提示选择要迁移的slot数量(ps:这里选择500)  
  3. How many slots do you want to move (from 1 to 16384)? 500  
  4. #选择要接受这些slot的node-id  
  5. What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf  
  6. #选择slot来源:  
  7. #all表示从所有的master重新分配,  
  8. #或者数据要提取slot的master节点id,最后用done结束  
  9. Please enter all the source node IDs.  
  10.   Type 'all' to use all the nodes as source nodes for the hash slots.  
  11.   Type 'done' once you entered all the source nodes IDs.  
  12. Source node #1:all  
  13. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  
  14. #Do you want to proceed with the proposed reshard plan (yes/no)? yes  
  15. #结束  
3):添加新的slave节点

a):前三步操作同添加master一样

b)第四步:redis-cli连接上新节点shell,输入命令:cluster replicate 对应master的node-id

 

Java代码   收藏代码
  1. cluster replicate 2b9ebcbd627ff0fd7a7bbcc5332fb09e72788835  
 

注意:在线添加slave 时,需要bgsave整个master数据,并传递到slave,再由 slave加载rdb文件到内存,rdb生成和传输的过程中消耗Master大量内存和网络IO,以此不建议单实例内存过大,线上小心操作。

例如本次添加slave操作产生的rdb文件

 

Java代码   收藏代码
  1. -rw-r--r-- 1 root root  34946 Apr 17 18:23 dump-6386.rdb  
  2. -rw-r--r-- 1 root root  34946 Apr 17 18:23 dump-7386.rdb  

4):在线reshard 数据:

对于负载/数据不均匀的情况,可以在线reshard slot来解决,方法与添加新master的reshard一样,只是需要reshard的master节点是已存在的老节点.

5):删除一个slave节点

Java代码   收藏代码
  1. #redis-trib del-node ip:port '<node-id>'  
  2. redis-trib.rb del-node 10.10.34.14:7386 'c7ee2fca17cb79fe3c9822ced1d4f6c5e169e378'  

 6):删除一个master节点

  a):删除master节点之前首先要使用reshard移除master的全部slot,然后再删除当前节点

     (目前redis-trib.rb只能把被删除master的slot对应的数据迁移到一个节点上)

 

Java代码   收藏代码
  1. #把10.10.34.14:6386当前master迁移到10.10.34.14:6380上  
  2. redis-trib.rb reshard 10.10.34.14:6380  
  3. #根据提示选择要迁移的slot数量(ps:这里选择500)  
  4. How many slots do you want to move (from 1 to 16384)? 500(被删除master的所有slot数量)  
  5. #选择要接受这些slot的node-id(10.10.34.14:6380)  
  6. What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2 (ps:10.10.34.14:6380的node-id)  
  7. Please enter all the source node IDs.  
  8.   Type 'all' to use all the nodes as source nodes for the hash slots.  
  9.   Type 'done' once you entered all the source nodes IDs.  
  10. Source node #1:f51e26b5d5ff74f85341f06f28f125b7254e61bf(被删除master的node-id)  
  11. Source node #2:done  
  12. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  
  13. #Do you want to proceed with the proposed reshard plan (yes/no)? yes  
 

b):删除空master节点

 

Java代码   收藏代码
  1. redis-trib.rb del-node 10.10.34.14:6386 'f51e26b5d5ff74f85341f06f28f125b7254e61bf'  
三:redis cluster 客户端(Jedis)

1:客户端基本操作使用

 

Java代码   收藏代码
  1. <span style="font-size: 16px;"private static BinaryJedisCluster jc;  
  2.   static {  
  3.        //只给集群里一个实例就可以  
  4.         Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();  
  5.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"6380));  
  6.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"6381));  
  7.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"6382));  
  8.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"6383));  
  9.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"6384));  
  10.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"7380));  
  11.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"7381));  
  12.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"7382));  
  13.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"7383));  
  14.         jedisClusterNodes.add(new HostAndPort("10.10.34.14"7384));  
  15.         jc = new BinaryJedisCluster(jedisClusterNodes);  
  16.     }  
  17. @Test  
  18.     public void testBenchRedisSet() throws Exception {  
  19.         final Stopwatch stopwatch = new Stopwatch();  
  20.         List list = buildBlogVideos();  
  21.         for (int i = 0; i < 1000; i++) {  
  22.             String key = "key:" + i;  
  23.             stopwatch.start();  
  24.             byte[] bytes1 = protostuffSerializer.serialize(list);  
  25.             jc.setex(key, 60 * 60, bytes1);  
  26.             stopwatch.stop();  
  27.         }  
  28.         System.out.println("time=" + stopwatch.toString());  
  29.     }</span>  

2:redis-cluster客户端的一些坑.

1)cluster环境下slave默认不接受任何读写操作,在slave执行readonly命令后,可执行读操作

2)client端不支持多key操作(mget,mset等),但当keys集合对应的slot相同时支持mget操作见:hash_tag

3)不支持多数据库,只有一个db,select 0。

4)JedisCluster 没有针对byte[]的API,需要自己扩展(附件是我加的基于byte[]的BinaryJedisCluster  api)


部分内容来自于网络。。。。。。。

以下内容来自于网络。 

1)redis通用配置.
Java代码   收藏代码
  1. #GENERAL  
  2. daemonize no  
  3. tcp-backlog 511  
  4. timeout 0  
  5. tcp-keepalive 0  
  6. loglevel notice  
  7. databases 16  
  8. dir /opt/redis/data  
  9. slave-serve-stale-data yes  
  10. #slave只读  
  11. slave-read-only yes  
  12. #not use default  
  13. repl-disable-tcp-nodelay yes  
  14. slave-priority 100  
  15. #打开aof持久化  
  16. appendonly yes  
  17. #每秒一次aof写  
  18. appendfsync everysec  
  19. #关闭在aof rewrite的时候对新的写操作进行fsync  
  20. no-appendfsync-on-rewrite yes  
  21. auto-aof-rewrite-min-size 64mb  
  22. lua-time-limit 5000  
  23. #打开redis集群  
  24. cluster-enabled yes  
  25. #节点互连超时的阀值  
  26. cluster-node-timeout 15000  
  27. cluster-migration-barrier 1  
  28. slowlog-log-slower-than 10000  
  29. slowlog-max-len 128  
  30. notify-keyspace-events ""  
  31. hash-max-ziplist-entries 512  
  32. hash-max-ziplist-value 64  
  33. list-max-ziplist-entries 512  
  34. list-max-ziplist-value 64  
  35. set-max-intset-entries 512  
  36. zset-max-ziplist-entries 128  
  37. zset-max-ziplist-value 64  
  38. activerehashing yes  
  39. client-output-buffer-limit normal 0 0 0  
  40. client-output-buffer-limit slave 256mb 64mb 60  
  41. client-output-buffer-limit pubsub 32mb 8mb 60  
  42. hz 10  
  43. aof-rewrite-incremental-fsync yes  
2)redis特殊配置.
Java代码   收藏代码
  1. #包含通用配置  
  2. include /opt/redis/redis-common.conf  
  3. #监听tcp端口  
  4. port 6379  
  5. #最大可用内存  
  6. maxmemory 100m  
  7. #内存耗尽时采用的淘汰策略:  
  8. volatile-lru -> remove the key with an expire set using an LRU algorithm  
  9. # allkeys-lru -> remove any key accordingly to the LRU algorithm  
  10. volatile-random -> remove a random key with an expire set  
  11. # allkeys-random -> remove a random key, any key  
  12. volatile-ttl -> remove the key with the nearest expire time (minor TTL)  
  13. # noeviction -> don't expire at all, just return an error on write operations  
  14. maxmemory-policy allkeys-lru  
  15. #aof存储文件  
  16. appendfilename "appendonly-6379.aof"  
  17. #不开启rdb存储,只用于添加slave过程  
  18. dbfilename dump-6379.rdb  
  19. #cluster配置文件(启动自动生成)  
  20. cluster-config-file nodes-6379.conf  
  21. #部署在同一机器的redis实例,把auto-aof-rewrite搓开,因为cluster环境下内存占用基本一致.  
  22. #防止同意机器下瞬间fork所有redis进程做aof rewrite,占用大量内存(ps:cluster必须开启aof)  
  23. auto-aof-rewrite-percentage 80-100  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值