Redis cluster details

Time:2020-10-11

Redis cluster details

Redis has three cluster modes, namely:

*Master slave mode

*Sentinel mode

*Cluster mode

Each of the three cluster modes has its own characteristics. For the introduction of redis, you can refer to NoSQL (2) – redis

Redis official website: https://redis.io/ , latest version 6.0.5

Master slave mode

Introduction of master slave mode
Master slave mode is the simplest of the three modes. In master-slave replication, databases are divided into two types: master and slave.

Master slave replication has the following characteristics:

*The master database can perform read-write operations. When the read-write operations cause data changes, the data will be synchronized to the slave database automatically

*The slave database is generally read-only and receives the data synchronized from the master database

*A master can have multiple slaves, but a slave can only correspond to one master

*If the slave is hung, it will not affect the read and write of other slave and master. After restarting, the data will be synchronized from the master

*After the master is hung up, the slave reading will not be affected. However, redis will no longer provide write service. After the master is restarted, redis will provide the external write service again

*After the master is hung, a master will not be selected again in the slave node

Working mechanism:

When the slave is started, it sends the sync command to the master. After receiving the sync command, the master saves the snapshot in the background (RDB persistence) and cache the command to save the snapshot, and then sends the saved snapshot file and cached command to the slave. After receiving the snapshot file and command, slave loads the snapshot file and cache execution command.

After the replication is initialized, each write command received by the master will be sent to the slave synchronously to ensure the consistency of the master-slave data.

Security settings:

After the master node sets the password,

The client needs a password to access the master

You need a password to start the slave. You can configure it in the configuration file

The client does not need a password to access the slave

Disadvantages:

It can be seen from the above that the master node is unique in the master-slave mode. If the master fails, redis cannot provide external write services.

Construction of master-slave mode

Environmental preparation:

Master node 192.168.30.128

Slave node 192.168.30.129

Slave node 192.168.30.130

Download and install all:


# cd /software
# wget http://download.redis.io/releases/redis-6.0.5.tar.gz
# tar zxf redis-5.0.4.tar.gz && mv redis-6.0.5/ /usr/local/redis
# cd /usr/local/redis && make && make install
# echo $?
0

All configured as services:

Service Documents

# vim /usr/lib/systemd/system/redis.service

[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf –supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

Shutdown script


# vim /usr/libexec/redis-shutdown

#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x

REDIS_CLI=/usr/local/bin/redis-cli

# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
  SERVICE_NAME=redis
fi

# Get the proper config file based on service name
CONFIG_FILE="/usr/local/redis/$SERVICE_NAME.conf"

# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`

# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
  PORT=${PORT:-6379}
else
  PORT=${PORT:-26739}
fi

# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"

# shutdown the service properly
if [ -e "$SOCK" ] ; then
    $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
    $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
# chmod +x /usr/libexec/redis-shutdown
# useradd -s /sbin/nologin redis
# chown -R redis:redis /usr/local/redis
# chown -R reids:redis /data/redis
#Yum install - y bash completion & source / etc / profile ා command completion

# systemctl daemon-reload
# systemctl enable redis

Modify configuration:

192.168.30.128

# mkdir -p /data/redis

# vim /usr/local/redis/redis.conf

Bind 192.168.30.128 ා listen to IP, and multiple IPS are separated by spaces
Daemonize yes ා allow background start
logfile "/usr/local/redis/ redis.log Log path
Dir / data / redis ා database backup file storage directory
Master 123456 ා slave connect master password, master can be omitted
Require pass 123456? Set the master connection password, which can be omitted

Appendonly yes ා generated in / data / redis / directory appendonly.aof File to append each write request to the appendonly.aof  In the file

# echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf

# sysctl -p

192.168.30.129


# mkdir -p /data/redis

# vim /usr/local/redis/redis.conf

bind 192.168.30.129
daemonize yes
logfile "/usr/local/redis/redis.log"
dir /data/redis
replicaof 192.168.30.128 6379
masterauth 123456
requirepass 123456
appendonly yes

# echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf

# sysctl -p

192.168.30.130


# mkdir -p /data/redis

# vim /usr/local/redis/redis.conf

bind 192.168.30.130
daemonize yes
logfile "/usr/local/redis/redis.log"
dir /data/redis
replicaof 192.168.30.128 6379
masterauth 123456
requirepass 123456
appendonly yes

# echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf

# sysctl -p

Start all redis:

# systemctl start redis

To view the cluster status:


# redis-cli -h 192.168.30.128 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.30.128:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.30.129,port=6379,state=online,offset=168,lag=1
slave1:ip=192.168.30.130,port=6379,state=online,offset=168,lag=1
master_replid:fb4941e02d5032ad74c6e2383211fc58963dbe90
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:168
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:168

# redis-cli -h 192.168.30.129 -a 123456 info replication

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.30.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:196
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:fb4941e02d5032ad74c6e2383211fc58963dbe90
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196

Data demonstration:


192.168.30.128:6379> keys *
(empty list or set)

192.168.30.128:6379> set key1 100
OK

192.168.30.128:6379> set key2 lzx
OK

192.168.30.128:6379> keys *
1) "key1"
2) "key2"

