Redis-cluster集群安装方案

Redis-cluster集群安装方案

概述:

Redis Cluster是Redis的分布式解决方案,在Redis 3.0版本正式推出的,有效解决了Redis分布式方面的需求。当遇到单机内存、并发、流量等瓶颈时,可以采用Cluster架构达到负载均衡的目的。

Redis Cluster采用虚拟槽分区,因此先介绍一下虚拟槽分区。

虚拟槽分区巧妙地使用了哈希空间,使用分散度良好的哈希函数把所有的数据映射到一个固定范围内的整数集合,整数定义为槽(slot)。比如Redis Cluster槽的范围是0 ~ 16383。槽是集群内数据管理和迁移的基本单位。采用大范围的槽的主要目的是为了方便数据的拆分和集群的扩展,每个节点负责一定数量的槽。

 

规划:

环境信息:

redis版本: 3.2.3

操作系统:CentOSrelease 6.5

内核版本:2.6.32-431.el6.x86_64

主从关系:

三主三从集群

172.19.216.27:6379 (主) <---- 172.19.216.30:6379(从)

172.19.216.28:6379 (主) <---- 172.19.216.31:6379(从)

172.19.216.29:6379 (主) <---- 172.19.216.32:6379(从)

 

 

安装步骤:

1.上传redis安装包redis-3.2.3.tar.gz

2.解压安装包,创建安装目录

tar –zxvf  redis-3.2.3.tar.gz

mkdir  -p /apps/service/usr/local/redis

3.源码安装redis

cd  redis-3.2.3

makePREFIX=/apps/service/usr/local/redis install

make  test

注:make test 会报以下错:

***[err]: Test replication partial resync: ok psync(diskless: yes, reconnect: 1) in tests/integration/replication-psync.tcl

解决方案:

a.进入源码包中

cd /apps/service/redis-3.2.3/tests/integration

b.打开文件replication-psync.tcl

vim replication-psync.tcl

把对应报错的那段代码中的 after后面的数字,从100改成 500

c.重新make test  就不会报错了

4.创建目录、创建日志文件

mkdir-p /apps/service/usr/local/redis/{conf,logs}

touch /apps/service/usr/local/redis/logs/redis.log           #redis日志

touch /apps/service/usr/local/redis/redis.conf          #redis配置文件

 

5.配置文件写入

vim  /apps/service/usr/local/redis/redis.conf

写入以下信息(标红需要根据实际情况修改,其他为源码中配置文件默认信息):

bind 172.19.216.28

protected-modeyes

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive300

daemonize yes

supervised no

pidfile/apps/service/usr/local/redis/conf/redis_6379.pid

loglevel notice

logfile/apps/service/usr/local/redis/logs/redis.log

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-erroryes

rdbcompressionyes

rdbchecksum yes

dbfilenamedump.rdb

dir /apps/service/usr/local/redis

slave-serve-stale-datayes

slave-read-onlyyes

repl-diskless-syncno

repl-diskless-sync-delay5

repl-disable-tcp-nodelayno

slave-priority100

#requirepass123456

appendonly yes

appendfilename"appendonly.aof"

appendfsynceverysec

no-appendfsync-on-rewriteno

auto-aof-rewrite-percentage100

auto-aof-rewrite-min-size64mb

aof-load-truncatedyes

lua-time-limit5000

cluster-enabled yes    #开启集群开关

cluster-config-file/apps/service/usr/local/redis/conf/nodes-6379.conf

cluster-node-timeout 15000

slowlog-log-slower-than10000

slowlog-max-len128

latency-monitor-threshold0

notify-keyspace-events""

hash-max-ziplist-entries512

hash-max-ziplist-value64

list-max-ziplist-size-2

list-compress-depth0

set-max-intset-entries512

zset-max-ziplist-entries128

zset-max-ziplist-value64

hll-sparse-max-bytes3000

activerehashingyes

client-output-buffer-limitnormal 0 0 0

client-output-buffer-limitslave 256mb 64mb 60

client-output-buffer-limitpubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsyncyes

6.启动redis

cd /apps/service/usr/local/redis/bin

./redis-server /apps/service/usr/local/redis/redis.conf     

7.同样以上步骤安装其余5台单机redis

8.配置集群

a.发现集群机器

随意登录一台redis单机

./redis-cli -h 172.19.216.27

执行其余5台ip发现命令

>CLUSTER MEET172.19.216.28 6379

>CLUSTER MEET172.19.216.29 6379

>CLUSTER MEET172.19.216.30 6379

>CLUSTER MEET172.19.216.31 6379

>CLUSTER MEET172.19.216.32 6379

执行命令cluster nodes 查看时候6台机器已互相发现

b.批量分配slots,三台主服务平均分配16384个solts

for i in{0..5461}; do ./redis-cli -h 172.19.216.27-p 6379 cluster addslots $i; done  

for i in {5462..10922}; do ./redis-cli -h172.19.216.28 -p 6379 cluster addslots $i; done  

for i in {10923..16383}; do ./redis-cli -h 172.19.216.29-p 6379 cluster addslots $i; done  

c.配置主从关系

登录从服务,配置目标主服务的nodeid ,可以通过cluster nodes  查看集群内nodeid

/redis-cli -h172.19.216.30

172.19.216.30:6379>cluster replicate 322945e79c5d11cbed99ab1ae4f478d482fee394

/redis-cli -h172.19.216.31

172.19.216.31:6379>cluster replicate   41e8860fb7978e3f120220b9bde2f671b27401a3

 /redis-cli -h 172.19.216.32

172.19.216.32:6379>cluster replicate cfea07ddf8b6e4f85915f364b390663c43667bf4

执行cluster nodes,三主三从如下,表示主从关系成功

至此集群模式搭建成功,下面是验证阶段

验证:

1.随机写入信息

连接集群 使用参数-c

./redis-cli  -c -h172.19.216.27

通过CRC16 算法,分配key 所在的slot,在查询和插入数据时候,自动重定向。

2.一台主服务宕机,该主服务的key 重定向到相应从服务

a.创建一个key ,根据算法分配到服务器28上,并重定向能够获取

b.停掉28主机上的redis服务

c.获取key test的值,请求被重定向到28的从机,31上了

至此,主从数据同步,从机在主服务不存在时候,自动切换,完成高可用。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值