redis 5 - redis cluster集群搭建及常用命令
简述
在redis 5版本,放弃了Ruby脚本管理集群的方式,使用redis-cli就可以直接操作redis集群。操作方式也跟之前Ruby脚本的方式很像。
redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
环境介绍
redis版本:5.0.3
操作系统:Manjaro 18.0.3 Illyria
集群节点个数:6
由于测试,都是在本机运行,如果是在多台服务器上,修改一下对应的IP和端口即可。
配置
protected-mode no
port 7001
daemonize yes
pidfile /data/data/redis/logs/redis_7001.pid
loglevel notice
logfile "/data/data/redis/logs/redis_7001.log"
databases 16
dbfilename dump_7001.rdb
dir /data/data/redis/
maxmemory 2gb
appendonly yes
appendfilename "appendonly_7001.aof"
appendfsync everysec
slowlog-log-slower-than 10000
slowlog-max-len 128
cluster-enabled yes
cluster-config-file nodes-7001.conf
每个节点需要指定集群配置文件,最后两行是启用cluster功能。多个节点,替换里面的7001即可。
启动节点
/data/apps/redis/bin/redis-server /data/apps/redis/etc/7001.conf
/data/apps/redis/bin/redis-server /data/apps/redis/etc/7002.conf
/data/apps/redis/bin/redis-server /data/apps/redis/etc/7003.conf
/data/apps/redis/bin/redis-server /data/apps/redis/etc/7004.conf
/data/apps/redis/bin/redis-server /data/apps/redis/etc/7005.conf
/data/apps/redis/bin/redis-server /data/apps/redis/etc/7006.conf
创建集群
--cluster-replicas 1 设置副本节点为1,即一个主节点至少有一个从节点,执行过程会列出slot及主从分配
/data/apps/redis/bin/redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 2a122a67a56688c6d607608f80a6b9751c1b4430 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: e6a12a074bb4e4160717fd6b57b8b187e687ef98 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
M: fc48069a547305efee0fb6ee0467d6fbc747d0f6 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
S: 322b12564f3e5ed03cebd990d068e3c169090ac3 127.0.0.1:7004
replicates 2a122a67a56688c6d607608f80a6b9751c1b4430
S: c5ae1fec5aab57337a338701cf857eb09e19b244 127.0.0.1:7005
replicates e6a12a074bb4e4160717fd6b57b8b187e687ef98
S: af4328273da76a7ca27732df9f013528fea9902a 127.0.0.1:7006
replicates fc48069a547305efee0fb6ee0467d6fbc747d0f6
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 2a122a67a56688c6d607608f80a6b9751c1b4430 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 322b12564f3e5ed03cebd990d068e3c169090ac3 127.0.0.1:7004
slots: (0 slots) slave
replicates 2a122a67a56688c6d607608f80a6b9751c1b4430
S: af4328273da76a7ca27732df9f013528fea9902a 127.0.0.1:7006
slots: (0 slots) slave
replicates fc48069a547305efee0fb6ee0467d6fbc747d0f6
S: c5ae1fec5aab57337a338701cf857eb09e19b244 127.0.0.1:7005
slots: (0 slots) slave
replicates e6a12a074bb4e4160717fd6b57b8b187e687ef98
M: fc48069a547305efee0fb6ee0467d6fbc747d0f6 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: e6a12a074bb4e4160717fd6b57b8b187e687ef98 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
会自动平均分配slot,分配完slot之后,就可以开始使用了。使用redis cluster的时候,需要有专门的驱动来支持。
常用命令
1、查看集群信息
redis-cli --cluster info 127.0.0.1:7001
2、创建集群
/data/apps/redis/bin/redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1
3、检查集群
redis-cli --cluster check 127.0.0.1:7001
4、修复异常
redis-cli --cluster fix 127.0.0.1:7001
5、迁移slot
redis-cli --cluster reshard 127.0.0.1:7001
6、均衡slot分配
redis-cli --cluster reblance 127.0.0.1:7001
7、查看所有节点
redis-cli -c -p 7001 cluster nodes
8、删除节点(需要把slot都迁移走)
redis-cli --cluster del-node 127.0.0.1:7005 c5ae1fec5aab57337a338701cf857eb09e19b244
9、添加节点
redis-cli --cluster add-node 127.0.0.1:7005 127.0.0.1:7001
10、均衡slot分配,包括给新加的节点分配
redis-cli --cluster rebalance --cluster-use-empty-masters --cluster-threshold 1 127.0.0.1:7001