Redis集群使用总结(一)

来源:互联网 发布:降维算法 编辑:程序博客网 时间:2024/04/29 00:46

Redis集群使用总结(一):

随着企业数据量的增多,Redis不论作为数据存储或是缓存,它的数据量也会逐渐增多,虽然Redis的速度非常可观,但随着其中的数据量的庞大,并且仅仅在一个设备或是一个Redis实例中,其存取速度也会大打折扣,所以我们需要在不同的设备或服务器上,搭建多个Redis实例仓库,将原来的Redis的所有的keys分发到各个服务器的Redis上,这就是现在所谓的Redis集群(Redis Cluster)。

 

·     原理

·     实施

·     注意

·     问题

 

一、原理

1、数据共享

 

Redis提供多个节点实例间的数据共享,也就是Redis A,B,C,D彼此之间的数据是同步的,同样彼此之间也可以通信,而对于客户端操作的keys是由Redis系统自行分配到各个节点中。

 

2、主从复制

 

Redis的多个实例间通信时,一旦其中的一个节点故障,那么Redis集群就不能继续正常工作,所以需要一种复制机制(Master-Slave)机制,做到一旦节点A故障了,那么其从节点A1和A2就可以接管并继续提供与A同样的工作服务,当然如果节点A,A1,A2节点都出现问题,那么同样这个集群不会继续保持工作,但是这种情况比较罕见,即使出现了,也会及时发现并修复使用。

建议:部署主从复制机制(Master-Slave)。

 

3、哈希槽值

 

Redis集群中使用哈希槽来存储客户端的keys,而在Redis中,目前存在16384个哈希槽,它们被全部分配给所有的节点,正如上图所示,所有的哈希槽值被节点A,B,C分配完成了。

 

二、实施

1、创建集群

A、辅助工具安装

在搭建Redis Cluster之前,请先确保系统已经安装了zlib和ruby(含rubygems)软件依赖包,请自行安装。

 

B、安装redis-cluster

在Redis的源码路径下,位于src目录中的redis-trib.rb文件是用来搭建和维护集群的工具,我们需要将其放入与redis-server和redis-cli同样的指令环境下,如下:

sudo cp /redis/redis-3.0.7/src/redis-trib.rb  /redis/bin

sudo cp /redis/redis-3.0.7/src/redis-server  /redis/bin

sudo cp /redis/redis-3.0.7/src/redis-cli  /redis/bin

 

C、配置redis-cluster

文件结构:

 

通用配置:

#generate configs

daemonize no

tcp-backlog 511

timeout 2000

tcp-keepalive 0

loglevel notice

databases 16

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay yes

slave-priority 100

#open the aof persistence

appendonly yes

#config aof mode for everysec

appendfsync everysec

#while rewrite op then close the aof write mode

no-appendfsync-on-rewrite yes

auto-aof-rewrite-min-size 64mb

aof-rewrite-incremental-fsync yes

#limit the time of lua scripts executes

lua-time-limit 5000

#open redis cluster switch

cluster-enabled yes

#timeout of nodes connections

cluster-node-timeout 15000

cluster-migration-barrier 1

slowlog-log-slower-than 10000

slowlog-max-len 128

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

#open the online rehash

activerehashing yes

 

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

 

hz 10

 

特殊配置:

这里以6379端口为例说明,其它的特殊配置只需修改对应的端口即可:

#common configs

include /redis/etc/redis-common.conf

#listen tcp port

port 6379

#memory cahce max size

maxmemory 100m

maxmemory-policy allkeys-lru

#aof filename

appendfilename "appendonly-6379.aof"

#rdb file,only use in the slave handle

dbfilename dump-6379.rdb

dir /redis/db-6379

#cluster config that auto create

cluster-config-file nodes-6379.conf

#log path

logfile /redis/logs/6379/log-6379.log

 

#in the same computer redis,then give the limit

#fork all redis processes done rewrite,using big memory

auto-aof-rewrite-percentage 80-100

 

D、搭建redis-cluster

在这里,我们使用Redis自带的ruby工具redis-trib.rb来创建和管理集群。本人共创建了6个redis节点,分别启动之后,使用./redis-trib.rb create –replicas配置和生成3个主节点和对应每个节点的3个从节点。

首先,启动6个节点实例:

$redis-server /redis/etc/redis-6379.conf

$redis-server  /redis/etc/redis-6380.conf

$redis-server  /redis/etc/redis-6381.conf

$redis-server  /redis/etc/redis-7379.conf

$redis-server  /redis/etc/redis-7380.conf

$redis-server  /redis/etc/redis-7381.conf

 

其次,创建分配主从节点:

$redis-trib.rb create --replicas 1

127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:7379 127.0.0.1:7380127.0.0.1:7381

 

执行的结果:

 

NOTE:

--replicas 1代表创建1个从节点,创建的从节点的顺序是按照6379-7379,6380-7380及6381-7381的顺序创建配置的。

 

从上图知道,已经创建和配置了3个Master和对应的3个Sub子节点,同时会提示您是否同意这个配置生成,当你回复yes之后,显示如下:

 

NOTE:

从上图知道,Redis集群已经创建和配置成功了,并且redis的16384个哈希槽已经全部分配完成。

 

2、集群验证

验证可以使用redis-rb-cluster或是redis-cli来验证,这里以最简单的方式redis-cli来验证使用,意在说明集群的使用和校验。

A、哈希槽分配

$redis-cli -c -p 6379

127.0.0.1:6379> set mykey "hello"

-> Redirected to slot [14687] located at 127.0.0.1:6381

 

$redis-cli -c -p 6380

127.0.0.1:6380> set mykey2 "hello world"

-> Redirected to slot [14119] located at 127.0.0.1:6381

OK

 

$redis-cli -c -p 6381

127.0.0.1:6381> set mykey3 "hello"

-> Redirected to slot [9990] located at 127.0.0.1:6380

OK

 

B、集群验证

$redis-trib.rb  127.0.0.1:7379

 

结果:

 

右上图,可以知道集群配置没问题,继续往下验证下数据的异步同步。

 

C、集群数据共享

$redis-cli -c -p 6379

127.0.0.1:6379> set mykey "hello"

-> Redirected to slot [14687] located at 127.0.0.1:6381

OK

127.0.0.1:6381> get mykey2

"hello world"

127.0.0.1:6381> get mykey3

-> Redirected to slot [9990] located at 127.0.0.1:6380

"hello"

 

NOTE:

从上面可以看出,我实例子6379中可以访问同一个集群内的节点数据,访问的机制是根据set时分配的哈希槽,例如:在6379中,使用get mykey3,那么自动定位到6380。  

 

三、注意

1、主从复制

Redis集群支持主从复制功能,也就是主节点对应的从节点,但是不需要在从节点中加入slaveof <server>:<port>,否则会报错哦。

 

2、主从配置

一般情况下,从节点的配置和对应的主节点的配置类似,但是一般从节点的大小要小于主节点的配置大小,这主要考虑内存和性能均衡方面,请在实际使用时留意下。

 

3、实例通信

Redis集群中的节点实例间的数据共享机制是通过定位哈希槽(set时的键值分配的哈希),不会区分主从节点或是普通节点的通信。

 

四、问题

遇到问题:

custom_require.rb:36:in `require': cannot load such file -- redis(LoadError)

from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'

from ./redis-trib.rb:25:in `<main>'

解决办法:

sudo gem install redis来安装ruby和redis的接口包即可

 

 

 

 

 

 

 

 

技术讨论群:

489451956(新)

 

 

 

 

1 0
原创粉丝点击