暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Redis5 Cluster搭建及常用命令

原创 陶老师运维 2020-01-17
5063

Redis5 Cluster搭建及常用命令

文章来源: 陶老师运维笔记-微信公众号

redis

1. 介绍

Redis Cluster是Redis官方在Redis 3.0版本正式推出的高可用以及分布式的解决方案。目前redis最新版本已是5.0.x,本文以redis5.0.6版本为例来搭建redis cluster。

1.1 cluster架构

redis-cluster架构:

Redis Cluster由多个Redis实例组成的整体,数据按照槽(slot)存储分布在多个Redis实例上,通过Gossip协议来进行节点之间通信。
rediscluster.png

架构细节:

  1. 所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
  2. 节点的fail是通过集群中超过半数的节点检测失效时才生效.
  3. 客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
  4. redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value

redis-cluster选举:

redisclusterfail.jpg

  • 1)选举过程是集群中所有master参与,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉。
  • 2)什么时候整个集群不可用(cluster_state:fail)? 当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误。
    a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成进群的slot映射[0-16383]不完成时进入fail状态。
    b:如果进群超过半数以上master挂掉,无论是否有slave集群进入fail状态。

2. 机器规划

角色 版本 IP port
node-1 redis-5.0.6 192.124.64.212 6301,7301
node-2 redis-5.0.6 192.124.64.213 6301,7301
node-3 redis-5.0.6 192.124.64.214 6301,7301

说明: 计划把6301端口做为初始master端口,7301为slave端口。

3. 软件安装

本次安装Redis版本为redis-5.0.6。在node1,node2,node3分别执行如下软件安装。

$wget http://download.redis.io/releases/redis-5.0.6.tar.gz #编译安装 $tar -zxvf redis-5.0.6.tar.gz $cd redis-5.0.6 $make MALLOC=jemalloc $make test $sudo make install PREFIX=/usr/local/redis-5.0.6 $find src/ -perm 755 -print | xargs -i /bin/cp {} /usr/local/redis-5.0.6/bin/ $cp src/redis-trib.rb /usr/local/redis-5.0.6/bin/ $cp README.md redis.conf utils /usr/local/redis-5.0.6/ -R $cp runtest* sentinel.conf /usr/local/redis-5.0.6/ #也可把编译后的二进制,压缩后再传到其它机器上 tar -zcvf Redis-server-5.0.6.el6.x86_64.tar.gz redis-5.0.6

4. cluster布署

4.1 创建目录

依规划在三台机器上建立redis6301(主),redis7301(从)库的目录。

$mkdir -p /data/redis6301/{conf,data,log} $mkdir -p /data/redis7301/{conf,data,log}

4.2 修改配置

以redis6301为例,修改其配置。

$cp /usr/local/redis-5.0.6/redis.conf /data/redis6301/conf/redis-cluster-6301.conf #修改port, dir, maxmemory等参数. $vim redis-cluster-6301.conf port 6301 dir "/data1/redis6301/data" dbfilename "redis-6301.rdb" logfile "/data1/redis6301/log/redis6301.log" cluster-enabled yes //开启集群 把注释#去掉 cluster-node-timeout 15000 //请求超时 设置5秒够了 cluster-config-file "nodes-6301.conf"

修改后配置如下:

cat /data1/redis6301/conf/redis-cluster-6301.conf |grep -v '#' |grep -v '^$'
daemonize yes
tcp-backlog 511
timeout 600
tcp-keepalive 60
loglevel notice
databases 16
dir "/data1/redis6301/data"
pidfile "/data1/redis6301/data/redis6301.pid"
stop-writes-on-bgsave-error no
repl-timeout 60
repl-ping-slave-period 10
repl-disable-tcp-nodelay no
repl-backlog-size 62500kb
repl-backlog-ttl 7200
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 512mb 256mb 180
client-output-buffer-limit pubsub 32mb 8mb 60
hz 50
port 6301
bind 0.0.0.0
maxmemory 1gb
maxmemory-policy volatile-lru
save 900 1
save 300 10
save 60 10000
dbfilename "redis-6301.rdb"
appendonly no
appendfsync everysec
appendfilename "appendonly-6301.aof"
aof-rewrite-incremental-fsync yes
no-appendfsync-on-rewrite yes
auto-aof-rewrite-min-size 62500kb
auto-aof-rewrite-percentage 94
rdbcompression yes
rdbchecksum yes
repl-diskless-sync no
repl-diskless-sync-delay 5
maxclients 10000
hll-sparse-max-bytes 3000
min-slaves-to-write 0
min-slaves-max-lag 10
aof-load-truncated yes
notify-keyspace-events ""
requirepass pwd123
masterauth pwd123
logfile "/data1/redis6301/log/redis6301.log"
cluster-enabled yes
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-config-file "nodes-6301.conf"
cluster-require-full-coverage no

