Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Redis Cluster

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of Redis cluster, which is very detailed and has certain reference value. Friends who are interested must finish it!

Detailed explanation of Redis Cluster

Redis has three cluster modes, which are:

* Master-slave mode * Sentinel mode * Cluster mode

The three cluster models have their own characteristics. For an introduction to Redis, please refer to here: NoSQL (2)-- Redis

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

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 categories: master database (master) and slave database (slave).

Master-slave replication has the following characteristics:

* the master database can perform read and write operations, and when the read and write operations cause data changes, the data will be automatically synchronized to the slave database * the slave database is generally read-only and receives data synchronized from the master database * a master can have multiple slave, but a slave can only correspond to one master* slave and does not affect the read and write of other slave and master Data will be synchronized from master after restart * after master is hung, it will not affect the read of slave, but redis will no longer provide write service. Redis will provide write service again after master restart. * after master is hung up, a master will not be re-selected in the slave node.

Working mechanism:

When slave starts, proactively send a SYNC command to master. After receiving the SYNC command, master saves the snapshot (RDB persistence) in the background and caches the command to save the snapshot for a period of time, and then sends the saved snapshot file and the cached command to slave. Slave loads snapshot files and cached execution commands after receiving snapshot files and commands.

After replication initialization, every write command received by master will be sent to slave synchronously to ensure the consistency of master and slave data.

Security Settings:

When the master node sets the password

Client access to master requires a password to start slave. You can configure it in the configuration file to access slave without a password.

Disadvantages:

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

Master-slave mode building

Environmental preparation:

Master node 192.168.30.128slave node 192.168.30.129slave node 192.168.30.130

Download and install all:

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

All configured as services:

Service file

# vim / usr/lib/systemd/system/ redis.service [Unit] Description=Redis persistent key-value databaseAfter=network.targetAfter=network-online.targetWants=network- online.target [Service] ExecStart=/usr/local/bin/redis-server / usr/local/redis/redis.conf-- supervised systemdExecStop=/usr/libexec/redis-shutdownType=notifyUser=redisGroup=redisRuntimeDirectory=redisRuntimeDirectoryMode= 0755 [install] WantedBy=multi-user.target

Shutdown script

# vim / usr xREDIS_CLI=/usr/local/bin/redis-cli# Retrieve service nameSERVICE_NAME= xREDIS_CLI=/usr/local/bin/redis-cli# Retrieve service nameSERVICE_NAME= if if [- z "$SERVICE_NAME"] Then SERVICE_NAME=redisfi# Get the proper config file based on service nameCONFIG_FILE= "/ usr/local/redis/$SERVICE_NAME.conf" # Use awk to retrieve host Port from config fileHOST= `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 PortHOST=$ {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 properlyif [- e "$SOCK"] Then $REDIS_CLI-s $SOCK $ADDITIONAL_PARAMS shutdownelse $REDIS_CLI-h $HOST-p $PORT $ADDITIONAL_PARAMS shutdownfi# chmod + x / usr/libexec/redis-shutdown# useradd-s / sbin/nologin redis# chown-R redis:redis / usr/local/redis# chown-R redis:redis / data/redis# yum install-y bash-completion & & source / etc/profile # Command completion # systemctl daemon-reload# systemctl enable redis

Modify the configuration:

192.168.30.128

# mkdir-p / data/redis# vim / usr/local/redis/redis.confbind 192.168.30.128 # listening ip, multiple ip separated by spaces daemonize yes # allows the background to launch logfile "/ usr/local/redis/redis.log" # Log path dir / data/redis# database backup file storage directory masterauth 123456 # slave connection master password Master can omit requirepass 123456 # to set the master connection password, and slave can omit appendonly yes # to generate an appendonly.aof file in the / data/redis/ directory and append each write request to the appendonly.aof file # echo 'vm.overcommit_memory=1' > > / etc/sysctl.conf# sysctl-p

192.168.30.129

# mkdir-p / data/redis# vim / usr/local/redis/redis.confbind 192.168.30.129daemonize yeslogfile "/ usr/local/redis/redis.log" dir / data/redisreplicaof 192.168.30.128 6379masterauth 123456requirepass 123456appendonly yes# echo 'vm.overcommit_memory=1' > > / etc/sysctl.conf# sysctl-p

192.168.30.130

# mkdir-p / data/redis# vim / usr/local/redis/redis.confbind 192.168.30.130daemonize yeslogfile "/ usr/local/redis/redis.log" dir / data/redisreplicaof 192.168.30.128 6379masterauth 123456requirepass 123456appendonly yes# echo 'vm.overcommit_memory=1' > > / etc/sysctl.conf# sysctl-p

