redis 集群架构 cluster sentinel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis 集群架构 cluster sentinel相关的知识,希望对你有一定的参考价值。

redis-cluster

实验环境:

centos6.5   IP192.168.1.11

依赖包:redis    ruby   rubygem

 

 

[[email protected] redis]#tar xf redis-3.0.2.tar.gz

[[email protected] redis]#cd redis-3.0.2

[[email protected] redis]#make &&make install

tab键看redis-  这些工具是否安装好,没安装则cp/usr/local/bin

[[email protected] redis]#which redis-cli

[[email protected] redis]#cp /root/redis-3.0.2/src/redis-trib.rb /usr/local/bin/redis-trib

[[email protected] redis]#mkdir /usr/local/redis

[[email protected] redis]#cd /usr/local/redis/

[[email protected] redis]#mkdir `seq 7001 7008`

[[email protected] redis]# cp ~/redis-3.0.2/redis.conf ./

[[email protected]ost redis]# ls

7001  7002  7003  7004  7005  7006  7007  7008  redis.conf

 

配置文件里,这三行配置:

daemonize yes//后台挂起

cluster-enabled yes//开启集群

cluster-config-file nodes-7001.conf//集群节点配置文件

cluster-node-timeout 5000//超时时间,毫秒

appendonly yes并且appendonly要开启:

 

 

for批量修改:

for i in {7001..7008};do sed  "s/6379/$i/" redis.conf >$i/redis.conf;done

 

for批量启动:

for i in {7001..7008};do cd /usr/local/redis/$i&&redis-server redis.conf ;done

[[email protected] 7008]# ps aux|grep redis//查看redis启动状态

 

 

联网  yum -y install ruby rubygems   建议用yum吧,用源码弄了半天弄不好

 

[[email protected] 7001]# gem install redis安装rubyredis接口

 

 

接下来就是redis-trib  工具出场了:

[[email protected] redis]# redis-trib help//查看能否使用

 

[[email protected] redis]# redis-trib create --replicas 1 192.168.1.11:7001 192.168.1.11:7002 192.168.1.11:7003 192.168.1.11:7004 192.168.1.11:7005 192.168.1.11:7006

Can I set the above configuration? (type ‘yes‘ to accept):  yes   //这里输入yes

 

#--replicas 1 代表复制一份,即每个主一个从

#redis cluster最低要求三个主, 定义是host1:port host2:port

#如果--replicas 2则:

#host1:port == master host2:port &host3:port

host1 host2 host3的主

如果是创建9个是节点   --replicas 2  a b c d e f g h i 则,d e a的从,f gb的从  h ic的从

 

[[email protected] redis]# redis-cli -c -h 127.0.0.1 -p 7001#连接7001

127.0.0.1:7001> cluster nodes#查看集群节点

127.0.0.1:7001> cluster info#查看集群信息

cluster_state:ok

 

 

集群扩容

[[email protected] redis]# redis-trib add-node 192.168.1.11:7007 192.168.1.11:7001

redis-trib add-node 要加的节点   现有的任意节点

 

M: ff2b5205f2c1531422052a7c4c4feca3db046878 192.168.1.11:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: ef4ed8963e868ed9befa2fc25049039d270f7dbe 192.168.1.11:7005

   slots: (0 slots) slave

   replicates 721b0611a6a8a6e027d6619091a747b05a0529c2

S: 1538fe39b9694912c80024fc2bfcacde9ddbfb47 192.168.1.11:7004

   slots: (0 slots) slave

   replicates ff2b5205f2c1531422052a7c4c4feca3db046878

M: 721b0611a6a8a6e027d6619091a747b05a0529c2 192.168.1.11:7002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

M: ef07b1bbb70392b4ac57f7d80a0f4ae7a13643f7 192.168.1.11:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 0d2e18cbbc2d6abd9cd72e554c8b7484fb089ccb 192.168.1.11:7006

   slots: (0 slots) slave

   replicates ef07b1bbb70392b4ac57f7d80a0f4ae7a13643f7

 

 

[[email protected] redis]# redis-cli -c -h 127.0.0.1 -p 7001

127.0.0.1:7001> cluster info

cluster_state:ok

cluster_slots_assigned:16384

...

127.0.0.1:7001> cluster nodes

ef4ed8963e868ed9befa2fc25049039d270f7dbe 192.168.1.11:7005 slave 721b0611a6a8a6e027d6619091a747b05a0529c2 0 1486917802211 5 connected

1538fe39b9694912c80024fc2bfcacde9ddbfb47 192.168.1.11:7004 slave ff2b5205f2c1531422052a7c4c4feca3db046878 0 1486917801707 4 connected

78a65dcd0b5165ecdab3ab2320bf975a35a66cf9 192.168.1.11:7007 master - 0 1486917802714 0 connected

ff2b5205f2c1531422052a7c4c4feca3db046878 192.168.1.11:7001 myself,master - 0 0 1 connected 0-5460

721b0611a6a8a6e027d6619091a747b05a0529c2 192.168.1.11:7002 master - 0 1486917801202 2 connected 5461-10922

ef07b1bbb70392b4ac57f7d80a0f4ae7a13643f7 192.168.1.11:7003 master - 0 1486917803218 3 connected 10923-16383

0d2e18cbbc2d6abd9cd72e554c8b7484fb089ccb 192.168.1.11:7006 slave ef07b1bbb70392b4ac57f7d80a0f4ae7a13643f7 0 1486917801202 6 connected

 

前一大串是节点里的ID,新上来的节点没有槽位,得重新分片:

[[email protected] redis]# redis-trib reshard 192.168.1.11:7007//7007分片

How many slots do you want to move (from 1 to 16384)?1000     //70071000槽位

What is the receiving node ID?  //接受节点的ID,这里复制7007ID过来

Source node #1:all//从源节点过来

 

添加70087007做从

[[email protected]]# redis-trib add-node     192.168.1.11:7008    192.168.1.11:7001

 

[[email protected] redis]# redis-cli -c -p 7008//进入7008节点

127.0.0.1:7008> cluster nodes//查看节点,现在他还是主

127.0.0.1:7008> cluster replicate 78a65dcd0b5165ecdab3ab2320bf975a35a66cf9

OK//复制7007ID过来

127.0.0.1:7008> cluster nodes//现在就变为7007的从了

 

删除节点:

[[email protected] redis]# redis-trib del-node 192.168.1.11:7008 6a8a8c8a4c9640d2cda682de85ac9b83b837e2cd

 

redis-trib del-ode 主机:端口 ID

 

 

查看节点以及槽位:

[[email protected] redis]# redis-trib check 192.168.1.11:7001

 

 

转移槽位:如果要删除主,则需要把槽位给删了在删除节点。

[[email protected] 7009]# redis-trib reshard 192.168.1.11:7002

How many slots do you want to move (from 1 to 16384)?

需要移动的数目:

 

What is the receiving node ID?

接受这些槽位的ID//目标节点

 

Source node #1:

从何处移到上面所指定的ID  //源节点

 

Source node #2:done//结束

 

[[email protected] redis]# pkill redis//关闭所有redis

 

 

 

可以看到redis-trib.rb具有以下功能:
1create:创建集群
2check:检查集群
3info:查看集群信息
4fix:修复集群
5reshard:在线迁移slot
6rebalance:平衡集群节点slot数量
7add-node:将新节点加入集群
8del-node:从集群中删除节点
9set-timeout:设置集群节点间心跳连接的超时时间
10call:在集群全部节点上执行命令
11import:将外部redis数据导入集群




redis-sentinel

IP分布:

reids master  192.168.1.11  6379

reids slalve   192.168.1.11  6380

reids slave   192.168.1.11  6381

reids sentinel  192.168.1.11  26379

 

先启动所有reids,(修改好配置,配置好主从)

redis-server /usr/local/redis/6379/redis.conf &

redis-server /usr/local/redis/6380/redis.conf &

redis-server /usr/local/redis/6381/redis.conf &

 

[[email protected] 6379]# ps aux|grep redis

root     17151  0.1  0.5 137436  2736 pts/0    Sl   07:43   0:00 redis-server *:6379                          

root     17165  0.2  0.5 137436  2680 pts/0    Sl   07:43   0:00 redis-server *:6381                                                 

root     17204  0.2  0.5 137436  2636 pts/0    Sl   07:45   0:00 redis-server *:6380  

 

cp /root/redis-3.0.0-rc2/sentinel.conf /usr/local/redis/

vi /usr/local/redis/sentinel.conf#一份配置文件可以配置多个群集组

sentinel monitor mymaster 192.168.7.40 6379 1

#监听的群集,1代表一个slave节点ping不通,则认为master宕机

sentinel down-after-milliseconds mymaster  5000

#sentinelmaster发送ping,多长时间内没通则认为宕机。单位为毫秒

sentinel failover-timeout mymaster  900000

#failover后,如果此时间内没有任何failover操作,则默认为failover失败

sentinel parallel-syncs mymaster  2 

    #最多有多少个slave对新master进行同步###mymaster可以自己定义

sentinel can-failover def_master yes 

#sentinel是否实施failoverno则只参与投票,不实施failover 

 

redis-server /usr/local/redis/sentinel.conf  --sentinel &启动sentinel

 

[[email protected] 6379]# ps aux|grep redis

root     17151  0.1  0.4 137436  2448 pts/0    Sl   07:43   0:02 redis-server *:6379                          

root     17165  0.1  0.5 137436  2536 pts/0    Sl   07:43   0:02 redis-server *:6381                          

root     17172  0.3  0.4 137436  2436 pts/0    Sl   07:43   0:05 redis-server *:26379 [sentinel]                            

root     17204  0.1  0.5 137436  2488 pts/0    Sl   07:45   0:02 redis-server *:6380                          

root     17419  0.0  0.1 103216   772 pts/0    S+   08:08   0:00 grep redis

 

 

[[email protected] 6379]# redis-cli -p 26379 info sentinel查看sentinel信息

# Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

master0:name=mymaster,status=ok,address=127.0.0.1:6381,slaves=2,sentinels=1    #红色部分为master

[[email protected] 6379]# redis-cli -p 26379 sentinel slaves mymaster查看集群从节点

 

[[email protected] ~]# redis-cli -p 6379 info Replication查看单个节点的角色 

 

故障演示:  (自行完成),当主宕机后,从代替,主恢复后,变成新主的从。

 

恢复数据:先把redis关掉,然后把.aof.rdb结尾的文件导入配置文件目录下。启动redis



本文出自 “12384524” 博客,请务必保留此出处http://12394524.blog.51cto.com/12384524/1897107

以上是关于redis 集群架构 cluster sentinel的主要内容,如果未能解决你的问题,请参考以下文章

高可用集群架构——redis的主从复制与哨兵模式,cluster

Redis高可用架构之Cluster集群和分⽚

redis cluster 集群畅谈

6.Redis集群

Redis Cluster集群

redis cluster集群理解