redis cluster and sentinel mode -- 03

Keywords: Java Database MySQL

192.168.80.51Main db01
192.168.80.11From web04
192.168.80.9From web03

1. Cluster preparation

To realize master-slave, please back up the data first

First, a single node redis has been installed
Single node installation
https://blog.csdn.net/ncnhhm/article/details/121443981
Master node profile

daemonize yes 
bind  192.168.80.51
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
save 900 1
save 300 10
save 60 10000
dbfilename redis.rdb
dir /data/redis_6379/


Compress the directory for scp decompression and installation

cd /opt
tar cf   redis.tar.gz /opt/redis_cluster
scp redis.tar.gz 192.168.80.11:/opt

Decompression installation

tar xf redis.tar.gz 
cd redis_cluster/redis-5.0.7 
make install

web04 modify configuration file

daemonize yes 
bind  192.168.80.11
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
save 900 1
save 300 10
save 60 10000
dbfilename redis.rdb
dir /data/redis_6379/
slaveof 192.168.80.51 6379 

web03 modify configuration file

daemonize yes 
bind  192.168.80.9
port 6379
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
save 900 1
save 300 10
save 60 10000
dbfilename redis.rdb
dir /data/redis_6379/
slaveof 192.168.80.51 6379 

slaveof 192.168.80.51 6379 can only be configured from the node's configuration file
Just modify the ip address and add the master-slave configuration

Create data directory

mkdir -p /data/redis_6379/

start-up

redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf

Manually configure master-slave

Log in to the slave node
Enter your command

slaveof 192.168.80.51 6379

Disconnect master-slave (if slaveof is not configured in the configuration file, there will be no master-slave relationship after restart)

Stop from library
shutdown

pkill redis

Log in to the database,Execute the following command
slaveof  no one

Master slave replication process

tail -f /opt/redis_cluster/redis_6379/logs/redis_6379.log

Main library: db01

37139:M 29 Nov 2021 16:10:47.117 * Background saving terminated with success
37139:M 29 Nov 2021 16:10:47.118 * Synchronization with replica 192.168.80.11:6379 succeeded
37139:M 29 Nov 2021 16:10:55.052 * Replica 192.168.80.9:6379 asks for synchronization
37139:M 29 Nov 2021 16:10:55.052 * Full resync requested by replica 192.168.80.9:6379
37139:M 29 Nov 2021 16:10:55.052 * Starting BGSAVE for SYNC with target: disk
37139:M 29 Nov 2021 16:10:55.052 * Background saving started by pid 50724
50724:C 29 Nov 2021 16:10:55.056 * DB saved on disk
50724:C 29 Nov 2021 16:10:55.057 * RDB: 0 MB of memory used by copy-on-write
37139:M 29 Nov 2021 16:10:55.093 * Background saving terminated with success
37139:M 29 Nov 2021 16:10:55.094 * Synchronization with replica 192.168.80.9:6379 succeeded

From library: web03

8274:S 29 Nov 2021 16:10:55.024 * Connecting to MASTER 192.168.80.51:6379
8274:S 29 Nov 2021 16:10:55.025 * MASTER <-> REPLICA sync started
8274:S 29 Nov 2021 16:10:55.026 * Non blocking connect for SYNC fired the event.
8274:S 29 Nov 2021 16:10:55.028 * Master replied to PING, replication can continue...
8274:S 29 Nov 2021 16:10:55.029 * Partial resynchronization not possible (no cached master)
8274:S 29 Nov 2021 16:10:55.030 * Full resync from master: feef8abd4a1ed9e739b552f569f7b00c054c21c4:18564
8274:S 29 Nov 2021 16:10:55.072 * MASTER <-> REPLICA sync: receiving 1266 bytes from master
8274:S 29 Nov 2021 16:10:55.072 * MASTER <-> REPLICA sync: Flushing old data
8274:S 29 Nov 2021 16:10:55.072 * MASTER <-> REPLICA sync: Loading DB in memory
8274:S 29 Nov 2021 16:10:55.072 * MASTER <-> REPLICA sync: Finished with success

process

1.The slave node sends a synchronization request to the master node
2.After the master node receives the successful request
   2.1 Execute now bgsave Persist the data in the cache to disk
   2.2 Will persist rdb Files of kitchenware are sent to the slave node
3. After receiving the file from the node
   3.1 Empty your data
   3.2 Import of master node rdb Data to your own memory
4.Then there is the synchronization of real-time data