Start redis all:

# systemctl start redis

View the cluster status:

# redis-cli-h 192.168.30.128-a 123456Warning: Using a password with'- a'or'- u 'option on the command line interface may not be safe.192.168.30.128:6379 > info replication# Replicationrole:masterconnected_slaves:2slave0:ip=192.168.30.129,port=6379,state=online,offset=168,lag=1slave1:ip=192.168.30.130,port=6379,state=online,offset=168 Lag=1master_replid:fb4941e02d5032ad74c6e2383211fc58963dbe90master_replid2:0000000000000000000000000000000000000000master_repl_offset:168second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:168# redis-cli-h 192.168.30.129-a 123456 info replicationWarning: Using a password with'- a'or'- u 'option on the command line interface may not be safe.# Replicationrole:slavemaster_host:192.168.30.128master_port:6379master_link_status:upmaster_last_ Io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:196slave_priority:100slave_read_only:1connected_slaves:0master_replid:fb4941e02d5032ad74c6e2383211fc58963dbe90master_replid2:0000000000000000000000000000000000000000master_repl_offset:196second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:196

Data presentation:

192.168.30.128 option on the command line interface may not be safe.192.168.30.129:6379 6379 > keys * (empty list or set) 192.168.30.128 option on the command line interface may not be safe.192.168.30.129:6379 6379 > set key1 100OK192.168.30.128:6379 > set key2 lzxOK192.168.30.128:6379 > keys * 1) "key1" 2) "key2" # redis-cli-h 192.168.30.129-a 123456Warning: 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 dir > CONFIG GET dir1) "dir" 2) "/ data/redis" 192.168.30.129 dir > CONFIG GET dbfilename1) "dbfilename" 2) "dump.rdb" 192.168.30.129 set key3 aaa 6379 > get key1 "100" 192.168.30.129ve6379 > get key2 "lzx" 192.168.30.1296379 > set key3 aaa (error) READONLY You can't write against a read only replica.# redis-cli-h 192. 168.30.130-a 123456Warning: 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.130RV 6379 > CONFIG GET dir1) "dir" 2) "/ data/redis" 192.168.30.130RV 6379 > CONFIG GET dbfilename1) "dbfilename" 2) "dump.rdb" 192.168.30.130RV 6379 > get key1 "100 "192.168.30.130 READONLY You can't write against a read only replica 6379 > get key2" lzx "192.168.30.130 READONLY You can't write against a read only replica 6379 > set key3 aaa (error).

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

Introduction to Sentinel mode Sentinel mode

The disadvantage of master-slave mode is that it does not have high availability. When master is dead, Redis will no longer be able to provide write operations, so sentinel arises at the historic moment.

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

* sentinel mode is based on master-slave mode. If there is only one Redis node, sentinel does not make any sense * when master dies, sentinel will select one of slave as master and modify their configuration files, and other slave configuration files will also be modified. For example, the slaveof attribute will point to the new master* when master is restarted. It will no longer be master but will be used as slave to receive synchronized data of new master * sentinel is also a process that may fail, so sentinel will also start multiple sentinel clusters * when multi-sentinel configuration, sentinel will also automatically monitor * when the master-slave mode configuration password, sentinel will also synchronously change the configuration information to the configuration file, do not worry * one sentinel or sentinel cluster can manage multiple master-slave Redis Multiple sentinel can also monitor the same redis* sentinel. It is better not to deploy on the same machine as Redis, otherwise after Redis's server dies, sentinel also dies.

Working mechanism:

* each sentinel sends a PING command to the master,slave and other sentinel instances it knows once per second * if the time from the last valid reply to the PING command exceeds the value specified in the down-after-milliseconds option, the instance will be marked as subjectively offline by sentinel. * if a master is marked as subjective offline, all sentinel monitoring the master should confirm that the master has indeed entered the subjective offline state at a frequency of once per second * when a sufficient number of sentinel (greater than or equal to the value specified in the profile) confirms that the master has indeed entered the subjective offline state within a specified time range, the master will be marked as objective offline * in general Each sentinel sends INFO commands to all known master,slave at a frequency of every 10 seconds * when master is marked as objective offline by sentinel, the frequency of INFO commands sent by sentinel to all slave of offline master will be changed from once in 10 seconds to once per second * if a sufficient number of sentinel agree that master has been offline, the objective offline status of master will be removed. If master returns a valid reply to sentinel's PING command, the subjective offline state of master will be removed.

