Redis5 Cluster setting and common commands
Abstract: This article introduces the steps of Redis5 Cluster construction in detail, and the common Redis command /Redis Cluster management command to demonstrate.
Article source: Tao Teacher operation notes – wechat official account
1. Introduction
Redis Cluster is a highly available and distributed solution released by Redis 3.0. At present, the latest version of Redis is5.0. x, this article takes Redis5.0.6 version as an example to build redis Cluster.
- IO /topics/clus…
- Redis. IO /documentati…
1.1 cluster architecture
Redis – cluster architecture:
A Redis Cluster consists of multiple Redis instances. Data is stored in slots on multiple Redis instances and communicates between nodes through the Gossip protocol.
Architectural details:
- All redis nodes are ping-pong with each other and use binary protocols internally to optimize transmission speed and bandwidth.
- Nodes fail only when more than half of the nodes in the cluster have detected failure.
- The client is directly connected to the Redis node without the intermediate proxy layer. The client does not need to connect to all nodes in the cluster, but to any available node in the cluster
- Redis-cluster maps all physical nodes to slot [0-16383]. The cluster maintains node<->slot<->value
Redis – cluster election:
- 1) All the master nodes in the cluster participate in the election process. If more than half of the master nodes communicate with the master node more than (cluster-node-timeout), the current master node is considered to be down.
- 2) When does the entire cluster become unavailable (Cluster_state :fail)? When The cluster is unavailable, all operations on The cluster are unavailable, and The (error) CLUSTERDOWN The cluster is down error is received. A: If any master of the cluster fails and the current master has no slave. The cluster enters the Fail state, which can also be interpreted as the incomplete slot mapping [0-16383] of the integrated group enters the Fail state. B: If more than half of the slave clusters fail, no matter whether any slave cluster enters the Fail state.
2. Machine planning
role | version | 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 |
Note: Port 6301 is planned to be the initial master port and port 7301 is planned to be the slave port.
3. Software installation
- Download address: download. Redis. IO/releases /
The Redis version is Redis-5.0.6. Install the following software on node1, Node2, and Node3.
$wget http://download.redis.io/releases/redis-5.0.6.tar.gz
# build install
$tar- ZXVF redis - 5.0.6. Tar. Gz$cdRedis - 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 /The compiled binary can also be compressed and then transferred to other machinesThe tar - ZCVF Redis - server - 5.0.6. El6. X86_64. Tar. Gz Redis - 5.0.6Copy the code
4. Cluster deployment
4.1 Creating a Directory
The directory of reDIS6301 (master) and ReDIS7301 (slave) libraries is set up on the three machines according to the plan.
$mkdir -p /data/redis6301/{conf,data,log}
$mkdir -p /data/redis7301/{conf,data,log}
Copy the code
4.2 Modifying the Configuration
- Redis. IO/switchable viewer/conf…
- Raw.githubusercontent.com/antirez/red…
The following uses Redis6301 as an example to modify its configuration.
$cp /usr/local/ redis - 5.0.6 / redis. Conf/data/redis6301 / conf/redis - cluster - 6301. Conf# modify port, dir, maxMemory, etc.
$vim redis-cluster-6301.conf
port 6301
dir "/data1/redis6301/data"
dbfilename "redis-6301.rdb"
logfile "/data1/redis6301/log/redis6301.log"Cluster-enabled yes // Enable cluster annotation# removeCluster-node-timeout 15000 // Request timeout set 5 seconds enough cluster-config-file"nodes-6301.conf"
Copy the code
After modification, the configuration is as follows:
cat /data1/redis6301/conf/redis-cluster-6301.conf |grep -v The '#' |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
bind0.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
Copy the code
4.3 start the redis
Start REDIS6301 / reDIS7301 on each of the three machines.
# start 6301
/usr/local/redis/bin/redis-server /data1/redis6301/conf/redis-cluster-6301.conf
# start 7301
/usr/local/redis/bin/redis-server /data1/redis7301/conf/redis-cluster-7301.conf
Copy the code
Inspection Service:
$ps -ef | grep redis # Check whether the startup is successful
$netstat -tnlp | grep redis You can see the Redis listening portTCP 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-serverCopy the code
4.4 Creating a Cluster
Now that we have the Redis nodes to build the cluster, we are going to string them together to build the cluster. Redis3.x /4.x creates clusters using redis-trib.rb, but the redis-cli of 5.0.x includes all the features of the original redis-trib.rb, which can be used to create clusters.
A new version of redis – the cli:
All functionality of #redis-trib.rb has been migrated to 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 - clusterhelp
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.
Copy the code
Example for creating a redis5 cluster:
Redis5.0.6 comes with a shell script create-cluster that can quickly create a Redis cluster environment on the machine, but it does not support passwords and other limitations. So let’s use redis-cli –cluster create to build redis-cluster.
#redis5 Native quick build redis-cluster script
$./utils/create-cluster/create-cluster [start|create|stop|watch|tail|clean]
#redis3.x/redis4.x, to support the password, also need to modify the redis-trib.rb file
$redis- TB. Rb create --replicas 1 192.124.64.212:6301 192.124.64.213:6301 192.124.64.214:6301 192.124.64.213:6301 192.124.64.213:7301 192.124.64.212 192.124.64.214:7301:7301# redis5.0.6
$redis-cli -h
--cluster <command> [args...] [opts...] Cluster Managercommand and arguments (see below).
Create a cluster, 3 primary and 3 secondary
$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 hashslots 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: 5 e19efdd5aaed8469fa4900bbda57dd3e88991d6 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 e19efdd5aaed8469fa4900bbda57dd3e88991d6 S: 3146 f9f50294ea34a60e668c7cea823cc460cc26 192.124.64.214:7301 8021 b063dd7c4b6fbcf0d883bf9b9 replicatesfc460 c29eac S: 4 fd97d97e9c8cfca5d88b621ca9da4c0568c55f6 192.124.64.212:7301 7 f4110121f1365119e5234 replicatescd4fe2a89eaf826f30
# enter 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 forthe cluster to join .... >>> Performing Cluster Check (using node 192.124.64.212:6301) M: 5 e19efdd5aaed8469fa4900bbda57dd3e88991d6 192.124.64.212:6301 slots: [0-5460] (5461 slots) master 1 additional up (s) M: 8021b063dd7c4b6fbcf0d883bf9b9fc460C29EAC 192.124.64.213:6301 slots:[5461-10922] (5462 slots) Master 1 Additional replica(s) S: 3146 f9f50294ea34a60e668c7cea823cc460cc26 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: 4 fd97d97e9c8cfca5d88b621ca9da4c0568c55f6 192.124.64.212:7301 slots: (0 slots) slave 7 f4110121f1365119e5234 replicatescd4 fe2a89eaf826f30 S: b8150fd3a869f28c89c85b242ad71c3a475a249a 192.124.64.213:7301 slots: (0 slots) slave replicates 5e19efdd5aaed8469fa4900bbda57dd3e88991d6 [OK] All nodes agree about slots configuration. >>> Checkfor open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Copy the code
Check the cluster result:
# redis5.0. X command
$/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 commandLine interface may not be safe. 192.124.64.212:6301 (5e19EFDD...) - > 0 keys 5461 slots | | 1 slaves. 192.124.64.213:6301 (8021 b063...). - > 0 keys 5462 slots | | 1 slaves. 192.124.64.214:6301 (7 f411012...). -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keysin 3 masters.
0.00 keys per slot on average.
# redis4.0. X command
$redis- trib. Rb check 192.124.64.212:6301Change the code to support passwords.
Copy the code
Compared with redis4.x’s redis-trib.rb, redis5 cluster info can be used to view cluster information more conveniently.
5. Redis common management commands
- Redis. IO/switchable viewer/redi…
5.1 Read and Write Operations
Read/write
$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
Copy the code
5.2 Viewing Memory Information
# Check memory information
$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
Copy the code
5.3 Viewing Keys and BigKeys
The situation of the # key
$redis- CLI -c -h 192.124.64.212 -p 6301-a pwd123 dbsize
# performance bigkey
$redis-cli -c --bigkeys -h 192.124.64.212 -p 6301 -a pwd123
# check the key
$redis- CLI -c -h 192.124.64.212 -p 6301-a pwd123 keys The '*' # Scan is recommended for high data volume risks
$redis- CLI -c -h 192.124.64.212 -p 6301-a pwd123 scan 0 match The '*' count 1000
1) "0"
2) 1) "get"
Copy the code
5.4 OPS and Client Links
# Access information
$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
Copy the code
5.5 statistical
$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
Copy the code
5.6 the slow query
# slow query
$redis- CLI -c -h 192.124.64.212 -p 6301-a pwd123 SLOWLOG RESET # Empty slow queries
# check
$redis- CLI -c -h 192.124.64.212 -p 6301-a pwd123 SLOWLOG GET
1) 1) (integer4)# log unique identifier (UID)(2)integer) 1578413848 The UNIX timestamp at which the command is executed(3)integer13)# Command execution duration in microseconds
4) 1) "SET" # command and command parameters
2) "database"
3) "Redis"
Copy the code
Timestamp conversion:
- Strftime (‘%Y-%m-%d %H:% m :%S’,time.localtime(1578413848))
- Select from_unixtime(1578413848);
5.7 Configuration Modification
Sometimes you need to modify the configuration, such as maximum memory size, aOF, etc.
# Config View/modify
$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 modify
$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
The secondary library is read-only
$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
Copy the code
5.8 Primary/Secondary Replication
Sync from the library
$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
# cancel sync
$redis-cli -h redis_master -p redis_port slaveof NO ONE
# Check the master/slave synchronization
$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
# ReplicationRole :slave master_host:192.124.64.214 master_port:6301 master_link_status:up master_last_io_SECONds_ago :1Copy the code
6 Redis cluster common commands
The cluster command on redis4. x will still work normally on redis5.0.x. However, the redis5.0.x redis-trib.rb tool is no longer available, and all functions related to redis-trib.rb have been migrated to the redis-cli –cluster command.
6.1 Syntax of the cluster command
$redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
$/usr/local/ redis - 5.0.6 / bin/redis - cli - clusterhelpCluster 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:portcommand arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help
Copy the code
Subcommand description:
- Create: Creates a cluster
- Check: Check the cluster
- Info: Displays cluster information
- Fix: restores the cluster
- Reshard: online migration slot
- Rebalance: Balances the number of slots on cluster nodes
- Add-node: Adds a new node to a cluster
- Del-node: deletes a node from a cluster
- Set-timeout: sets the timeout period for the heartbeat connection between cluster nodes
- Call: Run the command on all nodes in the cluster
- Import: Import external Redis data into the cluster
6.2 Cluster Basic Information
View basic information, such as CLUSTER INFO and CLUSTER NODES.
CLUSTER INFO Print cluster information
CLUSTER NODES # list all known nodes in the cluster and their information.
# sample:
$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 9 n 5 e19efdd5aaed8469fa4900bbda57dd3e88991d6 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- the cli - cluster info 192.124.64.212:6301-a pwd123, 6301:192.124.64.212 (5 e19efdd...). - > 1 keys 5461 slots | | 1 slaves. 192.124.64.213:6301 (8021 b063...). - > 0 keys 5462 slots | | 1 slaves. 192.124.64.214:6301 (7 f4119212...). -> 0 keys | 5461 slots | 1 slaves. [OK] 1 keysin 3 masters.
$redis- the cli - cluster check 192.124.64.212:6301-a pwd123
Copy the code
6.3 Batch Processing
Redis -cli –cluster call can be used to run commands on all redis nodes in a cluster.
$redis-cli --cluster call host:port command arg arg .. arg
# sample:
$redis- the 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
Copy the code
6.4 Node Management
CLUSTER MEET <ip> <port> Add the node specified by IP and port to the cluster and make it a part of the cluster.
CLUSTER FORGET <node_id> # remove the node with node_id from the cluster.
CLUSTER REPLICATE <node_id> Set the current node to the secondary node of the node specified node_id.
CLUSTER SAVECONFIG Save the node configuration file to the hard disk.
CLUSTER NODES # list all known nodes in the cluster and their information.
CLUSTER SLAVES node-id # Return a list of Slaves for the master node
The #cluster forget command must be executed on all nodes in the cluster
$redis- CLI --cluster call 192.124.64.212:6301 Cluster FORGET e4ADB3835XX-a pwd123
Copy the code
Cluster expansion Redis node:
- Expand the master node: Add the new node to the cluster, reshard migrates slot to the new node, and reblance cluster.
- Capacity expansion Slave: specifies –cluster-master-id, eliminating slot migration.
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
# node view
$redis- CLI -c -h 192.124.64.212 -p 6301-a pwd123 cluster nodes
Add the master node
$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
# assigned slot
$redis- the cli - cluster info 192.124.64.212:6301-a pwd123
$redis- the cli - cluster check 192.124.64.212:6301-a pwd123
#reshard, migrate slotRedis -cli --cluster reshard 192.124.64.212:7543 --cluster-from 7f4110121f1365119e5234cd4fe2a89eaf826f30 --cluster-to 5c524853ad5995cd30cad27adce042c9d1ad55ce --cluster-slots 4 -a pwd123
#rebalance
$redis- the cli - cluster rebalance 192.124.64.212:6301-a pwd123
# add slave node --slave indicates to add slave library
$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.waitingforThe cluster to join >>> Configure node as replica of 192.124.64.212:6301. [OK] New node added correctly.# check
$redis- the cli - cluster info 192.124.64.212:6301-a pwd123
$redis- the cli - cluster check 192.124.64.212:6301-a pwd123
Copy the code
Deleting a node:
- Delete primary library node: Only nodes with no slot assigned to them can be deleted. Therefore, remove the slot of the deleted node first and then del-node.
- To delete a secondary library node, run the –cluster del-node command.
# delete 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.
The forget command will be used if the delete fails
$redis- CLI --cluster call 192.124.64.212:6301 Cluster FORGET 5C524853AD59xx-a pwd123
Copy the code
6.5 SLOT processing
CLUSTER KEYSLOT <key> Calculate on which slot the key should be placed.
CLUSTER COUNTKEYSINSLOT <slot> # return the number of key-value pairs that slot currently contains.
CLUSTER GETKEYSINSLOT <slot> <count> # return keys in count slots.
#
CLUSTER ADDSLOTS <slot> [slot ...] Assign one or more slots to the current node.
CLUSTER DELSLOTS <slot> [slot ...] Remove one or more slots assigned to the current node.
CLUSTER FLUSHSLOTS # remove all slots assigned to the current node, making the current node a node with no slots assigned to it.
CLUSTER SETSLOT <slot> NODE <node_id> # assign slots to nodes specified by node_id.
CLUSTER SETSLOT <slot> MIGRATING <node_id> Migrate the slot of this node to the node whose node_id is specified.
CLUSTER SETSLOT <slot> IMPORTING <node_id> # import slot from node_id to this node.
CLUSTER SETSLOT <slot> STABLE Remove the import or migrate of slots.
Copy the code
Slot operation examples:
# calculation
$redis- CLI -c -h 192.124.64.212 -p 6301-a pwd123 CLUSTER KEYSLOT foo
(integer) 12182
# check slot.
$redis- the cli - cluster info 192.124.64.212:6301-a pwd123
$redis- the cli - cluster check 192.124.64.212:6301-a pwd123
# migration slot
$redis-cli --cluster reshard 192.124.64.212:7543 --cluster-from 5c524853ad5995cd30cad27adce042c9d1ad55ce --cluster-to 7f4110121f1365119e5234cd4fe2a89eaf826f30 --cluster-slots 4 -a pwd123
Add/remove slotsRedis -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
Copy the code
5.7 Reshard and Rebalance
Reshard and rebalance are also related to slot operations, but because they are often used and the commands have too many parameters, they are described separately.
Reshard :
Manual slot migration is cumbersome and error-prone, so redis-CLI provides the reshard subcommand to support online slot migration.
reshard host:port --cluster-from <arg> --cluster-to <arg> --cluster-slots <arg> --cluster-yes --cluster-timeout <arg> --cluster-pipeline <arg> --cluster-replace Parameter description: host: port: specifies the address of any node in a cluster, which is used to obtain cluster information. --cluster-from: Specifies the ID of the source node. If there are multiple source nodes, separate them using commas (,). If it is all, the source node becomes all primary nodes in the cluster, and prompts users to enter the id during migration. --cluster-to: specifies the ID of a destination node to be migrated. You can enter only one destination node id and be prompted to enter it during migration. --cluster-slots: Indicates the total number of slots to be migrated. --cluster-yes: specifies whether to enter yes before reshard execution plans are printed. --cluster-timeout: Controls the timeout duration of each Migrate operation. The default value is 60000 milliseconds. --cluster-pipeline: controls the number of keys migrated per batch. Default is 10.# sample
$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
Copy the code
Rebalance :
If the number of slots on nodes is unbalanced, you can use the rebalance command to balance the number of slots on nodes in the cluster.
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: This parameter is mandatory and is used to obtain cluster information from a node. --cluster-weight <arg> : specifies the node weight. The format is node_id=weight, for example, --weight b31e3a2e=5 node_id can be a node name prefix. Nodes that do not pass -- weight default to 1. --cluster-threshold <arg> : Perform rebalance only when the slot threshold exceeds the threshold. - cluster - use - empty - masters: Rebalance --use-empty-masters This rebalance is used to rebalance a node that has no slot assigned to it. --cluster-timeout <arg> : Set the timeout duration of the migrate command. --cluster-simulate: Set this parameter to simulate the rebalance operation and prompt users which slots will be migrated without actually performing the migration. --cluster-pipeline <arg> : Same as reshar pipeline parameters, define cluster getKeysinslot command to fetch the number of keys at a time, if not the default value is 10.# sample
#rebalance
redis-cli --cluster rebalance host:port
$redis- the cli - cluster rebalance 192.124.64.212:6301-a pwd123
$redis- the cli - cluster rebalance 192.124.64.212:6301-a pwd123 --cluster-threshold 1 --cluster-use-empty-masters --pipeline 10
Copy the code
Reference:
- Redis. IO/documentati…
- Redis. IO/switchable viewer/clus…
- Redis. IO/switchable viewer/redi…
- www.cnblogs.com/nanlinghan/…