4.3 启动redis

分别在三台机器上启动redis6301/redis7301。

#启动6301 /usr/local/redis/bin/redis-server /data1/redis6301/conf/redis-cluster-6301.conf #启动7301 /usr/local/redis/bin/redis-server /data1/redis7301/conf/redis-cluster-7301.conf

检查服务:

$ps -ef | grep redis #查看是否启动成功 $netstat -tnlp | grep redis #可以看到redis监听端口 tcp 0 0 0.0.0.0:6301 0.0.0.0:* LISTEN 26418/redis-server tcp 0 0 0.0.0.0:7301 0.0.0.0:* LISTEN 26428/redis-server tcp 0 0 0.0.0.0:16301 0.0.0.0:* LISTEN 26418/redis-server tcp 0 0 0.0.0.0:17301 0.0.0.0:* LISTEN 26428/redis-server

4.4 创建集群

前面已经准备好了搭建集群的redis节点,现在我们要把这些节点都串连起来搭建集群。redis3.x/4.x创建集群是使用redis-trib.rb来创建,不过5.0.x的redis-cli包括了原redis-trib.rb的全部功能,可用来创建cluster。

新版本redis-cli:

#redis-trib.rb的全部功能已迁移到redis-cli $/usr/local/redis-5.0.6/bin/redis-trib.rb WARNING: redis-trib.rb is not longer available! You should use redis-cli instead. $redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS] $/usr/local/redis-5.0.6/bin/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 ... help For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

创建redis5集群示例:

Redis5.0.6带有一个可在本机快速创建redis cluster实验环境的shell脚本create-cluster,但其不支持密码等等有局限性,故咱们直接使用redis-cli --cluster create 来建redis-cluster。

#redis5本机快速建redis-cluster脚本 $./utils/create-cluster/create-cluster [start|create|stop|watch|tail|clean] #redis3.x/redis4.x ,若要支持密码,还需要去修改redis-trib.rb文件 $redis-trib.rb create --replicas 1 192.124.64.212:6301 192.124.64.213:6301 192.124.64.214:6301 192.124.64.213:7301 192.124.64.214:7301 192.124.64.212:7301 #redis5.0.6 $redis-cli -h --cluster <command> [args...] [opts...] Cluster Manager command and arguments (see below). #创建cluster, 3主3从 $redis-cli --cluster create 192.124.64.212:6301 192.124.64.213:6301 192.124.64.214:6301 192.124.64.213:7301 192.124.64.214:7301 192.124.64.212:7301 \ --cluster-replicas 1 -a pwd123 >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 192.124.64.213:7301 to 192.124.64.212:6301 Adding replica 192.124.64.214:7301 to 192.124.64.213:6301 Adding replica 192.124.64.212:7301 to 192.124.64.214:6301 M: 5e19efdd5aaed8469fa4900bbda57dd3e88991d6 192.124.64.212:6301 slots:[0-5460] (5461 slots) master M: 8021b063dd7c4b6fbcf0d883bf9b9fc460c29eac 192.124.64.213:6301 slots:[5461-10922] (5462 slots) master M: 7f4110121f1365119e5234cd4fe2a89eaf826f30 192.124.64.214:6301 slots:[10923-16383] (5461 slots) master S: b8150fd3a869f28c89c85b242ad71c3a475a249a 192.124.64.213:7301 replicates 5e19efdd5aaed8469fa4900bbda57dd3e88991d6 S: 3146f9f50294ea34a60e668c7cea823cc460cc26 192.124.64.214:7301 replicates 8021b063dd7c4b6fbcf0d883bf9b9fc460c29eac S: 4fd97d97e9c8cfca5d88b621ca9da4c0568c55f6 192.124.64.212:7301 replicates 7f4110121f1365119e5234cd4fe2a89eaf826f30 #输入yes 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 192.124.64.212:6301) M: 5e19efdd5aaed8469fa4900bbda57dd3e88991d6 192.124.64.212:6301 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 8021b063dd7c4b6fbcf0d883bf9b9fc460c29eac 192.124.64.213:6301 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 3146f9f50294ea34a60e668c7cea823cc460cc26 192.124.64.214:7301 slots: (0 slots) slave replicates 8021b063dd7c4b6fbcf0d883bf9b9fc460c29eac M: 7f4110121f1365119e5234cd4fe2a89eaf826f30 192.124.64.214:6301 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 4fd97d97e9c8cfca5d88b621ca9da4c0568c55f6 192.124.64.212:7301 slots: (0 slots) slave replicates 7f4110121f1365119e5234cd4fe2a89eaf826f30 S: b8150fd3a869f28c89c85b242ad71c3a475a249a 192.124.64.213:7301 slots: (0 slots) slave replicates 5e19efdd5aaed8469fa4900bbda57dd3e88991d6 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.