When using sentinel mode, the client does not connect to Redis directly, but connects ip and port of sentinel, and sentinel provides a specific Redis implementation that provides services, so that when the master node dies, sentinel will perceive and provide the new master node to the consumer.

Sentinel schema building

Environmental preparation:

Master node 192.168.30.128 sentinel port: 26379slave node 192.168.30.129 sentinel port: 26379slave node 192.168.30.130 sentinel port: 26379

Modify the configuration:

Redis has been downloaded and installed earlier, which is omitted here and directly modified the sentinel configuration file.

192.168.30.128

# vim / usr/local/redis/sentinel.confdaemonize yeslogfile "/ usr/local/redis/sentinel.log" dir "/ usr/local/redis/sentinel" # sentinel working directory sentinel monitor mymaster 192.168.30.128 6379 2 # at least 2 sentinel consents are required to determine the failure of master. It is recommended to set it to nbank 2x1. N is the number of sentinel sentinel auth-pass mymaster 123456sentinel down-after-milliseconds mymaster 30000 # to judge the subjective offline time of master. Default is 30s.

It should be noted here that sentinel auth-pass mymaster 123456 needs to be configured under sentinel monitor mymaster 192.168.30.128 6379 2, otherwise an error message will be launched:

# / 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

Any host views the log:

# tail-f / usr/local/redis/sentinel.log21574:X 09 May 2019 15 May 32 May 04.298 # Sentinel ID is 30c417116a8edbab09708037366c4a7471beb77021574:X 09 May 2019 15V 32 May 04.298 # + monitor master mymaster 192.168.30.128 6379 quorum 221574 May 09 May 2019 15V 32v 04.299 * + slave slave 192.168.30.9V 6379 192.168.30.129 6379 @ mymaster 192.168.30.128 637921574JX 09 May 2019 15V 32V 04.300 * + slave slave 192.168.30. 130 May 6379 192.168.30.130 6379 @ mymaster 192.168.30.128 637921574 sentinel sentinel 79b8d61626afd4d059fb5a6a63393e9a1374e78f 2019 15347 * + sentinel sentinel 79b8d61626afd4d059fb5a6a63393e9a1374e78f 192.168.30.129 26379 @ mymaster 192.168.30.128 63792157VX 09 May 2019 15Ze32 31.584 * + sentinel sentinel d7b429dcba792103ef0d80827dd0910bd9284d21 192.168.30.130 26379 @ mymaster 192.168.30.128 6379

Several events in Sentinel mode:

+ reset-master: the primary server has been reset. + slave: a new slave server has been identified and associated by Sentinel. + failover-state-reconf-slaves: the failover state is switched to the reconf-slaves state. + failover-detected: another Sentinel starts a failover operation, or a slave server is converted to a master server. + slave-reconf-sent: the Sentinel of the leader (leader) sends the [SLAVEOF] (/ commands/slaveof.html) command to the instance to set a new master server for the instance. + slave-reconf-inprog: the instance is setting itself up as a slave to the specified master server, but the corresponding synchronization process has not yet been completed. + slave-reconf-done: the slave server has successfully completed synchronization with the new master server. -dup-sentinel: one or more Sentinel monitoring for a given primary server has been removed due to repetition-- this is the case when the Sentinel instance is restarted. + sentinel: a new Sentinel that monitors a given primary server has been identified and added. + sdown: the given instance is now subjectively offline. -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 objectively offline. + new-epoch: the current epoch has been updated. + try-failover: a new failover operation is in progress, waiting to be selected by most Sentinel (waiting to be elected by the majority). + elected-leader: win the election of the specified era, and you can perform failover operation. + failover-state-select-slave: the failover operation is now in select-slave state-Sentinel is looking for a slave server that can be upgraded to the master server. No-good-slave: the Sentinel operation failed to find a slave server suitable for upgrading. Sentinel will try again after a period of time to find a suitable slave server to upgrade, or simply give up performing the failover operation. Selected-slave: Sentinel successfully found a slave server that is suitable for upgrading. Failover-state-send-slaveof-noone: Sentinel is upgrading the specified slave server to the master server, waiting for the upgrade function to complete. Failover-end-for-timeout: the failover is aborted due to a timeout, but eventually all slave servers will start replicating the new master server (slaves will eventually be configured to replicate with the new master anyway). Failover-end: the failover operation completed successfully. All slave servers are starting to copy the new master server. + switch-master: the configuration has changed, and the IP and address of the primary server have changed. This is the information that the vast majority of external users care about. + tilt: enter tilt mode. -tilt: exit tilt mode.

