Redis one master, two slaves and three sentinels

Redis one master, two slaves and three sentinels

When you use Redis as a master-slave replication architecture and want to automatically perform failover when the master node fails, the one-master-slave three-sentinel mode is suitable. This architecture can improve system availability and fault tolerance.
The main components of sentinel mode include

主服务器(Master): Responsible for processing write operations and responding to client read and write requests.

从服务器(Slave): Replicates the data of the master server, is responsible for read operations, and takes over the role of the master server when the master server fails.

哨兵节点(Sentinel): Monitor the status of the master server and slave servers, perform fault detection, and automatically perform failover and failover when the master server fails. In a master-slave
three-sentinel mode, there is one master node and multiple slave nodes, and at the same time There are three sentinel nodes coming 监控主节点的健康状态. When the master node fails, the sentinel node will use 自动选举a slave node as the new master node and switch other slave nodes to the new master node. This process is automatic 不需要手动干预.

as shown in the picture

image-20230720165608782

Environment configuration

Host system IP address Installation package
Master node Centos7.9.2009 192.168.200.10 redis-7.0.9.tar.gz
Slave1node Centos7.9.2009 192.168.200.20 redis-7.0.9.tar.gz
Slave2node Centos7.9.2009 192.168.200.30 redis-7.0.9.tar.gz

purpose achieved: Install and start the Redis service on the three nodes. Configure a password for Redis access, and set the password to 123456. Then configure these three Redis nodes into Redis's one master, two slaves and three sentinel architecture, that is, one Redis master node, two slave nodes, and all three nodes are sentinel nodes.

practice

Configure master and slave

(1) Modify the host name (all nodes operate)

[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]# 
[root@localhost ~]# hostnamectl set-hostname slave1
[root@localhost ~]# bash
[root@slave1 ~]# 
[root@localhost ~]# hostnamectl set-hostname slave2
[root@localhost ~]# bash
[root@slave2 ~]# 

(2) Install redis (all nodes are executed)

# 安装工具包和编译依赖
yum install -y gcc wget net-tools vim  tree
# 下载软件包
wget  https://download.redis.io/releases/redis-7.0.9.tar.gz
# 解压
tar -zxf redis-7.0.9.tar.gz  -C /opt/
# 编译安装
cd /opt/redis-7.0.9/ && make && make install
# 搜索一下redis-server在哪,一般都在/usr/local/bin/ 下面
find / -name redis-server
# 复制一下所需要的配置文件到工作目录
cd /usr/local/bin/ && cp  -rf /opt/redis-7.0.9/redis.conf ./

(3) Modify the configuration file redis.conf (modify all nodes)

vim redis.conf
# 修改如下信息
bind 0.0.0.0   
protected-mode no
daemonize yes  
replicaof 192.168.200.10 6379
requirepass 123456 
masterauth 123456
# replicaof 选项Master节点不配置,从节点配置

(4) Start the redis service and release the port (modify all nodes)

redis-server redis.conf      # 启动redis
netstat -tlnp |grep redis    # 查看是否开放了6379端口
firewall-cmd --add-port=6379/tcp --permanent && firewall-cmd --reload   # 防火墙放行6379

(5) Log in to redis test

[root@master bin]# redis-cli -h 192.168.200.10 -p 6379 -a 123456   # 连接master节点
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.200.10:6379> ping
PONG
192.168.200.10:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.200.20,port=6379,state=online,offset=70,lag=1
slave1:ip=192.168.200.30,port=6379,state=online,offset=70,lag=1
master_failover_state:no-failover
master_replid:c486a9c578d65459edb9d496220ac11d1f7d790e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70

Configure sentry mode

(1) Exit the redis environment first

127.0.0.1:6379> quit

(2) Configure sentinel mode

[root@master bin]# vim sentinel.conf 
# 添加如下内容
daemonize yes
sentinel monitor master 192.168.200.10 6379 2
sentinel auth-pass master 123456
logfile "/var/log/sentinel.log"

# 再将配置的内容远程传输到20 30主机上
[root@master bin]# scp sentinel.conf 192.168.200.20:/usr/local/bin/sentinel.conf
[root@master bin]# scp sentinel.conf 192.168.200.30:/usr/local/bin/sentinel.conf

(3) Enable sentinel mode and release the default port 26379