# redis-cli -h 192.168.30.129 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.30.129:6379> keys *
1) "key2"
2) "key1"

192.168.30.129:6379> CONFIG GET dir
1) "dir"
2) "/data/redis"

192.168.30.129:6379> CONFIG GET dbfilename
1) "dbfilename"
2) "dump.rdb"

192.168.30.129:6379> get key1
"100"

192.168.30.129:6379> get key2
"lzx"

192.168.30.129:6379> set key3 aaa
(error) READONLY You can't write against a read only replica.

# redis-cli -h 192.168.30.130 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.30.130:6379> keys *
1) "key2"
2) "key1"

192.168.30.130:6379> CONFIG GET dir
1) "dir"
2) "/data/redis"

192.168.30.130:6379> CONFIG GET dbfilename
1) "dbfilename"
2) "dump.rdb"

192.168.30.130:6379> get key1
"100"

192.168.30.130:6379> get key2
"lzx"

192.168.30.130:6379> set key3 aaa
(error) READONLY You can't write against a read only replica.

As you can see, the data written in the master node is quickly synchronized to the slave node, and the data cannot be written to the slave node.

Sentinel mode

Sentinel mode introduction
The disadvantage of the master-slave mode is that it does not have high availability. When the master fails, redis will no longer be able to provide external write operations, so sentinel came into being.

Sentinel means sentinel in Chinese. As the name implies, its function is to monitor the operation of redis cluster. Its features are as follows:

*Sentinel mode is based on master-slave mode. If there is only one redis node, sentinel has no meaning

*When the master is hung, sentinel will select one of the slave as the master and modify their configuration files. Other slave configuration files will also be modified. For example, the slaveof attribute will point to the new master

*When the master is restarted, it will no longer be the master, but as a slave to receive the synchronization data of the new master

*Sentinel is also a process that may hang up. Therefore, sentinel will start several processes to form a sentinel cluster

*When configuring multiple sentinels, sentinels will also be monitored automatically

*When the master-slave mode configures the password, sentinel will also synchronously modify the configuration information to the configuration file, so there is no need to worry

*A sentinel or sentinel cluster can manage multiple master-slave redis, and multiple sentinels can also monitor the same redis

*Sentinel is better not to be deployed on the same machine as redis, otherwise, when the server of redis is hung up, sentinel will also hang up

Working mechanism:

*Each sentinel sends a ping command to its known master, slave, and other sentinel instances at a frequency of once per second

*If an instance takes more than the value specified by the down after milliseconds option until the last valid reply to ping command, the instance is marked as subjective offline by sentinel.

*If a master is marked as a subjective offline, all sentinels monitoring the master should confirm that the master has entered the subjective offline state at the frequency of once per second

*When a sufficient number of sentinels (greater than or equal to the value specified in the configuration file) confirm that the master has entered the subjective offline state within the specified time range, the master will be marked as objective offline

*In general, each sentinel will send the info command to all the masters and slaves it knows every 10 seconds

*When the master is marked as objective offline by sentinel, the frequency of sending info command from sentinel to all slave of offline master will be changed from once every 10 seconds to once every 1 second