Master downtime demo:

192.168.30.128

# systemctl stop redis# tail-f / usr/local/redis/sentinel.log22428:X 09 May 2019 15 sdown master mymaster 29.287 # + sdown master mymaster 192.168.30.128 637922428 sdown master mymaster 192.168.30.128 637922428 X 09 May 2019 15V 29.371 # + odown master mymaster 192.168.30.128 6379 # quorum 2 222428 May X 09 May 2019 15 15 sdown master mymaster 129.371 # + new-epoch 122428X 09 May 2019 15 May 2019 : 51 May 29.385 # + vote-for-leader 30c417116a8edbab09708037366c4a7471beb770 122428 d7b429dcba792103ef0d80827dd0910bd9284d21 voted for 30c417116a8edbab09708037366c4a7471beb770 X 09 May 2019 1515 d7b429dcba792103ef0d80827dd0910bd9284d21 voted for 30c417116a8edbab09708037366c4a7471beb770 29.403 # d7b429dcba792103ef0d80827dd0910bd9284d21 voted for 30c417116a8edbab09708037366c4a7471beb770 122428 May 2019 151V 29.408 # 79b8d61626afd4d059fb5a6a63393e9a1374e78f voted for 30c417116a8edbab09708037366c4a7471beb770 122428 79b8d61626afd4d059fb5a6a63393e9a1374e78f voted for 30c417116a8edbab09708037366c4a7471beb770 2019 151V 29.451 # + elected-leader master mymaster 192.168.128 637922428X 09 May 2019 151428X 2019 151428 51mer 29.451 # + failover-state-select-slave master mymaster 192.168.30.128 637922428MX 09 May 2019 151Rod 29.528 # + selected -slave slave 192.168.30.129 slave slave 6379 192.168.30.128 637922428 mymaster 192.168.30.128 637922428 mymaster 192.168.30.128 637922428 mymaster 192.168.30.129 6379 @ mymaster 192.168.30.129 6379 @ failover-state-wait-promotion slave 192.168.30.129 6379 192.168.30.129 6379 @ mymaster 192.168.30.128 637922428 mymaster 192.168.30.129 6379 @ mymaster 192.168.30.128 637922428 May 2019 1551 mymaster 192.168.30.128 637922428 failover-state-reconf-slaves master mymaster 192.168.30.128 637922428 mymaster 192.168.30.128 637922428 May 192.168.30.128 637922428 odown master mymaster 192.168.30.128 637922428 slave-reconf-inprog slave 192.168.30.130 mymaster 192.168.30.128 637922428 mymaster 192.168.30.128 637922428 slave-reconf-inprog slave 192.168.30.128 637922428 mymaster 192.168.30.128 637922428 May 192.168.30.130 mymaster 168.30.128 637922428 failover-end master mymaster 192.168.30.128 637922428 switch-master mymaster 192.168.30.128 6379 192.168.128 6379 192.168.128 6379 192.168.128 192.168.128 6379 192.168.328 May 2019 1551Rod 31.298 * + slave slave 192.168.30.130 May 6379 192.168.30.130 6379 192.168.30.130 6379 @ mymaster 192.168.30.129 637922428 X 09 May 2019 151414 31.298 * + slave slave 192.168.30.128 slave slave 6379 192.168.30.128 6379 @ mymaster 192.168.30.129 637922428 May 2019 152V 31.307 # + sdown slave 192.168.30.128 6379 @ mymaster 192.168.30.128 6379 @ mymaster

As you can see 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 123456Warning: Using a password with'- a'or'- u 'option on the command line interface may not be safe.192.168.30.129:6379 > info replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.30.130,port=6379,state=online,offset=291039 Lag=1master_replid:757aff269236ed2707ba584a86a40716c1c76d74master_replid2:47a862fc0ff20362be29096ecdcca6d432070ee9master_repl_offset:291182second_repl_offset:248123repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:291182192.168.30.129:6379 > set key4 linuxOK

Only one slave--192.168.30.130,master in the current cluster is 192.168.30.129, and 192.168.30.129 has write permissions.

Looking at the configuration file of redis on 192.168.30.130, you can also see replicaof 192.168.30.129 6379, which is the change sentinel made during the election of master.

Restart the process on 192.168.30.128

