Redis configure sentinel mode

Install redis:

1 . Download the installation package

wget http://download.redis.io/releases/redis-5.0.7.tar.gz

2. Decompression

tar -zxvf redis-5.0.7.tar.gz

3. Compile

cd redis-5.0.7/ make

After the installation is complete, you can see subdirectories such as src and conf in the directory.

4. Installation

cd src/ make install

2. Deploy the master-slave architecture (one master and two slaves)

Since there are many files under src, we can copy several commonly used commands and conf configuration files for unified management, as follows:

1. Create a new bin directory and an etc directory

 cd /root/redis/redis-5.0.7/
 mkdir etc
 mkdir bin
 cd etc/
 mkdir redis_master
 mkdir redis_slave1
 mkdir redis_slave2

Description: redis_master as the directory of the master node configuration file redis_slave1 as the directory of the first slave node configuration file redis_slave2 as the destination of the second slave node configuration file

2. Go back to the installation directory and copy the configuration file to the corresponding directory

 cp redis.conf /root/redis/redis-5.0.7/etc/redis_master
 cp redis.conf /root/redis/redis-5.0.7/etc/redis_slave1
 cp redis.conf /root/redis/redis-5.0.7/etc/redis_slave2

3. Go to the src folder and copy the mkreleasehdr.sh, redis-benchmark, redis-check-aof, redis-check-rdb, redis-cli, redis-server, redis-sentinel files to the bin folder

 cd src/
 cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel /root/redis/redis-5.0.7/bin/

4. Start a redis service test to see if redis can start normally

 cd /root/redis/redis-5.0.7/bin
 ./redis-server ../etc/redis_master/redis.conf

Use the redis-cli command to connect to the client, as follows

After the test is feasible, close the redis service

 redis-cli -p 6379 shutdown

5. Modify each node configuration file separately

 cd etc/redis_master
 vim redis.conf
  • Master node configuration
 bind: 0.0.0.0               ##Redis only allows local access by default. Modifying bind to 0.0.0.0 means that all remote access is allowed. If you want to specify restricted access, you can set the corresponding ip.
 port: 6379   
 protected-mode: no                     ##Turn off protected mode, can be accessed externally
 daemonize: yes                             ##Set to start in the background
 pidfile "/var/run/redis_6379.pid"       ##redi pid storage location, it is recommended to add the port, which is easy to distinguish
 logfile "/root/redis/redis-5.0.7/bin/redis_6379.log"     ##Log file storage location
 requirepass: pwdtest@2019        ##Set redis connection password
 masterauth: pwdtest@2019         ##Password for slave service to connect to master
  • Slave node configuration

The configuration of the slave is similar to that of the host. The difference is that you need to use replicaof to specify the IP address and port of the master. It should be noted that the old version uses slaveof. At present, the 5.0.7 version I use uses replicaof, as shown below

 bind: 0.0.0.0
 port: 6378
 protected-mode: no
 daemonize: yes
 pidfile "/var/run/redis_6378.pid"
 logfile "/root/redis/redis-5.0.7/bin/redis_6378.log"
 requirepass: pwdtest@2019
 masterauth: pwdtest@2019
 replicaof 192.168.231.130 6379   ##IP port of the master node

replicaof 192.168.231.130 6379 Specifies that when the local machine is the slave service, set the IP address and port of the master service. When redis starts, it will automatically synchronize data with the master, so both slaves can be configured in this way.

Modify the configuration file of another slave node as usual, the port can be 6377

So far, the master-slave architecture has been configured

Three: master-slave verification
1. Start three nodes respectively

 cd bin/
 ./redis-server ../etc/redis_master/redis.conf
 ./redis-server ../etc/redis_slave1/redis.conf
 ./redis-server ../etc/redis_slave2/redis.conf

You can query the redis startup status through ps -ef|grep redis

2. Connect to a node to view the master-slave information

 ./redis-cli -h xx.xx.xx.xx -p 6379
 auth pwdtest@2019   ##Since a password is set, you need to log in first
 info replication
 ./redis-cli -h xx.xx.xx.xx -p 6378
 auth pwdtest@2019   ##Since a password is set, you need to log in first
 info replication

You can see the role of the current node and related information

3. Master-slave synchronization verification

Add a few pieces of data to the master node to see if the slave node can see it

 ./redis-cli -h xx.xx.xx.xx -p 6379
 auth pwdtest@2019   
 set name xiaoming
 get name
 ./redis-cli -h xx.xx.xx.xx -p 6377
 auth pwdtest@2019   
 get name

Conclusion: The data added by the master node can be viewed by the slave node

Four: Sentinel mode construction

Sentinel's configuration file is: sentinel.conf

1. Also for the convenience of management, copy sentinel.conf to the corresponding directory

 cp redis-5.0.7
 cp sentinel.conf etc/redis_master/
 cp sentinel.conf etc/redis_slave1/
 cp sentinel.conf etc/redis_slave2/

2. Edit sentinel.conf separately

The port of sentinel.conf corresponding to master node 6379 is 6376 The port of sentinel.conf corresponding to node 6378 of slave1 is 6375 The port of sentinel.conf corresponding to node 6377 of slave2 is 6374

 vim sentinel.conf
 port:6376                               ## The default port is 26379.
 protected-mode:no                       ## Turn off protected mode, can be accessed externally
 daemonize:yes                           ##Set to start in the background
 pidfile "/var/run/redis-sentinel_6376.pid"         ##redis sentinel pid file location
 logfile "/root/redis/redis-5.0.7/bin/sentinel_6376.log"          ##log file
 sentinel monitor mymaster 192.168.231.130 6379 2           ##Specify the host IP address and port, and specify that when two sentinels think that the host is down, the host will be switched for disaster recovery
 sentinel auth-pass mymaster pwdtest@2019             ##When requirepass is enabled in the Redis instance, the password needs to be provided here
 sentinel down-after-milliseconds mymaster 3000                      ##Here is how many seconds the host does not respond, it is considered to be hung up
 snetinel parallel-syncs mymaster 1              ##When switching between master and slave, how many slaves can synchronize the new master at the same time, here is set to 1 by default
 sentinel failover-timeout mymaster 180000       ##The timeout for failover, here is set to three minutes

3. Start three sentries

 cd bin/
 redis-sentinel ../etc/redis_master/sentinel.conf
 redis-sentinel ../etc/redis_slave1/sentinel.conf
 redis-sentinel ../etc/redis_slave2/sentinel.conf

4. View sentinel information

 ./redis-cli -h 121.36.xx.xx -p 6375
 info sentinel

Five, disaster recovery switch

1. Simulate host downtime: manually stop the master node

 ./redis-cli -h 121.36.xx.xx -p 6379
 auth pwdtest@2019
 shutdown

2. Connect to the original slave1 node to view the master-slave information, and you will find that the master node has automatically changed

 ./redis-cli -h xx.xx.xx.xx -p 6378
 auth pwdtest@2019   
 info replication

3. Restart the 6379 node and find that it has joined the master and slave again.

 ./redis-server ../etc/redis_master/redis.conf
 ./redis-cli -h xx.xx.xx.xx -p 6378
 auth pwdtest@2019   
 info replication

Tags: Redis

Posted by lauriedunsire on Wed, 07 Sep 2022 02:06:53 +0930