1. The slave node is read-only and not writable
2. The slave node will not fail over automatically. It will always synchronize with the master node
3. Manual intervention is required for master-slave replication failover
4. The slave node will clear its original data. If the synchronized object is written incorrectly, it will lead to data loss
5. The subsequent synchronization between the slave database and the master database depends on the SYNC protocol of redis, not the RDB file. The RDB file is only used when the synchronization is established for the first time.
6. Normal persistent files can also be from the library

2. sentinel mode setup

Sentinel mode is built on the basis of master-slave mode
effect:

1.Solve human intervention
2.Achieve high availability

Port number

Common cluster port number 6379
 Sentinel's port number 26379

2.1 deploy sentry nodes

All three

mkdir -p /data/redis_26379
mkdir -p /opt/redis_cluster/redis_26379/{conf,pid,logs}

cat >/opt/redis_cluster/redis_26379/conf/redis_26379.conf << EOF
bind $(ifconfig eth0|awk 'NR==2{print $2}')
port 26379
daemonize yes
logfile /opt/redis_cluster/redis_26379/logs/redis_26379.log
dir /data/redis_26379
sentinel monitor myredis 192.168.80.51 6379 2
sentinel down-after-milliseconds myredis 3000
sentinel parallel-syncs myredis 1
sentinel failover-timeout myredis 18000
EOF

explain

sentinel monitor mymaster 192.168.80.51 6379 2
mymaster Master node alias master node ip And port, judge the primary node failure, two sentinel Node consent

sentinel down-after-milliseconds mymaster 3000
 Option specifies Sentinel The number of milliseconds it takes to assume that the server is disconnected.

sentinel parallel-syncs mymaster 1
 The number of slave nodes initiating replication operations to the new master node, and 1 polling initiates replication

sentinel failover-timeout mymaster 180000
 Failover timeout

Start cluster

redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf

Start the sentry

redis-sentinel /opt/redis_cluster/redis_26379/conf/redis_26379.conf

verification

redis-cli -h 192.168.80.51 -p 26379 Sentinel get-master-addr-by-name myredis
redis-cli -h 192.168.80.11 -p 26379 Sentinel get-master-addr-by-name myredis
redis-cli -h 192.168.80.9 -p 26379 Sentinel get-master-addr-by-name myredis
[root@db01 conf]# redis-cli -h 192.168.80.51 -p 26379 Sentinel get-master-addr-by-name myredis
1) "192.168.80.51"
2) "6379"

[root@web03 /]# redis-cli -h 192.168.80.9 -p 26379 Sentinel get-master-addr-by-name myredis
1) "192.168.80.51"
2) "6379"

Changes to configuration files 26379

bind 192.168.80.51
port 26379
daemonize yes
logfile "/opt/redis_cluster/redis_26379/logs/redis_26379.log"
dir "/data/redis_26379"
sentinel myid c108932796b940f546d2ac25648eecd41e80146d
sentinel deny-scripts-reconfig yes
sentinel monitor myredis 192.168.80.51 6379 2
sentinel down-after-milliseconds myredis 3000
# Generated by CONFIG REWRITE
protected-mode no
sentinel failover-timeout myredis 18000
sentinel config-epoch myredis 0
sentinel leader-epoch myredis 0
sentinel known-replica myredis 192.168.80.11 6379
sentinel known-replica myredis 192.168.80.9 6379
sentinel known-sentinel myredis 192.168.80.9 26379 e765086886822b35ca79c5559d80367f62562967
sentinel known-sentinel myredis 192.168.80.11 26379 d07a13189c81c052cac647b274b28073dbb83acd
sentinel current-epoch 0

It will be added automatically

2.2 fault simulation (stop the master node)

pkill redis

Profile change

