当前位置:网站首页>Redis replication cluster setup

Redis replication cluster setup

2022-07-23 10:28:00 Uncle flying against the wind

Preface

redis Replication cluster is a common cluster mode in development , This article demonstrates how to centos7 Build a quick redis Replication cluster ;

Environmental preparation

1、 be based on centos7 Server of the system ( Or cloud servers );

2、redis Installation package ;

 

Set up process

Due to resource constraints , This article will build on a server , Distinguish by different port numbers ;

1、 Upload redis Install the package to the specified directory ( And extract the )

 

2、 In the current directory , Create three directories

In the current directory , Create separate 7001,7002,7003 Three file directories

 

3、 take redis Unzip the package redis.conf The configuration files are copied to three directories

 

4、 Modify the default port number of each configuration file , Data storage directory

have access to sed Command to perform batch replacement modification

sed -i -e 's/6379/7001/g' -e 's/dir .\//dir \/tmp\/7001\//g' 7001/redis.conf
sed -i -e 's/6379/7002/g' -e 's/dir .\//dir \/tmp\/7002\//g' 7002/redis.conf
sed -i -e 's/6379/7003/g' -e 's/dir .\//dir \/tmp\/7003\//g' 7003/redis.conf

  After modification , We might as well check any configuration file ,

 

5、 Modify the declaration of each instance IP

There are multiple virtual machines IP, To avoid future chaos , Need to be in redis.conf Specify the binding of each instance in the file ip Information , The format is as follows :

replica-announce-ip At present IP

You can still edit in batch

sed -i '1a replica-announce-ip  At present IP' 7001/redis.conf
sed -i '1a replica-announce-ip  At present IP' 7002/redis.conf
sed -i '1a replica-announce-ip  At present IP' 7003/redis.conf

 

The modification is complete , You can confirm with the following command

 

The modification of the above configuration file is basically completed

6、 Cluster start

To start, respectively, 3 An example , The starting process is as follows :

7001 example :

 

7002 example :

 

7003 example :

 

If you want to stop with one key , You can run the following command :

printf '%s\n' 7001 7002 7003 | xargs -I{} -t redis-cli -p {} shutdown

7、 To configure 3 The master-slave relationship between instances

It's activated 3 An example , But there is no master-slave relationship between them , To configure master-slave, you can use replicaof perhaps slaveof(5.0 before ) command .

There are temporary and permanent modes :

  • Modify the configuration file ( permanent ), stay redis.conf Add a line of configuration : slaveof <masterip> <masterport>;
  • Use redis-cli The client connects to redis service , perform slaveof command ( Failure after restart ): slaveof <masterip> <masterport>;

Here is to show the effect , We use the second way to explain , adopt redis-cli Command connection 7002, Execute the following command :

redis-cli -p 7002

 

Then execute the following command

SLAVEOF 127.0.0.1 7001

The moment you execute the above command, you can see 7001 and 7002 Output information of two instance consoles , I see 7002 Completed the 7001 Connection and data synchronization  

Connect in the same way 7003 client , And perform the above operations

 

Connect 7001 client , perform info Command to view the cluster information , See the following message , It indicates that the cluster is successfully built

 

8、 Cluster testing

stay 7001 Connect the client and set a key, And then in 7002 Check above

  But if 7002 or 7003 above set One key It's a mistake , That is, the slave node does not have write permission ;

 

The problem summary

Many students upload and unzip the installation package in the first step , It is also configured correctly according to the configuration file , But the startup error , This makes a common sense mistake , Because at this time redis Compilation and installation have not been completed , Therefore, it is necessary to enter redis Unzip the package src, Under the table of contents , perform : make install command ;

原网站

版权声明
本文为[Uncle flying against the wind]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230354063460.html