检查集群结果:

#redis5.0.x命令
$/usr/local/redis-5.0.6/bin/redis-cli --cluster info 192.124.64.212:6301 -a pwd123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.124.64.212:6301 (5e19efdd...) -> 0 keys | 5461 slots | 1 slaves.
192.124.64.213:6301 (8021b063...) -> 0 keys | 5462 slots | 1 slaves.
192.124.64.214:6301 (7f411012...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

#redis4.0.x命令
$redis-trib.rb check 192.124.64.212:6301  #要修改代码才支持密码.

如上相比较redis4.x 的redis-trib.rb,redis5 cluster info可以更方便的查看cluster信息。

5. redis 常用管理命令

5.1 读写操作

#读写操作
$redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 set foo bar
$redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 get foo
$redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 del foo

5.2 查看内存等信息

#查看内存等信息 $redis-cli -h 192.124.64.212 -p 6301 -a pwd123 info $redis-cli -h 192.124.64.212 -p 6301 -a pwd123 info memory |grep human -i $redis-cli -h 192.124.64.212 -p 6301 -a pwd123 dbsize

5.3 查看key及bigkey

#key的情况 $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 dbsize #性能bigkey $redis-cli -c --bigkeys -h 192.124.64.212 -p 6301 -a pwd123 #查看key情况 $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 keys '*' #数据量大时,有风险,该使用scan $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 scan 0 match '*' count 1000 1) "0" 2) 1) "get"

5.4 ops及client链接

#访问情况 $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 info |grep ops $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 info clients # Clients connected_clients:1 $redis-cli -c -h 192.124.64.212 -p 6301 client list -a pwd123 |awk -F'addr=' '{print $2}' | awk '{print $1}'|awk -F':' '{print $1}'|sort |uniq -c|sort -nr

5.5 统计

$redis-cli --stat  -h 192.124.64.212 -p 6301 -a pwd123  
------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections          
1          62.49M   1       0       146718 (+0)         60   
 

5.6 慢查询

#慢查询
$redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123  SLOWLOG RESET   #清空慢查询
#查看
$redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123  SLOWLOG  GET 
1) 1) (integer) 4               # 日志的唯一标识符(uid)
   2) (integer) 1578413848      # 命令执行时的 UNIX 时间戳
   3) (integer) 13              # 命令执行的时长,以微秒计算
   4) 1) "SET"                  # 命令以及命令参数
      2) "database"
      3) "Redis"

时间戳转换:

  • python转换: time.strftime(’%Y-%m-%d %H:%M:%S’,time.localtime(1578413848))
  • MySQL转换: select from_unixtime(1578413848);