# systemctl start redis# tail-f / usr/local/redis/sentinel.log22428:X 09 May 2019 15 systemctl start redis# tail 31.297 # + switch-master mymaster 192.168.30.128 6379 192.168.129 637922428 switch-master mymaster 192.168.129 637922428 switch-master mymaster 192.168.129 637922428 switch-master mymaster 192.168.30.130V 6379 192.168.130V 6379 192.168.130 6379 @ mymaster 192.168.30.129 637922428 May 192.168.30.1286379 192.168 .30.128 6379 @ mymaster 192.168.30.129 637922428 sdown slave 192.168.30.128V 6379 192.168.30.128 6379 @ mymaster 192.168.30.129 637922428 sdown slave @ mymaster 192.168.30.129 637922428 May 2019 16V 01L 24.872 #-sdown slave 192.168.30.128V 6379 192.168.30.128 6379 @ mymaster 192.168.30.128 6379

View cluster information

# / usr/local/bin/redis-cli-h 192.168.30.128-p 6379-a 123456Warning: Using a password with'- a'or'- u 'option on the command line interface may not be safe.192.168.30.128:6379 > info replication# Replicationrole:slavemaster_host:192.168.30.129master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:514774slave_priority:100slave_read_only: 1connected_slaves:0master_replid:757aff269236ed2707ba584a86a40716c1c76d74master_replid2:0000000000000000000000000000000000000000master_repl_offset:514774second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:376528repl_backlog_histlen:138247192.168.30.128:6379 > get key4 "linux" 192.168.30.128 1connected_slaves:0master_replid:757aff269236ed2707ba584a86a40716c1c76d74master_replid2:0000000000000000000000000000000000000000master_repl_offset:514774second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:376528repl_backlog_histlen:138247192.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 joins the redis cluster as slave, and 192.168.30.129 is still master.

Introduction to Cluster mode 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 on a server, the master-slave mode or sentinel mode can not meet the demand. At this time, it is necessary to slice the stored data and store the data in multiple Redis instances. The emergence of cluster mode is to solve the problem of limited Redis capacity of a single machine, and distribute the data of Redis to multiple machines according to certain rules.

Cluster can be said to be a combination of sentinel and master-slave mode. Master-slave and master reselection can be realized through cluster, so if you configure two copies and three shards, you need six Redis instances. Because the data of Redis is allocated to different machines of cluster according to certain rules, when the amount of data is too large, you can add new machines to expand the capacity.

With clustering, you only need to open the cluster-enable configuration in the redis configuration file. At least three master databases are needed in each cluster to function properly, and it is very convenient to add new nodes.

Cluster cluster features:

* multiple redis nodes are interconnected and data are shared * all nodes are one master and one slave (or one master and multiple slaves), in which services are never provided and only as a standby * it is not supported to deal with multiple key (such as MSET/MGET) at the same time, because redis needs to distribute key evenly on each node. In the case of high concurrency, creating key-value at the same time will degrade performance and lead to unpredictable behavior * support adding and deleting nodes online * clients can connect to any master node to build read-write Cluster mode.

Environmental preparation:

Three machines, respectively open two redis services (ports) 192.168.30.128 port: 7001Magi 7002192.168.30.129 Port: 7003Magi 7004192.168.30.130 Port: 7005Power7006

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.confbind 192.168.30.128port 7001daemonize yespidfile "/ var/run/redis_7001.pid" logfile "/ usr/local/redis/cluster/redis_7001.log" dir "/ data/redis/cluster/redis_7001" # replicaof 192.168.30.129 6379masterauth 123456requirepass 123456appendonly yescluster-enabled yescluster-config-file nodes_7001.confcluster-node-timeout 1500 vim / usr/local / redis/cluster/redis_7002.confbind 192.168.30.128port 7002daemonize yespidfile "/ var/run/redis_7002.pid" logfile "/ usr/local/redis/cluster/redis_7002.log" dir "/ data/redis/cluster/redis_7002" # replicaof 192.168.30.129 6379masterauth "123456" requirepass "123456" appendonly yescluster-enabled yescluster-config-file nodes_7002.confcluster-node-timeout 15000

The configuration of the other two machines is the same as that of 192.168.30.128, 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 other two machines start in the same way as 192.168.30.128, omitted here

Install ruby and create a cluster (lower version):

If the redis version is lower, you need to install ruby. You can 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 # is intended to install this 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 replicas 7001 192.168.30.128 VOL7002 192.168.30.129RV 7003 192.168.30.129Rd 7004 192.168.30.130005 72.168.30.130RO 7006

Create a cluster:

This is redis5.0.4, so you don't need to install ruby, just create a cluster.