*If there is not enough sentinel to agree that the master has been offline, the objective offline status of the master will be removed;
If the master returns a valid reply to the sentinel ping command again, the master’s subjective offline status will be removed

When using sentinel mode, the client does not directly connect to redis, but connects to the IP and port of sentinel. Sentinel provides the specific implementation of redis that can provide services. In this way, when the master node is down, sentinel will perceive and provide the new master node to the user.

Sentinel mode construction

Environmental preparation:

Master node 192.168.30.128 sentinel port: 26379

Slave node 192.168.30.129 sentinel port: 26379

Slave node 192.168.30.130 sentinel port: 26379

Modify configuration:

Previously, redis has been downloaded and installed. Omit it here and modify the sentinel configuration file directly.

192.168.30.128

# vim /usr/local/redis/sentinel.conf

daemonize yes
logfile “/usr/local/redis/sentinel.log”
Dir “/ usr / local / redis / sentinel” ා sentinel working directory
Sentinel monitor mymaster 192.168.30.128 6379 2 ා it is recommended to set it to N / 2 + 1, where n is the number of sentinel
sentinel auth-pass mymaster 123456
Sentinel down after milliseconds mymaster 30000 ා judge the subjective offline time of the master, and the default is 30s

It should be noted that sentinel auth pass mymaster 123456 needs to be configured under sentinel monitor mymaster 192.168.30.128 6379 2, otherwise an error is reported when starting:


# /usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 104
>>> 'sentinel auth-pass mymaster 123456'
No such master with specified name.

Start sentinel all:

# mkdir /usr/local/redis/sentinel && chown -R redis:redis /usr/local/redis

# /usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf

To view logs on any host:

# tail -f /usr/local/redis/sentinel.log

21574:X 09 May 2019 15:32:04.298 # Sentinel ID is 30c417116a8edbab09708037366c4a7471beb770
21574:X 09 May 2019 15:32:04.298 # +monitor master mymaster 192.168.30.128 6379 quorum 2
21574:X 09 May 2019 15:32:04.299 * +slave slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 6379
21574:X 09 May 2019 15:32:04.300 * +slave slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.128 6379
21574:X 09 May 2019 15:32:16.347 * +sentinel sentinel 79b8d61626afd4d059fb5a6a63393e9a1374e78f 192.168.30.129 26379 @ mymaster 192.168.30.128 6379
21574:X 09 May 2019 15:32:31.584 * +sentinel sentinel d7b429dcba792103ef0d80827dd0910bd9284d21 192.168.30.130 26379 @ mymaster 192.168.30.128 6379

Several events in sentinel mode:

·+Reset master: the master server has been reset.

·+Slave: a new slave has been identified and associated by sentinel.

·+Failure state Recon Slaves: the failover state is switched to the reconf slaves state.

·+Failure detected: another sentinel starts a failover operation, or a slave server is converted to a master server.

·+Slave Recon sent: the sentinel of the leader sends [slaveof] (/ commands) to the instance/ slaveof.html )Command to set up a new master server for the instance.

·+Slave Recon inprog: the instance is setting itself as a slave of the specified master server, but the corresponding synchronization process has not been completed.

·+Slave reconf done: the slave server has successfully completed synchronization with the new master server.

·-DUP sentinel: one or more sentinels that monitor a given primary server have been removed because of repeated occurrences – this happens when the sentinel instance is restarted.

·+Sentinel: a new sentinel that monitors a given master server has been identified and added.

·+Sdown: the given instance is now in the subjective offline state.

·-Sdown: the given instance is no longer in the subjective offline state.

·+Odown: the given instance is now in an objective offline state.

·-Odown: the given instance is no longer in the objective offline state.

·+New Epoch: the current epoch has been updated.

·+Try failure: a new failover operation is in progress, waiting to be selected by the majority of sentinels.

·+Selected leader: after winning the election of the specified era, the fault migration operation can be carried out.

·+Failover state select Slave: the failover operation is now in the select slave state – sentinel is looking for a slave that can be upgraded to the primary server.

·No good Slave: sentinel operation failed to find a suitable slave for upgrade. Sentinel will try again after a period of time to find a suitable slave server to upgrade, or simply abort the fail over operation.