5.7 配置修改

有时会需要修改配置,如最大内存大小,开aof等。

#配置查看/修改 $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 config-admroot get maxmemory $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 config-admroot set maxmemory 1073741824 #aof修改 $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 config-admroot get appendonly 1) "appendonly" 2) "no" $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 config-admroot set appendonly yes #从库只读 $redis-cli -h 192.124.64.212 -p 6301 -a pwd123 config get slave-read-only 1) "slave-read-only" 2) "yes" $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 config-admroot set slave-read-only yes

5.8 主从复制

#从库同步 $redis-cli -h redis_slave_ip -p redis_slave_port -a xxx slaveof redis_master redis_port $redis-cli -h 192.124.64.212 -p 7301 -a pwd123 slaveof 192.124.64.212 6301 #取消同步 $redis-cli -h redis_master -p redis_port slaveof NO ONE # 查看主从同步情况 $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 info Replication role:master connected_slaves:1 slave0:ip=192.124.64.213,port=7301,state=online,offset=114756,lag=0 $redis-cli -c -h 192.124.64.212 -p 7301 -a pwd123 info Replication # Replication role:slave master_host:192.124.64.214 master_port:6301 master_link_status:up master_last_io_seconds_ago:1

6 redis cluster 常用命令

Redis4.x上面的cluster命令基本上都仍可以在redis5.0.x正常使用。 但是redis5.0.x上redis-trib.rb工具不再可用,redis-trib.rb相关的全部功能,已迁移到redis-cli --cluster命令上了。

6.1 cluster命令语法

$redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS] $/usr/local/redis-5.0.6/bin/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

子命令说明:

  • create:创建集群
  • check:检查集群
  • info:查看集群信息
  • fix:修复集群
  • reshard:在线迁移slot
  • rebalance:平衡集群节点slot数量
  • add-node:将新节点加入集群
  • del-node:从集群中删除节点
  • set-timeout:设置集群节点间心跳连接的超时时间
  • call:在集群全部节点上执行命令
  • import:将外部redis数据导入集群

6.2 cluster基本信息

基础信息查看, CLUSTER INFO ,CLUSTER NODES等。

CLUSTER INFO # 打印集群的信息 CLUSTER NODES # 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 #示例: $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 cluster nodes |grep master |sort -k 9n 5e19efdd5aaed8469fa4900bbda57dd3e88991d6 192.124.64.212:6301@16301 myself,master - 0 15791922727000 1 connected 0-5460 8021b063dd7c4b6fbcf0d883bf9b9fc460c29eac 192.124.64.213:6301@16301 master - 0 15791922728623 2 connected 5461-192922 7f41192121f1365119e5234cd4fe2a89eaf826f30 192.124.64.214:6301@16301 master - 0 15791922728000 3 connected 192923-16383 $redis-cli --cluster info 192.124.64.212:6301 -a pwd123 192.124.64.212:6301 (5e19efdd...) -> 1 keys | 5461 slots | 1 slaves. 192.124.64.213:6301 (8021b063...) -> 0 keys | 5462 slots | 1 slaves. 192.124.64.214:6301 (7f4119212...) -> 0 keys | 5461 slots | 1 slaves. [OK] 1 keys in 3 masters. $redis-cli --cluster check 192.124.64.212:6301 -a pwd123

6.3 批量处理

redis-cli –cluster call 可对集群中全部redis节点批量执行命令,非常好用。

$redis-cli --cluster call host:port command arg arg .. arg #示例: $redis-cli --cluster info 192.124.64.212:6301 -a pwd123 $redis-cli --cluster call 192.124.64.212:6301 info -a pwd123 |grep ops $redis-cli --cluster call 192.124.64.212:6301 info memory -a pwd123 |egrep 'Memory|human' $redis-cli --cluster call 192.124.64.212:6301 info Keyspace -a pwd123 $redis-cli --cluster call 192.124.64.212:6301 dbsize -a pwd123 $redis-cli --cluster call 192.124.64.212:6301 info clients -a pwd123 | grep Clients -i $redis-cli --cluster call 192.124.64.212:6301 client list -a pwd123

