RedisCluster quick start notes

Posted by stickman373 on Sat, 15 Feb 2020 10:07:09 +0100

[TOC]

RedisCluster quick start notes

PS: this tutorial is based on Redis5.0+

Note for Redis cluster cluster:

  1. It can expand linearly to 1000 nodes
  2. Nodes use P2P instead of Proxy to interact, sacrificing data consistency
  3. Client tolerates a certain degree of data loss
  4. Data is stored and distributed on multiple Redis instances according to Slot
  5. Cluster node will fail over automatically when it is hung up

Need to know:

All the master nodes correspond to [0-16383] integer interval slot; each master node maintains a batch of slot numbers (0-50005001-1000010001-16383);

So if a data is saved in a 0-5000 slot and this slot is saved in node 1, then if the user has been requesting or typing this data, the performance is not as good as that of a single Redis

The overall performance of Redis cluster and single machine need to be clearly divided in order to make better use of them in the project

Common commands and configuration of cluster

Frequently used commands

# Download Redis
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz

# View process
$ ps -ef | grep redis 

# Connect to the specified node (be sure to join - c, otherwise you will not enter the cluster environment)
$ redis-cli -h 127.0.0.1 -p 7000 -c

# Create a cluster (Redis needs to be in the startup state)
# Create: create cluster
# --Cluster replicas 1 is a replica machine (backup machine). If the data redundancy is 1, at least 3 masters and 3 Slave are required, 6 in total
$ redis-cli --cluster create 127.0.0.1:7000 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 --cluster-replicas 1

# Appended node
$ redis-cli --cluster add-node 127.0.0.1:7000 127.0.0.1:7001

# View the cluster status (enter any node to bring out all node status)
$ redis-cli  --cluster check 127.0.0.1:7000

# View node
$ redis-cli cluster nodes -h 127.0.0.1 -p 7000 -c

# Delete node
$ redis-cli --cluster del-node 127.0.0.1:7000

# Shut down the node (if you shut down the redis cluster, you can't kill the process directly, otherwise you can't save the data. You have to shut down it normally.)
$ redis-cli -h 127.0.0.1 -p 7000 shutdown

# If you don't want to get mixed up, you can shut down the process like this
ps -ef | grep redis | awk '{print $2}' | xargs kill

Cluster command

Command of Redis cluster
 The following command does not need redis cli -- cluster, but redis cli cluster

colony
 cluster info: print cluster information
 cluster nodes: lists all the nodes currently known to the cluster, as well as information about these nodes.


node
 Cluster meet < ip > < port >: add the nodes specified by ip and port to the cluster and make it a part of the cluster.
Cluster forget < node? ID >: remove the node specified by node? ID from the cluster (ensure empty channel).
Cluster replicate < node ID >: sets the current node to the slave node of the node specified by node ID.
cluster saveconfig: save the configuration file of the node to the hard disk.


Trough (slot)
Cluster addslots < slot > [slot...]: assign one or more slots to the current node.
Cluster delslots <slot> [slot...]: removes the assignment of one or more slots to the current node.
cluster flushslots: remove all slots assigned to the current node, and make the current node a node without any slots assigned.
Cluster setslot < slot > node < node ID >: assign slot slot to the node specified by node ID, if slot has been assigned to
 For another node, let the other node delete the slot > before assigning it.
Cluster setslot < slot > migrating < node ID >: migrate the slot slot of this node to the node specified by node ID.
Cluster setslot < slot > importing < node ID >: imports slot slots from the node specified by node ID to this node.
Cluster setslot < slot > stable: cancel the import or migration of slot.


key
 Cluster keyslot < key >: calculates which slot the key should be placed in.
Cluster countkeysinslot < slot >: returns the number of key value pairs currently contained in slot slot.
Cluster getkeysinslot < slot > < count >: returns the keys in count slot 


Cluster configuration

# Bind the login IP address. Don't forget to log out or change it to the IP address of the current server
# After logout, external access is unavailable, so local testing is called
bind 127.0.0.1
# Turn on the security mode. Once it is yes, you must bind the IP address. If you annotate the bind above, you must also note it out here
# Otherwise, you cannot use redis-cli-h to access the node remotely
protected-mode yes
# Port number monitored
port 7000
# Running Redis as a Daemons
daemonize yes
# redis log saving location
logfile "./redis-7000.log"
# Set the number of libraries. Only one library is supported under the cluster
databases 16
# Open AOF log
appendonly yes

# Open cluster cluster
cluster-enabled yes
# Cluster node log file name
cluster-config-file nodes-7000.conf
# Cluster node synchronization timeout 5s
cluster-node-timeout 5000
# Only when the status of all nodes in the cluster is ok can the service be provided. It is recommended to set no to provide services when the slot is not fully allocated.
# The default is yes. As long as 16384 slots are not covered due to node downtime, the whole cluster will stop serving, so it must be changed to no
# When it is no, it means that the cluster is still available when the primary Library in charge of a slot is offline and there is no corresponding slave library for failure recovery. This is demonstrated below.
# If yes is selected, it means that the cluster is not available when the primary library responsible for a slot is offline and there is no corresponding slave library for failure recovery. This is demonstrated below.
cluster-require-full-coverage no
# During failover, all slaves will request to apply for master. However, some slaves may be disconnected from the master for some time, resulting in the data being too old and should not be promoted to master. This parameter is used to determine whether the disconnection time between the slave node and the master is too long. The judgment method is: compare the time when the slave is disconnected with (node timeout * slave validity factor) + repl Ping slave period. If the node timeout is 30 seconds and the slave validity factor is 10, assume that the default repl Ping slave period is 10 seconds, that is, if the time exceeds 310 seconds, the slave will not attempt to failover
cluster-slave-validity-factor 10  
# If the master's slave number is greater than this value, the slave can be migrated to other isolated masters. If this parameter is set to 2, then only when a master node has two working slave nodes, one of its slave nodes will attempt to migrate.
cluster-migration-barrier 1

Topics: Database Redis