·Selected Slave: sentinel successfully finds the slave server suitable for upgrade.

·Failure state send slave of noone: Sentinel is upgrading the specified slave server to the master server, waiting for the upgrade function to complete.

·Fail end for timeout: the failover is aborted due to timeout, but eventually all slave servers will start to replicate with the new master any way.

·Fail end: the failover operation completed successfully. All slave servers are starting to replicate the new master server.

·+Switch master: the configuration changes, and the IP and address of the master server have been changed. This is information that most external users care about.

·+Tilt: enter tilt mode.

·-Tilt: exit tilt mode.

Master downtime demonstration:

192.168.30.128

# systemctl stop redis

# tail -f /usr/local/redis/sentinel.log

22428:X 09 May 2019 15:51:29.287 # +sdown master mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:29.371 # +odown master mymaster 192.168.30.128 6379 #quorum 2/2
22428:X 09 May 2019 15:51:29.371 # +new-epoch 1
22428:X 09 May 2019 15:51:29.371 # +try-failover master mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:29.385 # +vote-for-leader 30c417116a8edbab09708037366c4a7471beb770 1
22428:X 09 May 2019 15:51:29.403 # d7b429dcba792103ef0d80827dd0910bd9284d21 voted for 30c417116a8edbab09708037366c4a7471beb770 1
22428:X 09 May 2019 15:51:29.408 # 79b8d61626afd4d059fb5a6a63393e9a1374e78f voted for 30c417116a8edbab09708037366c4a7471beb770 1
22428:X 09 May 2019 15:51:29.451 # +elected-leader master mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:29.451 # +failover-state-select-slave master mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:29.528 # +selected-slave slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:29.528 * +failover-state-send-slaveof-noone slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:29.594 * +failover-state-wait-promotion slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:30.190 # +promoted-slave slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:30.190 # +failover-state-reconf-slaves master mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:30.258 * +slave-reconf-sent slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:30.511 # -odown master mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:31.233 * +slave-reconf-inprog slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:31.233 * +slave-reconf-done slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:31.297 # +failover-end master mymaster 192.168.30.128 6379
22428:X 09 May 2019 15:51:31.297 # +switch-master mymaster 192.168.30.128 6379 192.168.30.129 6379
22428:X 09 May 2019 15:51:31.298 * +slave slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.129 6379
22428:X 09 May 2019 15:51:31.298 * +slave slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379
22428:X 09 May 2019 15:52:31.307 # +sdown slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379

As can be seen from the log, master has been transferred from 192.168.30.128 to 192.168.30.129

View cluster information on 192.168.30.129


# /usr/local/bin/redis-cli -h 192.168.30.129 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.30.129:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.30.130,port=6379,state=online,offset=291039,lag=1
master_replid:757aff269236ed2707ba584a86a40716c1c76d74
master_replid2:47a862fc0ff20362be29096ecdcca6d432070ee9
master_repl_offset:291182
second_repl_offset:248123
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:291182

192.168.30.129:6379> set key4 linux
OK

Currently, there is only one slave in the cluster — 192.168.30.130, the master is 192.168.30.129, and 192.168.30.129 has write permission.

When viewing the redis configuration file on 192.168.30.130, you can also see replicaof 192.168.30.129 6379, which is a modification made by sentinel in the election master.

Restart the process on 192.168.30.128

# systemctl start redis

# tail -f /usr/local/redis/sentinel.log

22428:X 09 May 2019 15:51:31.297 # +switch-master mymaster 192.168.30.128 6379 192.168.30.129 6379
22428:X 09 May 2019 15:51:31.298 * +slave slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.129 6379
22428:X 09 May 2019 15:51:31.298 * +slave slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379
22428:X 09 May 2019 15:52:31.307 # +sdown slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379
22428:X 09 May 2019 16:01:24.872 # -sdown slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379

View cluster information


# /usr/local/bin/redis-cli -h 192.168.30.128 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.30.128:6379> info replication
# Replication
role:slave
master_host:192.168.30.129
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:514774
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:757aff269236ed2707ba584a86a40716c1c76d74
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:514774
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:376528
repl_backlog_histlen:138247