6.4 节点管理

CLUSTER MEET <ip> <port> # 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。 CLUSTER FORGET <node_id> # 从集群中移除 node_id 指定的节点。 CLUSTER REPLICATE <node_id> # 将当前节点设置为 node_id 指定的节点的从节点。 CLUSTER SAVECONFIG # 将节点的配置文件保存到硬盘里面。 CLUSTER NODES # 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 CLUSTER SLAVES node-id # 返回一个master节点的slaves 列表 #cluster forget 命令需要在集群内所有节点执行 $redis-cli --cluster call 192.124.64.212:6301 cluster FORGET e4adb3835xx -a pwd123

集群扩容redis节点:

  • 扩容master: 把新节点加入集群,reshard 迁移slot到新节点,reblance集群。
  • 扩容slave: 需要指定–cluster-master-id ,省去了迁移slot的步骤。
add-node     new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
del-node       host:port node_id
#节点查看
$redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123  cluster nodes    

#添加master节点
$redis-cli --cluster add-node   new_host:new_port existing_host:existing_port
$redis-cli --cluster add-node 192.124.64.212:6543 192.124.64.212:6301 -a pwd123

#分配slot
$redis-cli --cluster info  192.124.64.212:6301 -a pwd123
$redis-cli --cluster check  192.124.64.212:6301 -a pwd123    
#reshard ,迁移slot
redis-cli --cluster reshard  192.124.64.212:7543 --cluster-from 7f4110121f1365119e5234cd4fe2a89eaf826f30 --cluster-to 5c524853ad5995cd30cad27adce042c9d1ad55ce --cluster-slots 4 -a pwd123    

#rebalance
$redis-cli --cluster rebalance 192.124.64.212:6301 -a pwd123  

#添加slave节点,--slave 表示是添加从库
$redis-cli --cluster  add-node --cluster-slave --cluster-master-id xxx new_host:new_port existing_host:existing_port 
$redis-cli --cluster  add-node   192.124.64.212:6543 192.124.64.212:6301 --cluster-slave --cluster-master-id 5e19efdd5aaed8xx -a pwd123 
>>> Send CLUSTER MEET to node 192.124.64.212:6543 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.124.64.212:6301.
[OK] New node added correctly.

#检查
$redis-cli --cluster info 192.124.64.212:6301 -a pwd123 
$redis-cli --cluster check 192.124.64.212:6301 -a pwd123

删除节点:

  • 删除主库节点: 只能删除没有分配slot的节点,故先移除删除节点的slot,然后del-node。
  • 删除从库节点: 直接用–cluster del-node 命令删除即可。
#删除节点 #redis-cli --cluster del-node host:port node_id $redis-cli --cluster del-node host:port node_id $redis-cli -a pwd123 --cluster del-node 192.124.64.212:7543 5c524853ad5995cd30cad27adce042c9d1ad55ce >>> Removing node 5c524853ad5995cd30cad27adce042c9d1ad55ce from cluster 192.124.64.212:7543 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. #若是删除失败可能要用到forget命令 $redis-cli --cluster call 192.124.64.212:6301 cluster FORGET 5c524853ad59xx -a pwd123

6.5 SLOT处理

CLUSTER KEYSLOT <key>   # 计算键 key 应该被放置在哪个槽上。
CLUSTER COUNTKEYSINSLOT <slot>   # 返回槽 slot 目前包含的键值对数量。
CLUSTER GETKEYSINSLOT <slot> <count>   # 返回 count 个 slot 槽中的键。
#
CLUSTER ADDSLOTS <slot> [slot ...]   # 将一个或多个槽(slot)指派(assign)给当前节点。
CLUSTER DELSLOTS <slot> [slot ...]   # 移除一个或多个槽对当前节点的指派。
CLUSTER FLUSHSLOTS   # 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
CLUSTER SETSLOT <slot> NODE <node_id>   # 将槽 slot 指派给 node_id 指定的节点。
CLUSTER SETSLOT <slot> MIGRATING <node_id>   # 将本节点的槽 slot 迁移到 node_id 指定的节点中。
CLUSTER SETSLOT <slot> IMPORTING <node_id>   # 从 node_id 指定的节点中导入槽 slot 到本节点。
CLUSTER SETSLOT <slot> STABLE   # 取消对槽 slot 的导入(import)或者迁移(migrate)。

