Build a redis cluster with one master and two slaves

HairLossException 2022-01-26 11:28:38 阅读数:73

build redis cluster master slaves

One 、 build Redis colony

1.1、 Build three redis

Here we use three different ports to simulate three independent redis The server

First, create three redis.conf The configuration file :
Let's call them redis6379.conf、redis6380.conf、redis6381.conf

cp redis.conf redis6379.conf
cp redis.conf redis6380.conf
cp redis.conf redis6381.conf

 Insert picture description here
modify redis The configuration file , The following redis6380.conf For example ( The other two operations are the same )

port 6380
pidfile /var/run/redis_6380.pid
logfile 6380.log
dbfilename dump6380.rdb

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

1.2、 Three... Started separately redis service

redis-server redis6379.conf &
redis-server redis6380.conf &
redis-server redis6381.conf &

 Insert picture description here
 Insert picture description here
 Insert picture description here

1.3、redis The client connects to three servers respectively redis service

redis-cli -h 127.0.0.1 -p 6379
redis-cli -h 127.0.0.1 -p 6380
redis-cli -h 127.0.0.1 -p 6381

 Insert picture description here  Insert picture description here  Insert picture description here

1.4、 Set up master-slave relationship

Before the master-slave relationship is not set, each computer is set by default redis Servers are Master

Set never set the master

 stay 6380 On the implementation slaveof 127.0.0.1 6379 // Set up slave
stay 6381 On the implementation slaveof 127.0.0.1 6379 // Set up slave

 Insert picture description here
 Insert picture description here

1.5、 View master-slave relationship

info replication // View master-slave relationship

 Insert picture description here

here 6379 It's the main warehouse (Master)6380 and 6381 It's from the library (Slave)

  • Copy in full : Once the master-slave relationship is determined, the existing data in the master database will be automatically copied to the slave database
  • Incremental replication : The data written by the master library will be automatically synchronized to the slave library ( Read only from the library without writing )

Two 、 About downtime

2.1、 Host down

Here, the shutdown of the host service is used to simulate the host downtime

  • redis-cli -h 127.0.0.1 -p 6379 shutdown

View slave roles
 Insert picture description here
After restoring the host, check the master-slave relationship again
 Insert picture description here
Conclusion : The host is down and the slave is on standby After the host is restored, everything returns to normal

2.2、 From the machine down

It's closed here 6380 Service to simulate slave downtime

  • redis-cli -h 127.0.0.1 -p 6380 shutdown

View master-slave relationship
 Insert picture description here
After the slave is restored, check the master-slave relationship again
 Insert picture description here
Conclusion : After the slave goes down, other slaves will not be affected After the slave is restored, it will change back to the host, and the master-slave relationship needs to be reset

2.3、 Slave host

If the host fails to recover in a short time after downtime This database cluster can only perform read operations and cannot perform write operations At this time, it is necessary to replace the host computer from the host computer

// First, let the slave disconnect the original master-slave relationship
slaveof no one

 Insert picture description here

// And then in 6381 Reset the master-slave relationship
slaveof 127.0.0.1 6381

 Insert picture description here

copyright:author[HairLossException],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261128361444.html