# redis-cli-a 123456-- cluster create 192.168.30.128 redis-cli 7002 192.168.30.128V 7002 192.168.30.129V 7003 192.168.30.129V 7004 192.168.30.130V 7005 192.168.30.130V 7006-- cluster-replicas 1Warning: 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-5460Master [1] ]-> Slots 5461-10922Master [2]-> Slots 10923-16383Adding replica 192.168.30.129 16383Adding replica 7004 to 192.168.30.128:7001Adding replica 192.168.30.130V 7006 to 192.168.30.129:7003Adding replica 192.168.30.128V 7002 to 192.168.30.130V 7005m: 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128V 7001 slots: [0-5460] (5461 slots) masterS: b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128V 7002 replicates 6788453ee9a8d7f72b1d45a9093838efd0e501f1M: 4d74ec66e898bf09006dac86d4928f9fad81f373 192 .168.30.129: 7003 slots: [5461-10922] (5462 slots) masterS: b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 b6331cbc986794237c83ed2d5c30777c1551546e 7004 replicates 80c80a3f3e33872c047a8328ad579b9bea001ad8M: 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130V 7005 slots: [10923-16383] (5461 slots) masterS: 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130V 7006 replicates 4d74ec66e898bf09006dac86d4928f9fad81f373Can I set the above configuration? (type 'yes' to accept): enter yes for yes # and accept the above configuration > Nodes configuration updated > Assign a different config epoch to each node > Sending CLUSTER MEET messages to join the cluster

You can see

192.168.30.128master 7001 is master, its slave is 192.168.30.129 slave 7003 is master, its slave is 192.168.30.130master, its slave is 192.168.30.128RV 7002

Automatically generate nodes.conf files:

# 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 data/redis/cluster/redis_7001/nodes-7001.conf 6788453ee9a8d7f72b1d45a9093838efd0e501f1 7005 "17005 master-01557454406312 5 connected 10923-16383277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130V 7006" 17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557454407000 6 connectedb4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128V 7002 "17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 01557454408371 5 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128" 7001 "17001 myself Master-0 1557454406000 1 connected 0-5460b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557454407366 4 connected4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129master 7003 master-01557454407000 3 connected 5461-10922vars 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

View cluster information:

192.168.30.128 7001 > CLUSTER INFO # Cluster status cluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_ping_sent:580cluster_stats_messages_pong_sent:551cluster_stats_messages_sent:1131cluster_stats_messages_ping_received:546cluster_stats_messages_pong _ received:580cluster_stats_messages_meet_received:5cluster_stats_messages_received:1131

List node information:

192.168.30.128 CLUSTER NODES 7001 > CLUSTER NODES # lists the node information 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130V 7005 "17005 master-0 1557455176000 5 connected 10923-16383277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130 master 7006" 17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557455174000 6 connectedb4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128V 7002 "17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 01557455175000 5 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128UR 700117001" 17001 myself Master-0 1557455175000 1 connected 0-5460b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557455174989 4 connected4d74ec66e898bf09006dac86d4928f9fad81f373 01557455174989 4 master-01557455175995 3 connected 5461-10922

This is the same as the nodes.conf file.

Write data:

192.168.30.128 set key111 aaa- > Redirected to slot [13680] located at 192.168.30.130 located at 7005 # indicates that the data has reached 192.168.30.130 set key222 bbb- > Redirected to slot [2320] located at 192.168.30.128 located at 7001 # indicates that the data has arrived at 192.168.30.128 7001 > set key333 ccc- > Redirected to slot [7472] located at 192.168.30.129 located at 7003 # OK192.168.30.129:7003 > get key111- > Redirected to slot [13680] located at 192.168.30.130V 7005 "aaa" 192.168.30.130V 7005 > get key333- > Redirected to slot [7472] located at 192.168.30.129 Vol 7003 "ccc" 192.168.30.129V 7003 >

You can see that the redis cluster cluster is decentralized, each node is equal, and the data can be obtained and set by connecting to any node.

Of course, equality refers to the master node, because the slave node provides no service at all, only as a backup of the corresponding master node.

Add nodes:

Add a node to 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.confbind 192.168.30.129port 7007daemonize yespidfile "/ var/run/redis_7007.pid" logfile "/ usr/local/redis/cluster/redis_7007.log" dir "/ data/redis/cluster/redis_7007" # replicaof 192.168.30.129 6379masterauth "123456" requirepass "123456" appendonly yescluster-enabled yescluster- Config-file nodes_7007.confcluster-node-timeout 1500 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 to 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.confbind 192.168.30.130port 7008daemonize yespidfile "/ var/run/redis_7008.pid" logfile "/ usr/local/redis/cluster/redis_7008.log" dir "/ data/redis/cluster/redis_7008" # replicaof 192.168.30.130 6379masterauth "123456" requirepass "123456" appendonly yescluster-enabled yescluster- Config-file nodes_7008.confcluster-node-timeout 1500 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 CLUSTER NODES4d74ec66e898bf09006dac86d4928f9fad81f373 7003 > CLUSTER MEET 192.168.30.129 myself > 192.168.30.129 myself Master-0 1557457361000 3 connected 5461-1092280c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128 master 7001 "17001 master-01557457364746 1 connected 0-5460277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130V 7006" 17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557457362000 6 connectedb6331cbc986794237c83ed2d5c30777c1551546e 192.168.128.12004 "17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557457363000 4 connectedb4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128lane 7002" 17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557457362000 5 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.9Fro7007007 master-0 1557457362729 0 connected6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130Rd 7005 "17005 master-0155745736395 connected 10923-16383192.168. 30.129 myself 7003 > CLUSTER MEET 192.168.30.130 7008OK192.168.30.129:7003 > CLUSTER NODES4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129 myself Master-0 1557457489000 3 connected 5461-1092280c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128 master 7001D 17001 master-01557457489000 1 connected 0-5460277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130V 7006 "17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557457489000 6 connectedb6331cbc986794237c83ed2d5c30777c1551546e 192.168.120.12004" 17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557457488000 4 connectedb4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128MAG 7002 "17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 155745748972 5 connected1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130V7489489259 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.9489107007007 master-01557457489000 0 connected6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30. 130 7005 master-0 1557457490475 5 connected 10923-16383

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

Change the identity of the node:

Change the identity of the newly added 192.168.30.130VR 7008 node to the slave of 192.168.30.129VR 7007

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

Cluster replicate is followed by node_id to change the identity of the corresponding node. You can also log in to the cluster to change

# redis-cli-c-h 192.168.30.130-p 7008-a 123456192.168.130Rover 7008 > CLUSTER REPLICATE e51ab166bc0f33026887bcf8eba0dff3d5b0bf14OK192.168.30.130:7008 > CLUSTER NODES277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130V 7006 "17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557458316881 3 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128VERI 7001001 master-01557458314864 1 connected 0-54604d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.9314864 1 connected 0-015574583160003 connected 5461-109226788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130Rose 700517005 master-01557458315872 5 connected 10923-19210872 .168.30.128: 7002 "17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458317890 5 connected1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130 virtual 7008" 17008 myself Slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458315000 7 connectedb6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557458315004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557458315000 1 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129Vera 700715007 master-01557458314000 0 connected

Looking at the corresponding nodes.conf file, you can find that there are changes, which record the node information of the current cluster.

# cat / data/redis/cluster/redis_7001/nodes-7001.conf1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130 slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 7008 "17008 slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458236169 7 connected6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130V 7005" 17005 master-0 1557458235000 5 connected 10923-16383277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130V 7006 "17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557458234103 6 connectedb4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128" 17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458235129 5 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128iz700114001 myself Master-0 1557458234000 1 connected 0-5460b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 5460b6331cbc986794237c83ed2d5c30777c1551546e 700454 "17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557458236000 4 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129Visa 7007" 17007 master-01557458236000 0 connected4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129virtual 7003 "17003 master-01557458233089 3 connected 5461-10922vars currentEpoch 7 lastVoteEpoch 0

Delete a node:

192.168.30.130 ERR I tried hard but I can't forget myself... 7008 > CLUSTER FORGET 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 (error) # unable to delete login node 192.168.30.130 ERR Can't forget my master 7008 > CLUSTER FORGET e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 (error) ERR Can't forget my master! # you cannot delete your own master node 192.168.30.130 master 7008 > CLUSTER FORGET 6788453ee9a8d7f72b1d45a9093838efd0e501f1OK # you can delete other master nodes 192.168.30.130V 7008 > CLUSTER NODES277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130V 7006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557458887328 3 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128R 7001@ 17001 master-0 1557458887000 1 connected 0-54604d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129 54604d74ec66e898bf09006dac86d4928f9fad81f373 7003 "17003 master-01557458886000 3 connected 5461-10922b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128Rule 7002" 17002 slave-01557458888351 5 connected1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130pur7008 "17008 myself Slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458885000 7 connectedb6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557458883289 1 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.129 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 7007 master-015574588853100 connected192.168.30.130:7008 > CLUSTER FORGET b4d3eb411a7355d4767c6c23b4df69fa183ef8bcOK # can delete other slave nodes 192.168.30.130Rd 7008 > CLUSTER NODES277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130Rd 7006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557459031397 3 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128Vol 7001mm 17001 master-01557459032407 connected 0-192.168 .30.129: 7003 "17003 master-0 1557459035434 3 connected 5461-109226788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130 109226788453ee9a8d7f72b1d45a9093838efd0e501f1 7005" 17005 master-01557459034000 5 connected 10923-163831a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130 109226788453ee9a8d7f72b1d45a9093838efd0e501f1 7008 "17008 myself Slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557459032000 7 connectedb6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557459034000 1 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 01557459034000 1 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129 Vera 700715007 master-01557459034427 0 connected