slot操作示例:

#计算 $redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 CLUSTER KEYSLOT foo (integer) 12182 #检查slot $redis-cli --cluster info 192.124.64.212:6301 -a pwd123 $redis-cli --cluster check 192.124.64.212:6301 -a pwd123 #迁移slot $redis-cli --cluster reshard 192.124.64.212:7543 --cluster-from 5c524853ad5995cd30cad27adce042c9d1ad55ce --cluster-to 7f4110121f1365119e5234cd4fe2a89eaf826f30 --cluster-slots 4 -a pwd123 #增加/删除槽 redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 CLUSTER ADDSLOTS 16383 redis-cli -c -h 192.124.64.212 -p 6301 -a pwd123 CLUSTER DELSLOTS 16383

5.7 Reshard及Rebalance

虽然reshard和rebalance也是slot操作相关,但因为工作常会使用并且命令参数较多,故单独列出介绍。

Reshard :

手动迁移slot很烦琐,容易出错,所以redis-cli提供了reshard 子命令来支持在线迁移slot。

reshard host:port --cluster-from <arg> --cluster-to <arg> --cluster-slots <arg> --cluster-yes --cluster-timeout <arg> --cluster-pipeline <arg> --cluster-replace 参数说明: host:port:必传参数,集群内任意节点地址,用来获取整个集群信息。 --cluster-from:制定源节点的id,如果有多个源节点,使用逗号分隔,如果是all源节点变为集群内所有主节点,在迁移过程中提示用户输入。 --cluster-to:需要迁移的目标节点的id,目标节点只能填写一个,在迁移过程中提示用户输入。 --cluster-slots:需要迁移槽的总数量,在迁移过程中提示用户输入。 --cluster-yes:当打印出reshard执行计划时,是否需要用户输入yes确认后再执行reshard。 --cluster-timeout:控制每次migrate操作的超时时间,默认为60000毫秒。 --cluster-pipeline:控制每次批量迁移键的数量,默认为10。 #示例 $redis-cli --cluster reshard host:port $redis-cli --cluster reshard 192.124.64.212:6301 --cluster-from 5c524853ad5995cd30cad27adce042c9d1ad55ce --cluster-to 7f4110121f1365119e5234cd4fe2a89eaf826f30 --cluster-slots 4 -a pwd123

Rebalance :

若各节点slot数量不均衡时,可以用rebalance命令来平衡各集群节点slot数量。

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 host:port:这个是必传参数,用来从一个节点获取整个集群信息,相当于获取集群信息的入口。 --cluster-weight <arg>:节点的权重,格式为node_id=weight,例如--weight b31e3a2e=5 node_id可为节点名称的前缀。没有传递–weight的节点的权重默认为1。 --cluster-threshold <arg>:只有节点需要迁移的slot阈值超过threshold,才会执行rebalance操作。 --cluster-use-empty-masters:rebalance是否考虑没有节点的master,默认没有分配slot节点的master是不参与rebalance的,设置--use-empty-masters可以让没有分配slot的节点参与rebalance。 --cluster-timeout <arg>:设置migrate命令的超时时间。 --cluster-simulate:设置该参数,可以模拟rebalance操作,提示用户会迁移哪些slots,而不会真正执行迁移操作。 --cluster-pipeline <arg>:与reshar的pipeline参数一样,定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10。 #示例 #rebalance redis-cli --cluster rebalance host:port $redis-cli --cluster rebalance 192.124.64.212:6301 -a pwd123 $redis-cli --cluster rebalance 192.124.64.212:6301 -a pwd123 --cluster-threshold 1 --cluster-use-empty-masters --pipeline 10

参考:


微信公众号_陶老师运维

最后修改时间:2020-01-19 10:22:24
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论