Sentinel mode of Redis cluster scheme (high availability) (one master, two slave and three sentinels)

Sentinel mode of Redis cluster scheme (high availability) (one master, two slave and three sentinels)

Linux: conts7

Redis edition: 5.0.14
    Link to the download page on the official website: https://redis.io/download

Third party Redis Recommended connection tools: RedisDesktopManager
    Download from the official website: https://redisdesktop.com/download

1. Introduction to sentry of one master, two slave and three

One master, two slave and three sentinels, one master master node and two slave nodes. Sentinel sentinel mode is configured for all three Redis. When the master node goes down, a new master node is re selected through the sentinel to ensure the availability of the cluster.

Main functions of sentry:

1.Cluster monitoring: responsible for monitoring Redis master and slave Whether the process is working properly.
2.Message notification: if a Redis If the instance fails, the sentry is responsible for sending a message as an alarm notification to the administrator.
3.Failover: if master node If you hang up, it will be automatically transferred to slave node Come on.
4.Configuration center: Notify If failover occurs client Client NEW master Address.
PS: According to the recommendation mechanism, the number of sentinels in the cluster is preferably odd(3,5....)

Advantages and disadvantages of one master, two slave and three sentinels:

Advantages: it can meet high availability and use the least server resources under the condition of high availability (compared with master-slave and cluster modes)
Disadvantages: but the election will be interrupted. Difficult to scale and performance bottleneck.--(The cached data on each machine is the same (subject to the machine configuration of a single machine)

2 one master, two slave and three Sentinels

Environmental Science:

IP addressportroleRedis version
192.168.2.2036379redis-master,sentinel5.0.14
192.168.2.2056379redis-slave1,sentinel5.0.14
192.168.2.2066379redis-slave2,sentinel5.0.14

For Redis installation, see: [Linux Installation Tutorial]( https://github.com/zlk-github/common-test/blob/master/common-redis-test/README-INIT.md#   Linux Installation (Redis tutorial)

2.1 configure the Redis master node

2.1.1 main Redis configuration file redis.conf

vi redis.conf

redis.conf is modified as follows:

bind 0.0.0.0            # Indicates that redis allows all addresses to connect. The default is 127.0.0.1. Only local connections are allowed. You can also bind 192.168.2.203
daemonize yes             # Allow redis to run in the background
logfile "/var/log/redis.log"    # Set redis log storage path
requirepass "123456"        # Set redis password
protected-mode no      # Set to no to allow external network access
port 6376             # Modify redis listening port (customizable)
pidfile /var/run/redis.pid  # pid storage directory
dir /usr/local/redis/redis-5.0.14/tmp   # Working directory. You need to create a directory, which can be customized
masterauth 123456    # Password of master slave synchronization master

2.1.2 the master Redis modifies sentinel configuration

vi sentinel.conf

sentinel.conf is modified as follows:

port 2700 # Modify Sentinel listening port
daemonize yes  # Allow Sentinel to run in the background     
logfile "/var/log/redis-sentinel.log"   # Set Sentinel log storage path
dir /usr/local/redis/redis-5.0.14/tmp   # Working directory. You need to create a directory, which can be customized

# redis01: the master name can be customized
# 192.168.2.203 6379: redis master node IP and port
# 2: Indicates how many Sentinel nodes consider the redis master node to be invalid
sentinel monitor redis01 192.168.2.203 6379 2    # Sentinel listens to the redis master node

# When configuring the expiration time, the master will be subjectively considered unavailable by the sentinel, in milliseconds      
sentinel down-after-milliseconds redis01 10000
        
# If sentinel fails to complete the master/slave automatic switching within the configured value, it is considered that this failover has failed.        
sentinel failover-timeout redis01 60000

# How many slave s can synchronize the new master at the same time when a failover active / standby switch occurs.
sentinel parallel-syncs redis01 2

# Set the password when connecting the master and slave. Note that sentinel cannot set different passwords for the master and slave respectively, so the passwords of the master and slave should be the same
sentinel auth-pass redis01 123456

Note: Note: configurations containing mymaster must be placed after sentinel monitor mymaster 192.168.2.203 6379 2, otherwise problems will occur

2.2 configuring Redis slave nodes

Slave node 1 (redis slave1) and slave node 2 (redis slave1)

2.2.1 from the node Redis configuration file redis.conf

Copy the redis.conf of the master node and modify the replicaof and bind.

redis-slave1 corresponds to redis.conf

# Indicates that redis allows all addresses to connect. The default is 127.0.0.1. Only local connections are allowed. You can also bind 192.168.2.205 
bind 0.0.0.0            
# The primary library is the address of the primary virtual machine 1
replicaof 192.168.2.203 6379

redis-slave2 corresponds to redis.conf

# Indicates that redis allows all addresses to connect. The default is 127.0.0.1. Only local connections are allowed. You can also bind 192.168.2.205 
bind 0.0.0.0            
# The primary library is the address of the primary virtual machine 1
replicaof 192.168.2.203 6379

2.2.2 modify sentinel configuration from Redis

Just copy sentinel.conf of the master node. If necessary, you can change the local id of the bind slave node

# Indicates that redis allows all addresses to connect. The default is 127.0.0.1. Only local connections are allowed. You can also bind 192.168.2.205 
bind 0.0.0.0  

3. Start Redis

3.1 setting Redis startup

Add the Redis startup and configuration path to the system / etc/rc.d/rc.local and save it.

Enter edit: vim /etc/rc.d/rc.local
    Add the following( redis of bin Directory startup related profile directory):
    /usr/local/redis/redis-5.0.14/bin/redis-server  /usr/local/redis/redis-5.0.14/etc/redis.conf
    /usr/local/redis/redis-5.0.14/bin/redis-sentinel  /usr/local/redis/redis-5.0.14/etc/sentinel.conf

3.2 set up soft links to facilitate service startup

ln -s /usr/local/redis/redis-5.0.14/bin/redis-server /usr/bin/redis-server
ln -s /usr/local/redis/redis-5.0.14/bin/redis-cli /usr/bin/redis-cli
ln -s /usr/local/redis/redis-5.0.14/bin/redis-sentinel /usr/bin/redis-sentinel

3.2 Redis cluster startup

get into bin catalogue(No need to set up soft applications):  cd /usr/local/redis/redis-5.0.14/bin

start-up Redis colony
1.start-up Redis,Sequential master->from
redis-server /usr/local/redis/redis-5.0.14/etc/redis.conf

2.start-up Sentinel,Sequential master->from
redis-sentinel /usr/local/redis/redis-5.0.14/etc/sentinel.conf

4. Access & verify Redis cluster

4.1 accessing the redis master node

get into bin catalogue(No need to set up soft applications):  cd /usr/local/redis/redis-5.0.14/bin

get into redis-cli:  redis-cli -h 127.0.0.1 -p 6379 -a 123456

View clusters: info replication

The results of master cluster are as follows:

  [root@localhost bin]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
  Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  127.0.0.1:6379> info replication
  # Replication
  # Personal note: the current node is the primary node
  role:master
  # Personal note: there are 2 slave nodes, 192.168.2.205 6379 and 192.168.2.206 6379
  connected_slaves:2 
  slave0:ip=192.168.2.206,port=6379,state=online,offset=167962,lag=0
  slave1:ip=192.168.2.205,port=6379,state=online,offset=167684,lag=1
  master_replid:f259bbbd3b0b0c3439460c9e2f666dc68ac6166c
  master_replid2:0000000000000000000000000000000000000000
  master_repl_offset:167962
  second_repl_offset:-1
  repl_backlog_active:1
  repl_backlog_size:1048576
  repl_backlog_first_byte_offset:1
  repl_backlog_histlen:167962

4.2 accessing redis slave node 1 (redis slave 1)

get into bin catalogue(No need to set up soft applications):  cd /usr/local/redis/redis-5.0.14/bin

get into redis-cli:  redis-cli -h 127.0.0.1 -p 6379 -a 123456

View clusters: info replication

The results of redis-slave1 cluster are as follows:

127.0.0.1:6379> info replication
# Replication
 # Personal note: the current node is a slave node
role:slave
# Personal note: master node 192.168.2.203 6379
master_host:192.168.2.203
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:249823
slave_priority:100
# Personal notes: read only from node
slave_read_only:1
connected_slaves:0
master_replid:f259bbbd3b0b0c3439460c9e2f666dc68ac6166c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:249823
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:249823

4.3 accessing redis slave node 2 (redis-slave 2)

get into bin catalogue(No need to set up soft applications):  cd /usr/local/redis/redis-5.0.14/bin

get into redis-cli:  redis-cli -h 127.0.0.1 -p 6379 -a 123456

View clusters: info replication

The results of redis-slave2 cluster are as follows:

127.0.0.1:6379> info replication
# Replication
 # Personal note: the current node is a slave node
role:slave
# Personal note: master node 192.168.2.203 6379
master_host:192.168.2.203
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:291664
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:f259bbbd3b0b0c3439460c9e2f666dc68ac6166c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:291664
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:183
repl_backlog_histlen:291482

4.4 slave node read only test (redis-slave 2)

Sentinel mode: the slave node is allowed to read but not write.

127.0.0.1:6379> set key 123456
(error) READONLY You can't write against a read only replica.

4.5 master node write, copy to slave node

redis-master

127.0.0.1:6379> set key1001 600
OK

redis-slave1

127.0.0.1:6379> get key1001
"600"

redis-slave2

127.0.0.1:6379> get key1001
"600"

4.6 verifying Redis failover

4.6.1 stop the master node 192.168.2.203 (redis Master)

Enter Directory:  cd /usr/local/redis/redis-5.0.14/bin

Stop service (password 123456): redis-cli -a 123456 shutdown    (Note: do not use kill -9 PID,It may cause data loss during backup)

4.6.2 viewing master nodes (redis-slave1 and redis-slave2)

Original redis-slave1:

get into bin catalogue(No need to set up soft applications):  cd /usr/local/redis/redis-5.0.14/bin

get into redis-cli:  redis-cli -h 127.0.0.1 -p 6379 -a 123456

View clusters: info replication

As a result, 192.168.2.205 (the original slave node) becomes the master node. Only 192.168.2.206 is left from the node. The original master node 192.168.2.203 was eliminated.

192.168.2.205 cluster information.

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.2.206,port=6379,state=online,offset=18702889,lag=1
master_replid:10dc8efe5dca92037d2cc945fd16a76981afec85
master_replid2:f259bbbd3b0b0c3439460c9e2f666dc68ac6166c
master_repl_offset:18702889
second_repl_offset:18664112
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:17654314
repl_backlog_histlen:1048576

192.168.2.206 cluster information:

get into bin catalogue(No need to set up soft applications):  cd /usr/local/redis/redis-5.0.14/bin

get into redis-cli:  redis-cli -h 127.0.0.1 -p 6379 -a 123456

View clusters: info replication

As a result, 192.168.2.205 (formerly slave node 1) becomes the master node. Slave node 192.168.2.206 or slave node 2.

192.168.2.206 cluster information.

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.2.205
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:18826183
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:10dc8efe5dca92037d2cc945fd16a76981afec85
master_replid2:f259bbbd3b0b0c3439460c9e2f666dc68ac6166c
master_repl_offset:18826183
second_repl_offset:18664112
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:17777608
repl_backlog_histlen:1048576

4.6.1 start 192.168.2.203 (the original primary node redis Master)

get into bin catalogue(No need to set up soft applications):  cd /usr/local/redis/redis-5.0.14/bin

Start service: redis-server /usr/local/redis/redis-5.0.14/etc/redis.conf

get into redis-cli:  redis-cli -h 127.0.0.1 -p 6379 -a 123456

View clusters: info replication

192.168.2.203 (the original master node) is now a slave node. The master node changes from 192.168.2.206 (the original slave node).

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.2.205
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:18780158
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:10dc8efe5dca92037d2cc945fd16a76981afec85
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:18780158
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:18771309
repl_backlog_histlen:8850

3. Springboot 2.0 integrates one master, two slave and three sentinels

reference resources

Redis colony https://www.cnblogs.com/yufeng218/p/13688582.html

Redis Official website https://www.redis.net.cn/tutorial/3501.html || https://redis.io/download

Redis Source address: https://github.com/redis/redis

see: https://www.jianshu.com/p/e71c5a3a7162

Keywords: Linux Redis Spring Boot

Added by testtesttesttest on Thu, 28 Oct 2021 16:07:39 +0300