192.168.30.128:6379> get key4
"linux"

192.168.30.128:6379> set key5
(error) ERR wrong number of arguments for 'set' command

Even if 192.168.30.128 restarts the redis service, it will join the redis cluster as a slave. 192.168.30.129 is still the master.

Cluster mode

Introduction to cluster mode
Sentinel mode can basically meet the needs of general production, with high availability. However, when the amount of data is too large to be stored in one server, the master-slave mode or sentinel mode can not meet the requirements. At this time, the stored data needs to be partitioned and stored in multiple redis instances. The emergence of cluster mode is to solve the problem of the limited capacity of a single redis. Redis data is allocated to multiple machines according to certain rules.

Cluster can be said to be a combination of sentinel and master-slave mode. Through cluster, master-slave and master-slave reselection can be realized. Therefore, if two copies and three partitions are configured, six redis instances are required. Because redis data is allocated to different machines in the cluster according to certain rules, when the amount of data is too large, new machines can be added to expand the capacity.

To use a cluster, you only need to open the cluster enable configuration in the redis configuration file. Each cluster needs at least three primary databases to run normally. It is very convenient to add new nodes.

Cluster features:

*Multiple redis nodes network interconnection, data sharing

*All nodes are one master and one slave (which can also be one master and many slaves). They never provide services and are only used as standby

*It is not supported to process multiple keys at the same time (such as Mset / mget), because redis needs to distribute keys evenly on each node,
In the case of high concurrency, creating key value at the same time will reduce performance and lead to unpredictable behavior

*Support online addition and deletion of nodes

*The client can connect any master node for reading and writing

Build cluster mode

Environmental preparation:

Three machines, respectively open two redis services (ports)

192.168.30.128 port: 70017002

192.168.30.129 port: 70037004

192.168.30.130 port: 70057006

Modify the configuration file:

192.168.30.128

# mkdir /usr/local/redis/cluster

# cp /usr/local/redis/redis.conf /usr/local/redis/cluster/redis_7001.conf

# cp /usr/local/redis/redis.conf /usr/local/redis/cluster/redis_7002.conf

# chown -R redis:redis /usr/local/redis

# mkdir -p /data/redis/cluster/{redis_7001,redis_7002} && chown -R redis:redis /data/redis


# vim /usr/local/redis/cluster/redis_7001.conf

bind 192.168.30.128
port 7001
daemonize yes
pidfile "/var/run/redis_7001.pid"
logfile "/usr/local/redis/cluster/redis_7001.log"
dir "/data/redis/cluster/redis_7001"
#replicaof 192.168.30.129 6379
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 15000

# vim /usr/local/redis/cluster/redis_7002.conf

bind 192.168.30.128
port 7002
daemonize yes
pidfile "/var/run/redis_7002.pid"
logfile "/usr/local/redis/cluster/redis_7002.log"
dir "/data/redis/cluster/redis_7002"
#replicaof 192.168.30.129 6379
masterauth "123456"
requirepass "123456"
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 15000

The configuration of the other two machines is consistent with 192.168.30.128, which is omitted here

Start the redis service:

# redis-server /usr/local/redis/cluster/redis_7001.conf

# tail -f /usr/local/redis/cluster/redis_7001.log

# redis-server /usr/local/redis/cluster/redis_7002.conf

# tail -f /usr/local/redis/cluster/redis_7002.log

The start-up of the other two machines is consistent with that of 192.168.30.128, which is omitted here

Install Ruby and create a cluster (low version)
If the version of redis is relatively low, you need to install ruby. Install Ruby on any machine

# yum -y groupinstall "Development Tools"

# yum install -y gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel

# mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P ~/rpmbuild/SOURCES

# wget http://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P ~/rpmbuild/SPECS

# rpmbuild -bb ~/rpmbuild/SPECS/ruby22x.spec

# rpm -ivh ~/rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.x86_64.rpm

#Gem install redis ා the purpose is to install this, which is used to configure the cluster

# cp /usr/local/redis/src/redis-trib.rb /usr/bin/

