Redis-4 Cluster(集群)

官网: https://redis.io/topics/cluster-tutorial

Redis Cluster(集群)

安装:
需要安装ruby, 然后拷贝redis-trib.rb到bin

#1: 安装ruby
sudo apt-get install ruby
sudo gem install redis

yum install ruby
yum install rubygems

#2: 找到redis-trib.rb 拷贝到 usr/local/bin目录下
find / -name 'redis-trib.rb'
cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/

配置集群:
集群最少需要6台机器, 我们假设:
6379, 6380, 6381, 6382, 6383, 6384
6379的配置就用系统的默认配置, redis.conf
6380/6381/6382/6383/6384的配置如下(redis6380.conf)

port 6380

protected-mode no
daemonize yes

pidfile "/var/run/redis/redis-server6380.pid"
logfile "/var/log/redis/redis-server6380.log"

dir "/var/lib/redis"
dbfilename "dump6380.rdb"

appendfilename "appendonly6380.aof"

save 900 1
save 300 10
save 60 10000
databases 16

cluster-enabled yes
cluster-config-file nodes6380.conf
cluster-node-timeout 5000
appendonly yes

启动redis

#1: 启动redis6379 - 8364
#2: 启动集群
redis-trib.rb create --replicas 1 10.0.0.99:6379 10.0.0.99:6380 10.0.0.99:6381 10.0.0.99:6382 10.0.0.99:6383 10.0.0.99:6384
79(从83), 80(从84), 91(从82)

错误及解决办法:
错误1:
[ERR] Node 10.0.0.99:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决:
到/var/lib/redis 删除所有的node**.conf, *.rdf *.aof

连接到集群
写数据/读数据(注意一定要带上参数 -c)

redis-cli -c -p 6379 -h 10.0.0.99

查询集群状态

redis-trib.rb check 10.0.0.99:6379

模拟主(6379)挂掉

kill xxx
redis-trib.rb check 10.0.0.99:6379
#[ERR] Sorry, can't connect to node 10.0.0.99:6379

redis-trib.rb check 10.0.0.99:6380
#可以看出, 6083升为Master

#再次启动6379
redis-server redis.conf -h 10.0.0.99 -p 6379
#可以看出, 6083变为slave了

模拟主和从同时挂掉(6079, 6083)

kill 7909 7606
[ERR] Not all 16384 slots are covered by nodes.
#结果显示报错, 这个时候, 集群无法工作, 需要人工介入.

手动增加, 并进入集群

#首先启动6385
redis-server redis6385.conf 

#加入集群
redis-trib.rb add-node 10.0.0.99:6385 10.0.0.99:6380

#这个时候会有错误提示, 表示, 数据没有被分配到新加入的节点
[ERR] Not all 16384 slots are covered by nodes.

#解决:
redis-trib.rb reshard 10.0.0.99:6380

检查节点状态
redis-trib.rb check 10.0.0.99:6379

#发现
M: c6d81a55a151942973b228b3bd338e1158ac1e94 10.0.0.99:6385
   slots: (0 slots) master
   0 additional replica(s)
   
#说明没有分配槽到这个节点, 下面我们需要重新分片迁移
redis-trib reshard 10.0.0.99:6385

#提示需要移动多少个槽?
How many slots do you want to move (from 1 to 16384)?

#输入16384/4 =4096

#接着提示, 那个节点需要分配?
What is the receiving node ID?
#入6385这个节点的ID.

#接着他又问, 从哪个节点取? 输入all
Source node #1:

再次检查节点状态
redis-trib.rb check 10.0.0.99:6379
显示每个节点都有槽(slot)被利用

增加从节点
这个是没有指定主节点的从节点, 随机

redis-trib.rb add-node --slave 10.0.0.99:6386 10.0.0.99:6379
redis-trib.rb add-node --slave --master-id 74dc560998a077c78f47400abddcc3eded349f73 10.0.0.99:6387 10.0.0.99:6379

查看主节点下面几个从节点

redis-cli -c -p 6379 -h 10.0.0.99 cluster nodes | grep 74dc560998a077c78f47400abddcc3eded349f73

移除节点

redis-trib.rb del-node 10.0.0.99:6379 74dc560998a077c78f47400abddcc3eded349f73
#提示[ERR] Node 10.0.0.99:6379 is not empty! Reshard data away and try again.
#则需要重新分片redis-trib.rb reshard 10.0.0.99:6379

基准测试

redis-benchmark -q -n 100000

你可能感兴趣的:(Redis-4 Cluster(集群))