Redis master-slave replication and sentinel mode build documentation

One, master-slave replication

Master-slave replication: Master-slave replication is the basis of high-availability Redis. Sentinel and clusters are all based on master-slave replication to achieve high availability. Master-slave replication mainly implements multi-machine backup of data, as well as load balancing and simple failure recovery for read operations. Defects: Failure recovery cannot be automated: write operations cannot be load balanced; storage capacity is limited by a single machine.

Host IP address Software package
Master node 192.168.199.40 redis-5.0.7.tar.gz
Slave1 node 192.168.199.50 redis-5.0.7.tar.gz
Slave2 node 192.168.199.60 redis-5.0.7.tar.gz
systemctl stop firewalld
setenforce 0

Install Redis

[NoSQL Redis configuration and optimization]

Modify Redis configuration file (Master node)

vim /etc/redis/6379.conf
bind 0.0.0.0                        #70,修改监听地址为0.0.0.0
daemonize yes                       #137行,开启守护进程
logfile /var/ log/redis_6379.log    #172行,指定日志文件目录
dir /var/lib/redis/6379             #264行,指定工作目录
appendonly yes                      #700行,修改为yes开启AOF持久化功能

/etc/init.d/redis_6379 restart

Modify Redis configuration file (Slave node)

vim /etc/redis/6379.conf
bind 0.0.0.0                       #70行,修改监听地址为0.0.0.0
daemonize yes                      #137行,开启守护进程
logfile /var/log/redis_6379.log    #172行,指定日志文件目录
dir /var/lib/redis/6379            #264行,指定工作目录
replicaof 192.168.199.40 6379      #288行,指定要同步的Master节点IP和端口
appendonly yes                     #700行,开启AOF持久化功能

/etc/init.d/redis_6379 restart

Verify the master-slave effect

Look at the log on the Master node

tail -f /var/log/redis_6379.log 

Insert picture description here

Verify the slave node on the master node

redis-cli info replication

Insert picture description here

Second, build Redis sentry mode

The start of the sentinel depends on the master-slave mode, so you must install the master-slave mode before doing the sentry mode. All nodes need to deploy the sentry mode. The sentry mode will monitor whether all Redis working nodes are normal. When the Master appears When there is a problem, because other nodes have lost contact with the master node, they will vote. More than half of the vote will think that this Master does have a problem, and then the sentry room will be notified, and then one of the Slaves will be selected as the new Master.

systemctl stop firewalld
setenforce 0

Modify the configuration file of Redis sentry mode (all nodes)

vim /opt/redis-5.0.7/sentinel.conf
protected-mode no                                    #17行,关闭保护模式
port 26379                                           #21行,Redis哨兵默认的监听端口
daemonize yes                                        #26行,指定sentinel为后台启动
logfile "/var/1og/sentinel.log"                      #36行,指定日志存放路径
dir "/var/lib/redis/6379"                            #65行,指定数据库存放路径
sentinel monitor mymaster 192. 168.199.40 6379 2     #84行,修改,指定该哨兵节点监控192.168.199.40:6379这个主节点,该主节点的名称是mymaster,最后的2的含义与主节点的故障判定有关:至少需要2
个哨兵节点同意,才能判定主节点故障并进行故障转移
sentinel down-after-milliseconds mymaster 3000       #113行,判定服务器down掉的时间周期,默认30000毫秒(30 )
sentinel failover-timeout mymaster 180000            #146行,故障节点的最大超时时间为180000 (180)

Activate sentinel mode

Start master first, then slave

cd /opt/redis-5.0.7/ 
redis-sentinel sentinel.conf &

Insert picture description here

View sentinel information

redis-cli -p 26379 info sentinel

Insert picture description here

Failure simulation

View redis-server process number

ps -ef | grep redis

Insert picture description here

Kill the process ID of the redis-server on the Master node

kill -9 8503     #Master节点上redis-server的进程号

Insert picture description here

redis-cli -p 26379 info sentinel

Insert picture description here

Insert picture description here

Three, build a Redis cluster

Generally speaking, a redis cluster requires 6 nodes, 3 masters and 3 slaves.
For convenience, all nodes here are simulated on the same server:
distinguished by port numbers:
3 master node port numbers: 6000/6001/6002, and corresponding slave node port numbers: 6004/6003/6005.

cd /etc/redis/
mkdir -p redis-cluster/redis600{
    
    0..5}

for w in {
    
    0..5}
do
cp /opt/redis-5.0.7/redis.conf  /etc/redis/redis-cluster/redis600$w
cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis600$w
done

Insert picture description here

Turn on the cluster function

其他5个文件夹的配置文件以此类推修改,注意6个端口都要不一样。
vim /etc/redis/redis-cluster/redis6000/redis.conf

#bind 127.0.0.1                       #69行,注释掉bind项,默认监听所有网卡
protected-mode no                     #88行,修改,关闭保护模式
port 6000                             #92行,修改,redis 监听端口,
daemonize yes                         #136行,开启守护进程,以独立进程启动
appendonly yes                        #700行,修改,开启AOF持久化
cluster -enabled yes                  #832行,取消注释,开启群集功能
cluster-config-file nodes-6379.conf   #840行,取消注释,群集名称文件设置
cluster-node-timeout 15000            #846行,取消注释群集超时时间设置

Insert picture description here

Start the redis node

分别进入那六个文件夹,执行命令: redis-server redis.conf,来启动redis节点
cd /etc/redis/redis-cluster/redis6000
redis-server redis.conf
(或使用for循环来启动)
for i in {
    
    0..5}
do
cd /etc/redis/redis-cluster/redis600$i
redis-server redis.conf
done

Insert picture description here
Insert picture description here

ps -ef | grep redis

Start the cluster

redis-cli --cluster create 127.0.0.1:6000 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 --cluster-replicas 1

#The six instances are divided into three groups, each group has one master and one slave, the front is the master node, and the back is the slave node. You need to enter yes to create the following interaction.
-Replicas 1 means that each master node has 1 slave node.

Insert picture description here

redis-cli -p 6000 -c         #加-c参数,节点之间就可以互相跳转	
cluster slots			     #查看节点的哈希槽编号范围

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51614581/article/details/114581884