# redis-trib.rb create –replicas 1 192.168.30.128:7001 192.168.30.128:7002 192.168.30.129:7003 192.168.30.129:7004 192.168.30.130:7005 192.168.30.130:7006

To create a cluster:

This is redis6.0.5, so you don’t need to install ruby, just create a cluster

# redis-cli -a 123456 --cluster create 192.168.30.128:7001 192.168.30.128:7002 192.168.30.129:7003 192.168.30.129:7004 192.168.30.130:7005 192.168.30.130:7006 --cluster-replicas 1

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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.168.30.129:7004 to 192.168.30.128:7001
Adding replica 192.168.30.130:7006 to 192.168.30.129:7003
Adding replica 192.168.30.128:7002 to 192.168.30.130:7005
M: 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001
  slots:[0-5460] (5461 slots) master
S: b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002
  replicates 6788453ee9a8d7f72b1d45a9093838efd0e501f1
M: 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003
  slots:[5461-10922] (5462 slots) master
S: b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004
  replicates 80c80a3f3e33872c047a8328ad579b9bea001ad8
M: 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005
  slots:[10923-16383] (5461 slots) master
S: 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006
  replicates 4d74ec66e898bf09006dac86d4928f9fad81f373
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

As you can see,

192.168.30.128:7001 is the master, and its slave is 192.168.30.129:7004;

192.168.30.129:7003 is the master, and its slave is 192.168.30.130:7006;

192.168.30.130:7005 is the master, and its slave is 192.168.30.128:7002

Automatic generation nodes.conf Document:


# ls /data/redis/cluster/redis_7001/
appendonly.aof dump.rdb nodes-7001.conf

# vim /data/redis/cluster/redis_7001/nodes-7001.conf 

6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master - 0 1557454406312 5 connected 10923-16383
277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557454407000 6 connected
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557454408371 5 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] myself,master - 0 1557454406000 1 connected 0-5460
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557454407366 4 connected
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master - 0 1557454407000 3 connected 5461-10922
vars currentEpoch 6 lastVoteEpoch 0

Cluster operation

Log in to the cluster:

#Redis-cli – C – H 192.168.30.128 – P 7001 – a 123456 ා – C, log in using cluster mode

To view the cluster information:

192.168.30.128:7001 > cluster info ᦇ cluster status

cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:580
cluster_stats_messages_pong_sent:551
cluster_stats_messages_sent:1131
cluster_stats_messages_ping_received:546
cluster_stats_messages_pong_received:580
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1131

List node information:

192.168.30.128:7001 > cluster nodes ා list node information

6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master - 0 1557455176000 5 connected 10923-16383
277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557455174000 6 connected
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557455175000 5 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] myself,master - 0 1557455175000 1 connected 0-5460
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557455174989 4 connected
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master - 0 1557455175995 3 connected 5461-10922

Here and here nodes.conf The contents of the documents are the same

Write data:

192.168.30.128:7001> set key111 aaa
->Re directed to slot [13680] located at 192.168.30.130:7005
OK

192.168.30.130:7005> set key222 bbb
->Re directed to slot [2320] located at 192.168.30.128:7001
OK

192.168.30.128:7001> set key333 ccc
->Re directed to slot [7472] located at 192.168.30.129:7003
OK

192.168.30.129:7003> get key111
-> Redirected to slot [13680] located at 192.168.30.130:7005
"aaa"

192.168.30.130:7005> get key333
-> Redirected to slot [7472] located at 192.168.30.129:7003
"ccc"

192.168.30.129:7003>

We can see that the redis cluster cluster is decentralized, each node is equal, and any node connected can obtain and set data.

Of course, equality refers to the master node, because the slave node does not provide services at all, but serves as a backup of the corresponding master node.

Add node:

Add a node on 192.168.30.129


# cp /usr/local/redis/cluster/redis_7003.conf /usr/local/redis/cluster/redis_7007.conf

# vim /usr/local/redis/cluster/redis_7007.conf

bind 192.168.30.129
port 7007
daemonize yes
pidfile "/var/run/redis_7007.pid"
logfile "/usr/local/redis/cluster/redis_7007.log"
dir "/data/redis/cluster/redis_7007"
#replicaof 192.168.30.129 6379
masterauth "123456"
requirepass "123456"
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7007.conf
cluster-node-timeout 15000