tail -f /opt/redis_cluster/redis_26379/logs/redis_26379.log 
10201:X 29 Nov 2021 16:58:24.083 # +sdown sentinel c108932796b940f546d2ac25648eecd41e80146d 192.168.80.51 26379 @ myredis 192.168.80.51 6379
10201:X 29 Nov 2021 16:58:24.155 # +sdown master myredis 192.168.80.51 6379
10201:X 29 Nov 2021 16:58:24.298 # +new-epoch 1
10201:X 29 Nov 2021 16:58:24.299 # +vote-for-leader d07a13189c81c052cac647b274b28073dbb83acd 1
10201:X 29 Nov 2021 16:58:25.301 # +odown master myredis 192.168.80.51 6379 #quorum 2/2
10201:X 29 Nov 2021 16:58:25.301 # Next failover delay: I will not start a failover before Mon Nov 29 16:59:00 2021
10201:X 29 Nov 2021 16:58:25.390 # +config-update-from sentinel d07a13189c81c052cac647b274b28073dbb83acd 192.168.80.11 26379 @ myredis 192.168.80.51 6379
10201:X 29 Nov 2021 16:58:25.390 # +switch-master myredis 192.168.80.51 6379 192.168.80.9 6379
10201:X 29 Nov 2021 16:58:25.392 * +slave slave 192.168.80.11:6379 192.168.80.11 6379 @ myredis 192.168.80.9 6379
10201:X 29 Nov 2021 16:58:25.392 * +slave slave 192.168.80.51:6379 192.168.80.51 6379 @ myredis 192.168.80.9 6379
10201:X 29 Nov 2021 16:58:28.408 # +sdown slave 192.168.80.51:6379 192.168.80.51 6379 @ myredis 192.168.80.9 6379

You can see that the primary node becomes 192.168.80.9

[root@web03 /]# redis-cli -h 192.168.80.9 -p 26379 Sentinel get-master-addr-by-name myredis
1) "192.168.80.9"
2) "6379"

Turn on master node

redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf
redis-sentinel /opt/redis_cluster/redis_26379/conf/redis_26379.conf

It will be automatically added to the cluster at this time

53853:X 29 Nov 2021 17:01:15.168 # +config-update-from sentinel d07a13189c81c052cac647b274b28073dbb83acd 192.168.80.11 26379 @ myredis 192.168.80.51 6379
53853:X 29 Nov 2021 17:01:15.169 # +switch-master myredis 192.168.80.51 6379 192.168.80.9 6379
53853:X 29 Nov 2021 17:01:15.171 * +slave slave 192.168.80.11:6379 192.168.80.11 6379 @ myredis 192.168.80.9 6379
53853:X 29 Nov 2021 17:01:15.171 * +slave slave 192.168.80.51:6379 192.168.80.51 6379 @ myredis 192.168.80.9 6379

Question: at this time, db01 is no longer the master server, but web03. How to solve it and switch between master and slave

Query command:CONFIG GET slave-priority
 set command:CONFIG SET slave-priority 0
 Active switching:sentinel failover myredis

The default weight is 100. At this time, we set the other two to 0
Then, the master-slave switch is performed on the one to be set as the host
After switching, remember to restore the weight so that the next time there is a damaged machine, you can select the master

View of weights

redis-cli -h 192.168.80.9 -p 6379 CONFIG GET slave-priority 
redis-cli -h 192.168.80.11 -p 6379 CONFIG GET  slave-priority 
redis-cli -h 192.168.80.51  -p 6379 CONFIG GET  slave-priority 

Specific operation

redis-cli -h 192.168.80.9 -p 6379 CONFIG SET slave-priority 0
redis-cli -h 192.168.80.11 -p 6379 CONFIG SET slave-priority 0
redis-cli -h 192.168.80.51 -p 26379 sentinel failover myredis

Verify that the original master node is restored

redis-cli -h 192.168.80.51 -p 26379 Sentinel get-master-addr-by-name myredis

configuration file

bind 192.168.80.51
port 26379
daemonize yes
logfile "/opt/redis_cluster/redis_26379/logs/redis_26379.log"
dir "/data/redis_26379"
sentinel myid c108932796b940f546d2ac25648eecd41e80146d
sentinel deny-scripts-reconfig yes
sentinel monitor myredis 192.168.80.51 6379 2
sentinel down-after-milliseconds myredis 3000
# Generated by CONFIG REWRITE
protected-mode no
sentinel failover-timeout myredis 18000
sentinel config-epoch myredis 2
sentinel leader-epoch myredis 2
sentinel known-replica myredis 192.168.80.9 6379
sentinel known-replica myredis 192.168.80.11 6379
sentinel known-sentinel myredis 192.168.80.11 26379 d07a13189c81c052cac647b274b28073dbb83acd
sentinel known-sentinel myredis 192.168.80.9 26379 e765086886822b35ca79c5559d80367f62562967
sentinel current-epoch 2

Restore weight

redis-cli -h 192.168.80.9 -p 6379 CONFIG SET slave-priority 100
redis-cli -h 192.168.80.11 -p 6379 CONFIG SET slave-priority 100

Posted by Grunt on Mon, 29 Nov 2021 02:17:48 -0800