Save the configuration:

192.168.30.130 cat 7008 > CLUSTER SAVECONFIG # Save the node configuration information to the hard disk OK # cat / data/redis/cluster/redis_7001/nodes-7001.conf1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130V 7008 "17008 slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 01557458236169 7 connected6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130V 7005" 17005 master-01557458235000 5 connected 10923-16383277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130V 7006 "17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557458234103 6 connectedb4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.12815" 17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 01557458235129 5 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 7001mm 17001 myself Master-0 1557458234000 1 connected 0-5460b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129Vera 700400017004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557458236000 4 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129VOL7007007 master-01557458236000 0 connected4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.12VO7003 "17003 master-01557458233089 3 connected 5461-10922vars currentEpoch 7 lastVoteEpoch" redis-cli-c-h 192.168.30.130-p 7008-a 123456Warning: Using a password with 'a'or'- u' option on the command line interface may not be safe.192.168.30.130 : 7008 > CLUSTER NODES277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 7006 "17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557459500741 3 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128Vera 700114001 master-01557459500000 1 connected 0-54604d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129VOV 17003" 17003 master-015574595010003 connected 5461-109226788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130VOU700500017005 master-01557459500000 5 connected 10923-16383b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.8U7002mm 17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 155745499737 5 connected1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130700817008 myself Slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557459499000 7 connectedb6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 015574595017004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 01557459501750 1 connectede51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 7007 master-01557459498000 0 connected

As you can see, the previously deleted node is restored, because the corresponding configuration file is not deleted, and the CLUSTER SAVECONFIG restore is performed.

Simulate the master node to hang up:

192.168.30.128

# netstat-lntp | grep 7001tcp 00 192.168.30.128 grep 7001tcp 17001 0.0.0.0 grep 7001tcp 00 192.168.30.128 grep 7001tcp 7001 0.0.0.0 LISTEN 6701/redis-server 1 # kill 6701192.168.30.130 grep 7001tcp 7008 > CLUSTER NODES277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130 grep 7001tcp 7006 17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 01557461178000 3 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 7001mm 17001 master Fail-1557460950483 1557460947145 1 disconnected4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129purl 7003 "17003 master-01557461174922 3 connected 5461-109226788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130V 7005005005 master-01557461181003 5 connected 10923-16383b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128Ranger 7002" 17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 01557461179993 5 connected1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130U700817008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557461176000 7 connectedb6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.12918 7004 "17004 master-0155746117898 connected 0-5460e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.1230.9Rose 7007007 master-01557461179000 connected

For a row corresponding to 7001, you can see that master fail, with a status of disconnected; and a row corresponding to 7004, slave has become master.

Restart the 7001 node:

# redis-server / usr/local/redis/cluster/redis_7001.conf192.168.30.130:7008 > CLUSTER NODES277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130 CLUSTER NODES277daeb8660d5273b7c3e05c263f861ed5f17b92 7006 "17006" 17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557461307000 3 connected80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.128VOL7001 "17001 slave b6331cbc986794237c83ed2d5c30777c1551546e 01557461305441 8 connected4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129Rd 7003" 17003 master-01557461307962 3 connected 5461-109226788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130Rd 7005 "17005 master-01557461304935 5 connected 10923-16383b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.12815" 7002 "17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 01557461305 connected1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.13030 / 7008" 17008 myself Slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557461305000 7 connectedb6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129 connected 7004 17004 master-01557461308972 8 connected 0-5460e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129 slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 7007007 master-01557461307000 0 connected

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

In addition, if you test the 7007 nodes here, you will find that the 7008 nodes will not switch because there is no data on the 7007 nodes. The cluster data is divided into three parts. If 16384 slot are allocated by hash slot, the slot intervals borne by the three nodes are as follows:

Node 7004 covers 0mur5460 nodes 7003 covers 5461Mui 10922 nodes 7005 covers 10923Mel 16383 above is all the content of this article "sample Analysis of Redis clusters", thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report