Redis sentinel mode installation and deployment

Redis sentinel mode installation and deployment

What is a sentinel?

Sentinel is actually a redis server program. It also executes the serverCron function regularly, but it is not used by other programs in it. It uses the monitoring of ordinary redis nodes and the failover module.

When Sentinel is initialized, it will clear the original command table and write its own unique commands into it. Therefore, the data read and write commands supported by ordinary redis nodes cannot find commands for Sentinel because it does not initialize these commands at all. Of actuators.

Sentinel will periodically execute the info command on the master it monitors to obtain the latest master-slave relationship. It will also periodically send a ping heartbeat detection command to all redis nodes. If it detects that a master cannot respond, it will send it to other Sentinel sends a message, subjectively thinking that the master is down. If the Sentinel cluster agrees that the number of offline masters reaches a certain value, then everyone agrees to go offline and the master is offline.

What you need to do before going offline is to find one of the Sentinel clusters to perform the offline operation. This step is called leader election. After the election, a suitable one will be selected from all the slave nodes of the master as the new master, and let The other slaves resynchronize with the new master.

In fact, above we briefly introduced what Sentinel is and what it does in essence. Later we will elaborate on the details of the implementation in conjunction with the source code. Here we look again, how to configure and start a Sentinel monitoring. (The production environment is recommended to configure more than three

Sentinel mode configuration

  1. Prepare three machines or three redis instances
hadoop001 hadoop002 hadoop3 分别安装 redis

hadoop001:192.168.52.50
hadoop002:192.168.52.52
hadoop003:192.168.52.53

Set hadoop001 as the master node, hadoop002 as the slave node, and hadoop003 as the sentinel node

  1. Edit the configuration file of the master node: cp redis.conf master.conf
daemonize yes  #设置redis在后台启动
port 16379
slave-read-only no
protected-mode no
  1. Edit the configuration file of the slave node: cp redis.conf slave.conf
daemonize yes  # 设置redis在后台启动)
port 26379
slaveof 192.168.52.50 16379
slave-read-only no
protected-mode no
  1. The sentinel node edit configuration file: sentinel.conf
protected-mode no
daemonize yes  # 设置redis在后台启动)
port 26380
sentinel monitor master1 192.168.52.50 16379 1
sentinel down-after-milliseconds master1 5000
sentinel failover-timeout master1 900000
sentinel parallel-syncs master1 1
  1. Other configuration item information can be modified by yourself or the default
protected-mode no                                    (关闭保护模式)

port 6379                    
daemonize yes                                        (设置redis在后台启动)

pidfile /var/run/redis_6379.pid              
logfile "/opt/module/redis/6379/logs/redis_6379.log"  (设置日志文件的存放位置) 

dbfilename dump_6379.rdb                       		 (设置dump文件名)
dir "/opt/module/redis/6379"                         (设置dump文件和nodes文件的存放位置)               

masterauth bigdata                                    (设置集群节点间访问密码,跟上面一致)
requirepass bigdata                                   (设置redis访问密码)

Start the redis service

  1. Start Master
redis-server master.conf
  1. Start Slave
redis-server slave.conf 
  1. Start Sentineld
redis-sentinel sentinel.conf

4. Connect to the redis client to view configuration information

redis-cli -p 16379

[root@redis01 src]# redis-cli -p 16379
127.0.0.1:16379> INFO replication
# Replication
role:master   ---------------------> 角色为master
connected_slaves:1   ---------------------> slave 连接数为1
slave0:ip=172.31.17.229,port=26379,state=online,offset=183125,lag=0
master_replid:a7e8d31a7cca92f659dc1af9d6556e2d039588b8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:183125
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:183125
[root@redis02 redis-sentinel]# redis-cli -p 26379
127.0.0.1:26379> INFO replication
# Replication
role:slave   ---------------------> 角色为slave
master_host:172.31.31.130
master_port:16379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:185981
slave_priority:100
slave_read_only:0
connected_slaves:0
master_replid:a7e8d31a7cca92f659dc1af9d6556e2d039588b8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:185981
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:71
repl_backlog_histlen:185911

Test Redis is available

Set the value on the master node, and then check whether it can be synchronized on the slave node

127.0.0.1:16379[1]> set test6 666

127.0.0.1:16379[1]> keys *
1) "test6"
2) "test5"
3) "test1"
4) "test3"
127.0.0.1:26379[1]> keys *
1) "test1"
2) "test5"
3) "test3"
4) "test6"

127.0.0.1:26379[1]> get test6
"666"

Guess you like

Origin blog.csdn.net/qq_43081842/article/details/114290215