Redis cluster installation master-slave cluster

1. Master-slave cluster

RedisThere are three cluster modes, namely: master-slave mode, sentinel mode, and Clustermode. RdisAt the beginning, the master-slave mode was used as a cluster. If masterit crashes, it needs to be manually configured and slaveconverted to master; later, the sentinel mode was proposed for high availability. In this mode, there is a sentinel monitoring masterand can be automatically converted to the cluster slaveif it crashes , but it also has a problem. , that is, it cannot be dynamically expanded; so the cluster mode is proposed .masterslavemaster3.xcluster

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 mastercan have multiple slave, but one slavecan only correspond to onemaster
  • slaveHanging does not affect other slavereads and masterwrites. After restarting, the data will masterbe synchronized from
  • masterAfter hanging up, slavethe reading will not be affected, but redisthe writing service will no longer be provided. Aftermaster restarting , the writing service will be provided to the outside world againredis
  • masterAfter hanging up, slavea new one will not be selected in the nodemaster

1.2. Working Mechanism

When slavestarted, it actively mastersends SYNCcommands to the server. masterAfter receiving SYNCthe command, save the snapshot ( RDBpersistence) 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. slaveAfter receiving the snapshot file and command, load the snapshot file and the cached execution command. After the replication is initialized, mastereach received write command will be sent synchronously slaveto ensure the consistency of master-slave data.

2. RedisMaster-slave cluster construction

2.1. Cluster structure

The master-slave cluster structure built is shown in the figure:

insert image description here

There are three nodes in total, one master node and two slave nodes.

Here we will start 3 instances in the same virtual machine redisto 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:

insert image description here

2) Restore the original configuration

Modify redis-6.2.4/redis.confthe file, change the persistence mode to the default RDBmode, and AOFkeep it closed.

# 开启RDB
# save ""
save 3600 1
save 300 100
save 60 10000

# 关闭AOF
appendonly no

insert image description here

insert image description here

3) Copy the configuration file to each instance directory

Then redis-6.2.4/redis.confcopy the files to three directories ( /homeexecute 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

insert image description here

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 rdbmodify the file storage location to your own directory ( /homeexecute 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

insert image description here

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.confof 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 ( /homeexecute 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

insert image description here

2.3. Startup

In order to view the logs conveniently, we open 3 sshwindows, start 3 redisinstances 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:

insert image description here

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

insert image description here
If none of the above methods stop successfully redis, you can use kill -9 PIDto kill redisthe 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 replicaofthe or slaveof( 5.0formerly) command.

There are two modes, temporary and permanent:

  • Modify the configuration file (permanent)

    • redis.confAdd a line of configuration in :replicaof <masterip> <masterport>
  • Use redis-clithe client to connect to redisthe service and execute replicaofthe command (it will fail after restart):

    replicaof <masterip> <masterport>
    

Note :5.0The command will be added in the futurereplicaof,salveofwhich is consistent with the effect.

If masterthere 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:

insert image description here

2.5. Testing

Do the following to test:

  • Using redis-cliconnection 7001, executeset test 'wo ai beijing tian an men.'

  • Use redis-cliconnection 7002, execute get test, execute againset id 7002

  • Use redis-cliconnection 7003, execute get test, execute againset id 7003

insert image description here

It can be found that only node 7001 mastercan perform write operations, and nodes 7002 and 7003 slavecan only perform read operations.

insert image description here

Guess you like

Origin blog.csdn.net/qq_37726813/article/details/130796819