1. Master-slave cluster
Redis
There are three cluster modes, namely: master-slave mode, sentinel mode, and Cluster
mode. Rdis
At the beginning, the master-slave mode was used as a cluster. If master
it crashes, it needs to be manually configured and slave
converted to master
; later, the sentinel mode was proposed for high availability. In this mode, there is a sentinel monitoring master
and can be automatically converted to the cluster slave
if it crashes , but it also has a problem. , that is, it cannot be dynamically expanded; so the cluster mode is proposed .master
slave
master
3.x
cluster
1.1. Introduction to master-slave mode
The 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
).
Among them, 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 replicated data flow is unidirectional and can only be replicated from the master node to the slave node.
- The slave database is generally read-only and receives data synchronized from the master database
- One
master
can have multipleslave
, but oneslave
can only correspond to onemaster
slave
Hanging does not affect otherslave
reads andmaster
writes. After restarting, the data willmaster
be synchronized frommaster
After hanging up,slave
the reading will not be affected, butredis
the writing service will no longer be provided. Aftermaster
restarting , the writing service will be provided to the outside world againredis
master
After hanging up,slave
a new one will not be selected in the nodemaster
1.2. Working Mechanism
When slave
started, it actively master
sends SYNC
commands to the server. master
After receiving SYNC
the command, save the snapshot ( RDB
persistence) in the background and cache the command for saving the snapshot during this period, and then send the saved snapshot file and the cached command to slave
. slave
After receiving the snapshot file and command, load the snapshot file and the cached execution command. After the replication is initialized, master
each received write command will be sent synchronously slave
to ensure the consistency of master-slave data.
2. Redis
Master-slave cluster construction
2.1. Cluster structure
The master-slave cluster structure built is shown in the figure:
There are three nodes in total, one master node and two slave nodes.
Here we will start 3 instances in the same virtual machine redis
to simulate a master-slave cluster. The information is as follows:
IP |
PORT |
Role |
---|---|---|
10.0.10.16 | 7001 | master |
10.0.10.16 | 7002 | slave |
10.0.10.16 | 7003 | slave |
2.2. Prepare instance and configuration
To start three instances on the same virtual machine, three different configuration files and directories must be prepared. The directory where the configuration files are located is also the working directory.
1) Create a directory
We create three folders named 7001, 7002, and 7003:
# 进入/home目录
cd /home
# 创建目录
mkdir 7001 7002 7003
As shown in the picture:
2) Restore the original configuration
Modify redis-6.2.4/redis.conf
the file, change the persistence mode to the default RDB
mode, and AOF
keep it closed.
# 开启RDB
# save ""
save 3600 1
save 300 100
save 60 10000
# 关闭AOF
appendonly no
3) Copy the configuration file to each instance directory
Then redis-6.2.4/redis.conf
copy the files to three directories ( /home
execute the following command in the directory):
# 方式一:逐个拷贝
cp redis-6.2.4/redis.conf 7001
cp redis-6.2.4/redis.conf 7002
cp redis-6.2.4/redis.conf 7003
# 方式二:管道组合命令,一键拷贝
echo 7001 7002 7003 | xargs -t -n 1 cp redis-6.2.4/redis.conf
4) Modify the port and working directory of each instance
Modify the configuration file in each folder, modify the ports to 7001, 7002, and 7003 respectively, and rdb
modify the file storage location to your own directory ( /home
execute the following commands in the directory):
sed -i -e 's/6379/7001/g' -e 's/dir .\//dir \/home\/7001\//g' 7001/redis.conf
sed -i -e 's/6379/7002/g' -e 's/dir .\//dir \/home\/7002\//g' 7002/redis.conf
sed -i -e 's/6379/7003/g' -e 's/dir .\//dir \/home\/7003\//g' 7003/redis.conf
5) Modify the declaration of each instanceIP
There are multiple virtual machines IP
. In order to avoid confusion in the future, we need to specify the binding information redis.conf
of each instance in the file . The format is as follows:ip
# redis实例的声明 IP
replica-announce-ip 10.0.10.16
Each directory needs to be changed, and we can complete the modification with one click ( /home
execute the following command in the directory):
# 逐一执行
sed -i '1a replica-announce-ip 10.0.10.16' 7001/redis.conf
sed -i '1a replica-announce-ip 10.0.10.16' 7002/redis.conf
sed -i '1a replica-announce-ip 10.0.10.16' 7003/redis.conf
# 或者一键修改
printf '%s\n' 7001 7002 7003 | xargs -I{} -t sed -i '1a replica-announce-ip 10.0.10.16' {}/redis.conf
2.3. Startup
In order to view the logs conveniently, we open 3 ssh
windows, start 3 redis
instances respectively, and start the command:
# 第1个
redis-server 7001/redis.conf
# 第2个
redis-server 7002/redis.conf
# 第3个
redis-server 7003/redis.conf
After startup:
If you want to stop with one key, you can run the following command (if there is an authentication password, this command will fail):
printf '%s\n' 7001 7002 7003 | xargs -I{} -t redis-cli -p {} shutdown
There is an authentication password command:
printf '%s\n' 7001 7002 7003 | xargs -I{} -t redis-cli -p {} shutdown -a 123456
If none of the above methods stop successfully redis
, you can use kill -9 PID
to kill redis
the process.
2.4. Open the master-slave relationship
Now the three instances have nothing to do with each other. To configure the master-slave, you can use replicaof
the or slaveof
( 5.0
formerly) command.
There are two modes, temporary and permanent:
-
Modify the configuration file (permanent)
redis.conf
Add a line of configuration in :replicaof <masterip> <masterport>
-
Use
redis-cli
the client to connect toredis
the service and executereplicaof
the command (it will fail after restart):replicaof <masterip> <masterport>
Note :5.0
The command will be added in the futurereplicaof
,salveof
which is consistent with the effect.
If master
there is password authentication, it needs to be enabled:
masterauth <master-password>
7002, 7003 modify the following configuration:
replicaof 10.0.10.16 7001
masterauth 123456
Then connect to node 7001 to view the cluster status:
# 连接 7001
redis-cli -p 7001
# 查看状态
info replication
result:
2.5. Testing
Do the following to test:
-
Using
redis-cli
connection 7001, executeset test 'wo ai beijing tian an men.'
-
Use
redis-cli
connection 7002, executeget test
, execute againset id 7002
-
Use
redis-cli
connection 7003, executeget test
, execute againset id 7003
It can be found that only node 7001 master
can perform write operations, and nodes 7002 and 7003 slave
can only perform read operations.