# mkdir /data/redis/cluster/redis_7007

# chown -R redis:redis /usr/local/redis && chown -R redis:redis /data/redis

# redis-server /usr/local/redis/cluster/redis_7007.conf 

Add a node on 192.168.30.130


# cp /usr/local/redis/cluster/redis_7005.conf /usr/local/redis/cluster/redis_7008.conf

# vim /usr/local/redis/cluster/redis_7007.conf

bind 192.168.30.130
port 7008
daemonize yes
pidfile "/var/run/redis_7008.pid"
logfile "/usr/local/redis/cluster/redis_7008.log"
dir "/data/redis/cluster/redis_7008"
#replicaof 192.168.30.130 6379
masterauth "123456"
requirepass "123456"
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7008.conf
cluster-node-timeout 15000

# mkdir /data/redis/cluster/redis_7008

# chown -R redis:redis /usr/local/redis && chown -R redis:redis /data/redis

# redis-server /usr/local/redis/cluster/redis_7008.conf 

Add nodes to the cluster:


192.168.30.129:7003> CLUSTER MEET 192.168.30.129 7007
OK

192.168.30.129:7003> CLUSTER NODES

4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] myself,master - 0 1557457361000 3 connected 5461-10922
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] master - 0 1557457364746 1 connected 0-5460
277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557457362000 6 connected
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557457363000 4 connected
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557457362000 5 connected
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master - 0 1557457362729 0 connected
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master - 0 1557457363739 5 connected 10923-16383

192.168.30.129:7003> CLUSTER MEET 192.168.30.130 7008
OK

192.168.30.129:7003> CLUSTER NODES

4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] myself,master - 0 1557457489000 3 connected 5461-10922
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] master - 0 1557457489000 1 connected 0-5460
277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557457489000 6 connected
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557457488000 4 connected
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557457489472 5 connected
1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] master - 0 1557457489259 0 connected
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master - 0 1557457489000 0 connected
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master - 0 1557457490475 5 connected 10923-16383

As you can see, all the new nodes join the cluster as master

Change node identity:

Change the identity of the new 192.168.30.130:7008 node to the slave of 192.168.30.129:7007

# redis-cli -c -h 192.168.30.130 -p 7008 -a 123456 cluster replicate e51ab166bc0f33026887bcf8eba0dff3d5b0bf14

Cluster replicate followed by node_ ID, change the identity of the corresponding node. You can also log in to the cluster for changes


# redis-cli -c -h 192.168.30.130 -p 7008 -a 123456

192.168.30.130:7008> CLUSTER REPLICATE e51ab166bc0f33026887bcf8eba0dff3d5b0bf14
OK

192.168.30.130:7008> CLUSTER NODES

277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458316881 3 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] master - 0 1557458314864 1 connected 0-5460
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master - 0 1557458316000 3 connected 5461-10922
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master - 0 1557458315872 5 connected 10923-16383
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458317890 5 connected
1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458315000 7 connected
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458315000 1 connected
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master - 0 1557458314000 0 connected

View the corresponding nodes.conf File, which records the node information of the current cluster

# cat /data/redis/cluster/redis_7001/nodes-7001.conf

1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458236169 7 connected
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master – 0 1557458235000 5 connected 10923-16383
277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458234103 6 connected
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458235129 5 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] myself,master – 0 1557458234000 1 connected 0-5460
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458236000 4 connected
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master – 0 1557458236000 0 connected
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master – 0 1557458233089 3 connected 5461-10922
vars currentEpoch 7 lastVoteEpoch 0

Delete node:

192.168.30.130:7008> CLUSTER FORGET 1a1c7f02fce87530bd5abdfc98df1cffce4f1767
(error) err I produced hard but I can’t forget myself

192.168.30.130:7008> CLUSTER FORGET e51ab166bc0f33026887bcf8eba0dff3d5b0bf14
(error) err can’t forget my master

192.168.30.130:7008> CLUSTER FORGET 6788453ee9a8d7f72b1d45a9093838efd0e501f1
OK ා you can delete other master nodes

