Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况。为了分担读压力,Redis支持主从复制,保证主数据库的数据内容和从数据库的内容完全一致。 Redis 的主从结构可以采用一主多从或者级联结构,Redis主从复制可以根据是否是全量分为全量同步和增量同步
■ 全量同步
Redis全量复制一般发生在Slave初始化阶段,这时Slave需要将Master上的所有数据都复制一份
● 从服务器连接主服务器,发送SYNC命令
● 主服务器接收到SYNC命名后,开始执行BGSAVE命令生成RDB文件并使用缓冲区记录此后执行的所有写命令
● 主服务器BGSAVE执行完后,向所有从服务器发送快照文件,并在发送期间继续记录被执行的写命令
● 从服务器收到快照文件后丢弃所有旧数据,载入收到的快照
● 主服务器快照发送完毕后开始向从服务器发送缓冲区中的写命令
● 从服务器完成对快照的载入,开始接收命令请求,并执行来自主服务器缓冲区的写命令
■ 增量同步
Redis增量同步是指Slave初始化后开始正常工作时主服务器发生的写操作同步到从服务器的过程
增量复制的过程主要是主服务器每执行一个写命令就会向从服务器发送相同的写命令,从服务器接收并执行收到的写命令
■ Redis主从同步策略
主从刚刚连接的时候,进行全量同步;全同步结束后,进行增量同步。当然,如果有需要,slave在任何时候都可以发起全量同步。redis策略是,无论如何,首先会尝试进行增量同步,如不成功,要求从机进行全量同步
■ 哨兵模式原理
哨兵(sentinel) 是一个分布式系统,用于对主从结构中的每台服务器进行监控,当出现故障时通过投票机制选择新的master并将所有slave连接到新的master。所以整个运行哨兵的集群的数量不得少于3个节点
■ 哨兵模式的作用
● 监控
不断的检查master和slave是否正常运行。
master存活检测、master与slave运行情况检测
● 通知(提醒)
当被监控的服务器出现问题时,向其他(哨兵间,客户端)发送通知。
● 自动故障转移
断开master与slave连接,选取一个slave作为master,将其他slave连接到新的master,并告知客户端新的服务器地址
PS:哨兵也是一台redis服务器,只是不提供数据服务
哨兵的启动依赖于主从模式,所以须把主从模式安装好的情况下再去做哨兵模式,所有节点上都需要部署哨兵模式,哨兵模式会监控所有的redis工作节点是否正常,当master出现问题的时候,因为其他节点与主节点失去联系,因此会投票,投票过半就认为这个master的确出现问题,然后会通知哨兵间,然后从slaves中选取一个作为新的master
[root@master ~]# systemctl stop firewalld
[root@master ~]# setenforce 0
[root@master ~]# tar zxvf redis-5.0.4.tar.gz #安装包解压缩
[root@master ~]# cd redis-5.0.4/ #进入目录
[root@master redis-5.0.4]# make #进行安装
[root@master redis-5.0.4]# make PREFIX=/usr/local/redis install #安装路径设置
[root@master redis-5.0.4]# cd
[root@master ~]# ln -s /usr/local/redis/bin/* /usr/local/bin #进行命令链接
[root@master ~]# cd redis-5.0.4/utils/
[root@master utils]# ./install_server.sh #运行脚本
[root@master utils]# netstat -anpt | grep redis #查看服务状态
[root@master utils]# cd
[root@master ~]# vi /etc/redis/6379.conf #进入主模式配置文件
[root@slaver1 utils]# cd
[root@slaver1 ~]# vi /etc/redis/6379.conf
[root@slave2 utils]# cd
[root@slave2 ~]# vi /etc/redis/6379.conf
[root@master ~]# /etc/init.d/redis_6379 restart 重启服务
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
[root@slave1 ~]# /etc/init.d/redis_6379 restart
[root@slave2 ~]# /etc/init.d/redis_6379 restart
[root@master ~]# tail -f /var/log/redis_6379.log #查看日志
[root@master ~]# tail -f /var/log/redis_6379.log
59040:M 18 Dec 2020 14:27:20.101 * Synchronization with replica 192.168.74.30:6379 succeeded #同步给Slave2成功
59040:M 18 Dec 2020 14:27:27.445 # Connection with replica 192.168.74.20:6379 lost.
59040:M 18 Dec 2020 14:27:27.453 * Replica 192.168.74.20:6379 asks for synchronization
59040:M 18 Dec 2020 14:27:27.453 * Full resync requested by replica 192.168.74.20:6379 #全量同步,请求来自于Slave1
59040:M 18 Dec 2020 14:27:27.453 * Starting BGSAVE for SYNC with target: disk
59040:M 18 Dec 2020 14:27:27.454 * Background saving started by pid 59104
59104:C 18 Dec 2020 14:27:27.456 * DB saved on disk
59104:C 18 Dec 2020 14:27:27.456 * RDB: 4 MB of memory used by copy-on-write
59040:M 18 Dec 2020 14:27:27.463 * Background saving terminated with success #后台保存命令成功
59040:M 18 Dec 2020 14:27:27.463 * Synchronization with replica 192.168.74.20:6379 succeeded
[root@master ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.74.30,port=6379,state=online,offset=2030,lag=0
slave1:ip=192.168.74.20,port=6379,state=online,offset=2030,lag=1
master_replid:f4956bad4bf3b233f6f703a38e7a86d8202bfe51
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2030
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2030
127.0.0.1:6379>
[root@slave1 ~]# tail -f /var/log/redis_6379.log
58186:S 18 Dec 2020 14:27:27.463 * MASTER <-> REPLICA sync: Finished with success #主从建立成功
58186:S 18 Dec 2020 14:27:27.463 * Background append only file rewriting started by pid 58190
58186:S 18 Dec 2020 14:27:27.499 * AOF rewrite child asks to stop sending diffs.
58190:C 18 Dec 2020 14:27:27.499 * Parent agreed to stop sending diffs. Finalizing AOF...
58190:C 18 Dec 2020 14:27:27.499 * Concatenating 0.00 MB of AOF diff received from parent.
58190:C 18 Dec 2020 14:27:27.499 * SYNC append only file rewrite performed
58190:C 18 Dec 2020 14:27:27.499 * AOF rewrite: 4 MB of memory used by copy-on-write
58186:S 18 Dec 2020 14:27:27.552 * Background AOF rewrite terminated with success
58186:S 18 Dec 2020 14:27:27.552 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
58186:S 18 Dec 2020 14:27:27.552 * Background AOF rewrite finished successfully
[root@slave2 ~]# tail -f /var/log/redis_6379.log
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> get a
"1"
127.0.0.1:6379>
[root@master ~]# vi redis-5.0.4/sentinel.conf
17/protected-mode no #关闭保护模式
26/daemonize yes #指定sentinel为后台启动
36/logfile "/var/log/sentinel.log" #指定日志存放路径
65/dir "/var/lib/redis/6379" #指定数据库存放路径
84/sentinel monitor mymaster 192.168.74.10 6379 2 #至少几个哨兵检测到主服务器故障了,才会进行故障迁移
113/sentinel down-after-milliseconds mymaster 3000 #判定服务器down掉的时间周期,默认30000毫秒(30秒)
146/sentinel failover-timeout mymaster 100000 #故障节的的最大超时时间为180000(180秒)
[root@master ~]# vi redis-5.0.4/sentinel.conf
[root@master ~]# scp redis-5.0.4/sentinel.conf root@192.168.74.20:/root/redis-5.0.4
The authenticity of host '192.168.74.20 (192.168.74.20)' can't be established.
ECDSA key fingerprint is SHA256:VEizcsuU+V0LOIvHezPhrm6kTgydj3Fxy2nXMAcD8K0.
ECDSA key fingerprint is MD5:b1:a7:ad:69:d2:f0:87:b8:ab:53:f5:6b:53:a3:16:be.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.74.20' (ECDSA) to the list of known hosts.
root@192.168.74.20's password:
sentinel.conf 100% 9748 7.4MB/s 00:00
[root@master ~]# scp redis-5.0.4/sentinel.conf root@192.168.74.30:/root/redis-5.0.4
The authenticity of host '192.168.74.30 (192.168.74.30)' can't be established.
ECDSA key fingerprint is SHA256:17g6YnNbxEc/en5d72P8B1iRo1U/borhDBfy8NyBsDU.
ECDSA key fingerprint is MD5:a8:5c:c9:d1:2f:5d:97:5a:8f:3d:31:d2:72:a6:bb:19.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.74.30' (ECDSA) to the list of known hosts.
root@192.168.74.30's password:
sentinel.conf 100% 9748 8.9MB/s 00:00
[root@master ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[root@slave1 ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[root@slave2 ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[root@master ~]# tail -f /var/log/sentinel.log
[root@master ~]# ps aux | grep sentinel #查看端口信息
[root@slave1 ~]# redis-cli -h 192.168.74.10 -p 26379 info sentinel
[root@master ~]# /etc/init.d/redis_6379 stop #停止服务
Stopping ...
Redis stopped
[root@slave1 ~]# ps aux | grep redis
[root@slave1 ~]# tail -f /var/log/sentinel.log 进入slave1 查看日志
[root@master ~]# /etc/init.d/redis_6379 start
在Slave2上查看状态
[root@slave2 ~]# redis-cli -h 192.168.74.30 -p 26379 info sentinel