redis-sentinel sentinel.conf      # 启动哨兵模式
netstat -tlnp |grep 26379   # 开放默认端口26379
firewall-cmd  --add-port=26379/tcp --permanent && firewall-cmd --reload  # 防火墙开放端口

(4) Check whether the configuration is successful

[root@master bin]#  redis-cli  -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master,status=ok,address=192.168.200.10:6379,slaves=2,sentinels=3
# 发现有3个哨兵节点就OK了!

test

Master-slave replication test

# master写
[root@master bin]# redis-cli -h 192.168.200.10 -p 6379 -a 123456
192.168.200.10:6379> ping
PONG
192.168.200.10:6379> MSET name csq age 18 sex man
OK
# slave1读
[root@slave1 bin]# redis-cli -h 192.168.200.20 -p 6379 -a 123456
192.168.200.20:6379> ping
PONG
192.168.200.20:6379> MGET name age sex
1) "csq"
2) "18"
3) "man"
# slave2读
[root@slave2 bin]# redis-cli -h 192.168.200.30 -p 6379 -a 123456
192.168.200.30:6379> ping
PONG
192.168.200.30:6379> MGET name age sex
1) "csq"
2) "18"
3) "man"

Simulate master downtime

192.168.200.10:6379> SHUTDOWN
(0.83s)
not connected> quit

Sentinel log query

PINGThe Sentinel node monitors the survival status of the instance by sending commands to the Redis instance .

If the master or slave server fails to respond to the sentinel node's PINGcommands, the sentinel node marks the instance as 主观下线.

When the sentinel node detects that the main server is offline, it will request other sentinel nodes to confirm the offline main server. Once 大部分哨兵节点都确认主服务器已下线, that master is marked as 客观下线.

The sentinel node will start from the currently available slave server 选出一个成为新的主服务器and connect other slave servers 切换为新的主服务器to the slave server.

image-20230721232049987

Log in to 192.168.200.20redis to check whether it has become the master

192.168.200.20:6379> info replication
# Replication
role:master   # 之前的slave1 变为了master
connected_slaves:1
slave0:ip=192.168.200.30,port=6379,state=online,offset=243852,lag=1  # 从节点
master_failover_state:no-failover
master_replid:1fcb24a95055f3bb705997e3f4cbe9dc231d5fb7
master_replid2:c486a9c578d65459edb9d496220ac11d1f7d790e
master_repl_offset:243993
second_repl_offset:74074
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:243993

Log in to 192.168.200.30redis to check whether it has become a slave.

192.168.200.30:6379> INFO replication
# Replication
role:slave                      # 从节点
master_host:192.168.200.20      # IP
master_port:6379                # 端口
master_link_status:up           # 从节点能连接到主节点
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:263732
slave_repl_offset:263732
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:1fcb24a95055f3bb705997e3f4cbe9dc231d5fb7
master_replid2:c486a9c578d65459edb9d496220ac11d1f7d790e
master_repl_offset:263732
second_repl_offset:74074
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:263732

restore master

[root@master bin]# redis-server redis.conf   # 启动redis
[root@master bin]# redis-cli -h 192.168.200.10 -p 6379 -a 123456
192.168.200.10:6379> ping
PONG
192.168.200.10:6379> INFO replication
# Replication
role:slave                  # 变为了从节点
master_host:192.168.200.20  # IP
master_port:6379            # 端口
master_link_status:up       # 从节点和主节点连接状态
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:301513
slave_repl_offset:301513
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:1fcb24a95055f3bb705997e3f4cbe9dc231d5fb7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:301513
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:299080
repl_backlog_histlen:2434

Test whether the old master is writable

# 很显然是不可写的
192.168.200.10:6379> set a b
(error) READONLY You can't write against a read only replica.

View the master-slave information of master 192.168.200.20 again

192.168.200.20:6379> INFO replication
# Replication
role:master
connected_slaves:2  # 成功添加进去了
slave0:ip=192.168.200.30,port=6379,state=online,offset=352750,lag=1
slave1:ip=192.168.200.10,port=6379,state=online,offset=352609,lag=1
master_failover_state:no-failover
master_replid:1fcb24a95055f3bb705997e3f4cbe9dc231d5fb7
master_replid2:c486a9c578d65459edb9d496220ac11d1f7d790e
master_repl_offset:352750
second_repl_offset:74074
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:352750

End of test

Guess you like

Origin blog.csdn.net/qq_52089863/article/details/131861949