192.168.30.130:7008> CLUSTER NODES

277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458887328 3 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] master – 0 1557458887000 1 connected 0-5460
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master – 0 1557458886000 3 connected 5461-10922
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave – 0 1557458888351 5 connected
1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458885000 7 connected
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458883289 1 connected
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master – 0 1557458885310 0 connected

192.168.30.130:7008> CLUSTER FORGET b4d3eb411a7355d4767c6c23b4df69fa183ef8bc
OK ා you can delete other slave nodes

192.168.30.130:7008> CLUSTER NODES

277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557459031397 3 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] master – 0 1557459032407 1 connected 0-5460
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master – 0 1557459035434 3 connected 5461-10922
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master – 0 1557459034000 5 connected 10923-16383
1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557459032000 7 connected
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557459034000 1 connected
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master – 0 1557459034427 0 connected

Save configuration:

Save the node configuration information to the hard disk
OK

# cat /data/redis/cluster/redis_7001/nodes-7001.conf

1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458236169 7 connected
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master – 0 1557458235000 5 connected 10923-16383
277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458234103 6 connected
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458235129 5 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] myself,master – 0 1557458234000 1 connected 0-5460
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458236000 4 connected
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master – 0 1557458236000 0 connected
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master – 0 1557458233089 3 connected 5461-10922
vars currentEpoch 7 lastVoteEpoch 0

# redis-cli -c -h 192.168.30.130 -p 7008 -a 123456
Warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe.

192.168.30.130:7008> CLUSTER NODES
277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557459500741 3 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] master – 0 1557459500000 1 connected 0-5460
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master – 0 1557459501000 3 connected 5461-10922
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master – 0 1557459500000 5 connected 10923-16383
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557459499737 5 connected
1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557459499000 7 connected
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557459501750 1 connected
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master – 0 1557459498000 0 connected

As you can see, the nodes that were previously deleted have been restored. This is because the corresponding configuration file has not been deleted. Cluster saveconfig recovery is performed.

Simulate that the master node fails:

192.168.30.128


# netstat -lntp |grep 7001
tcp    0   0 192.168.30.128:17001  0.0.0.0:*        LISTEN   6701/redis-server 1 
tcp    0   0 192.168.30.128:7001   0.0.0.0:*        LISTEN   6701/redis-server 1 

# kill 6701

192.168.30.130:7008> CLUSTER NODES

277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557461178000 3 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] master,fail – 1557460950483 1557460947145 1 disconnected
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master – 0 1557461174922 3 connected 5461-10922
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master – 0 1557461181003 5 connected 10923-16383
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557461179993 5 connected
1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557461176000 7 connected
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] master – 0 1557461178981 8 connected 0-5460
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master – 0 1557461179000 0 connected

In the row corresponding to 7001, you can see that master failed and the status is disconnected; while in the row corresponding to 7004, slave has become master.

Restart 7001 node:


# redis-server /usr/local/redis/cluster/redis_7001.conf

192.168.30.130:7008> CLUSTER NODES

277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:[email protected] slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557461307000 3 connected
80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:[email protected] slave b6331cbc986794237c83ed2d5c30777c1551546e 0 1557461305441 8 connected
4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:[email protected] master - 0 1557461307962 3 connected 5461-10922
6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:[email protected] master - 0 1557461304935 5 connected 10923-16383
b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:[email protected] slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557461306000 5 connected
1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:[email protected] myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557461305000 7 connected
b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:[email protected] master - 0 1557461308972 8 connected 0-5460
e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:[email protected] master - 0 1557461307000 0 connected

As you can see, node 7001 is a slave node after it is started, and it is a slave node of 7004. That is, if the master node is down, its slave node becomes a new master node and continues to provide external services. If the original master node is restarted, it becomes the slave node of the new master node.

In addition, if the 7007 node is used for testing, it will be found that the 7008 node will not switch, because there is no data on the 7007 node at all. The cluster data is divided into three parts. If 16384 slots are allocated by hash slots, the slot intervals of the three nodes are as follows:

Node 7004 covers 0-5460
Node 7003 covers 5461-10922
Node 7005 covers 10923-16383

For more information:

Redis cluster common commands