第十六期:基于三台服务器部署Redis主从及配置哨兵模式
一. Redis主从同步
1.1 原理
redis主从复制,当用户往master端写入数据时,通过redis sync机制将数据文件发送给slave,slave也会执行相同的操作确保数据一致。同时slave上还可以开启二级slave,三级slave从库。Redis主从配置非常简单,只需要在redis从库配置中指定slave of ip port即可,IP表示指定主库的ip,port表示redis监听端口。
master:192.168.193.104
slave:192.168.193.103
1.2 主从配置
1.2.1 主库配置
不设置密码。从库可以直接使用slave of ip port连接主库
#修改配置文件
[root@node4redis]# egrep "^(daemonize|bind)" 6379.conf
bind 0.0.0.0
daemonize yes #以守护进程在后台运行
设置认证密码,从库需要指定master的密码才能完成同步,否则日志会有报错信息提示
#修改配置文件
[root@node4redis]# egrep "^(daemonize|bind)" 6379.conf
bind 0.0.0.0
daemonize yes
1.2.2 从库配置
#指定主库
[root@node3redis]# egrep "^(replicaof)" /usr/local/redis/6379.conf
replicaof192.168.193.104 6379
1.2.3 从库查看状态
#登录
#设置别名
[root@node3redis]# alias redis-cli='/usr/local/redis/bin/redis-cli'
[root@node3redis]# redis-cli
127.0.0.1:6379>
#查看角色
127.0.0.1:6379>role
1)"slave"
2)"192.168.193.104"
3) (integer)6379
4)"connected"
5) (integer) 532
#查看详细信息(主库连接状态必须为up,表示成功)
127.0.0.1:6379>info replication
# Replication
role:slave
master_host:192.168.193.104
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:616
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d1fd66feac1237202e502e8ba91f0a42c2b9ff65
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:616
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616
#查看主库状态:
[root@node3redis]# redis-cli -h 192.168.193.104
192.168.193.104:6379>info replication
# Replication
role:master #角色为master
connected_slaves:1 #当前有一个从库
slave0:ip=192.168.193.103,port=6379,state=online,offset=812,lag=1 #从库信息
master_replid:d1fd66feac1237202e502e8ba91f0a42c2b9ff65
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:812
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:812
192.168.193.104:6379>
1.2.4 日志分析
[root@node3 redis]# tail -f redis_6379.log
7619:S 20 Jun 2020 17:28:28.585 * DB loaded from disk: 0.000 seconds
7619:S 20 Jun 2020 17:28:28.585 * Before turning into a replica,using my master parameters to synthesize a cached master: I may be able tosynchronize with the new master with just a partial transfer.
7619:S 20 Jun 2020 17:28:28.585 * Ready to accept connections
7619:S 20 Jun 2020 17:28:28.585 * Connecting to MASTER192.168.193.104:6379
7619:S 20 Jun 2020 17:28:28.586 * MASTER <-> REPLICA syncstarted
7619:S 20 Jun 2020 17:28:28.586 * Non blocking connect for SYNCfired the event.
7619:S 20 Jun 2020 17:28:28.586 * Master replied to PING,replication can continue...
7619:S 20 Jun 2020 17:28:28.587 * Trying a partial resynchronization(request d1fd66feac1237202e502e8ba91f0a42c2b9ff65:1).
7619:S 20 Jun 2020 17:28:28.587 * Successful partialresynchronization with master.
7619:S 20 Jun 2020 17:28:28.587 * MASTER <-> REPLICA sync:Master accepted a Partial Resynchronization.
1.2.5 主从复制相关参数解读
# 指定主库IP和端口:
replicaof 192.168.75.136 6379
# 指定主库得认证密码:
masterauth 123456
# 从库正在复制时,从库可以响应用户读请求,如果设置为no,则返回报错信
息。
replica-serve-stale-data yes
# 设置从库为只读
replica-read-only yes
# 启动socket方式复制数据库,master生成rdb文件,不在是先保存到磁盘,
然后发给从库,而是直接把rdb发送给从库,减少了磁盘IO
repl-diskless-sync yes
# 配置延时时间,让更多slave加入传输队列,如果复制已经开始,则5秒内,
不接受新的slave同步请求
repl-diskless-sync-delay 5
# 指定从库定期检查主库状态,默认10秒
repl-ping-replica-period 10
# 同步超时时间
repl-timeout 60
# 是否禁用tcp-nodelay,yes表示禁用,redis会在写缓存积累到一定量之
后一起发送,节省带宽,但是会导致master和slave数据延迟,no,表示启
用,redis会立即发送数据包,即使是很小数据,数据同步会比较快,但是消耗
更多带宽
repl-disable-tcp-nodelay no
# 设置在同步过程中,写缓冲区得大小,需要考虑到同步的时间和数据的写入速
度
repl-backlog-size 1mb
# 设置从库的优先级,在主库宕机后,根据优先级选择slave,值越小,则优先
级越高,0,表示不参与竞选,故永远不会被选中。
replica-priority 100
1.3 Redis哨兵模式
1.3.1 哨兵主要作用
监控:监控redis主库及从库运行状态;
通知:如果redis发生故障转移,可以通过邮件通知管理员;
自动故障转移:一旦发现主库宕机,则在从库中通过选举新的master
进行故障转移
1.3.2 工作原理
哨兵(sentinel) 是一个分布式系统,你可以在一个架构中运行多个哨兵
(sentinel) 进程,这些进程使用流言协议(gossipprotocols)来接收关于
Master是否下线的信息,并使用投票协议(agreement protocols)来决定是
否执行自动故障迁移,以及选择哪个Slave作为新的Master。
每个哨兵(sentinel) 会向其它哨兵(sentinel)、master、slave定时发送消
息,以确认对方是否”活”着,如果发现对方在指定时间(可配置)内未回应,
则暂时认为对方宕机了,这种宕机称为”主观认为宕机” Subjective Down,
简称sdown)。
若“哨兵群”中的多数sentinel,都报告某一master没响应,系统才认为该
master真正宕机,即客观上认为宕机,Objective Down,简称odown),通
过一定的vote算法,从剩下的slave节点中,选一台提升为master,然后自
动修改相关配置。
1.3.3 哨兵模式配置
master:192.168.193.104:6379
slave1:192.168.193.103:6379
slave2:192.168.193.103:7000
1.3.4 配置redis主从
#配置master,开启网卡监听
[root@node4redis]# egrep "^bind" /usr/local/redis/6379.conf
bind 0.0.0.0
#配置slave1,指定master
[root@node3redis]# egrep "^(bind|replicaof)" /usr/local/redis/6379.conf
bind 0.0.0.0
replicaof192.168.193.104 6379
#配置slave2,指定master
[root@node37000]# egrep "^(bind|replicaof)" /usr/local/redis/7000/7000.conf
bind 0.0.0.0
replicaof192.168.193.104 6379
#启动master和slave的redis服务
1.3.5 查看主从状态
#在master执行以下指令,均可查看同步信息
[root@node4redis]# redis-cli
127.0.0.1:6379>role
1)"master"
2) (integer)3865
3) 1) 1)"192.168.193.103"
2) "6379"
3) "3865"
2) 1) "192.168.193.103"
2) "7000"
3) "3865"
127.0.0.1:6379>info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.193.103,port=6379,state=online,offset=3963,lag=1
slave1:ip=192.168.193.103,port=7000,state=online,offset=3963,lag=1
master_replid:d1fd66feac1237202e502e8ba91f0a42c2b9ff65
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3963
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3963
1.3.6 配置哨兵
#master端配置sentinel:
[root@node4redis]# cd /opt/redis-5.0.5
[[email protected]]# cp sentinel.conf /usr/local/redis/
[root@node4 ~]#vim /usr/local/redis/sentinel.conf
# 绑定监听IP
bind 0.0.0.0
# 监听端口
port 26379
# 后台运行
daemonize yes
pidfile /var/run/redis-sentinel.pid
# 哨兵得日志文件
logfile"sentinel.log"
# 日志文件存放路径
dir/usr/local/redis/
# 设置初始master以及法定认为下线人数:
sentinel monitormymaster 192.168.193.104 6379 2
# master主观下线时间,默认30秒,30秒内没有回复pong,则认为下线了
sentineldown-after-milliseconds mymaster 30000
# 指定在故障转移期间,多少个slave向新的master同步得数量,如果slave
是提供查询服务,则应该设置小一点更好
sentinelparallel-syncs mymaster 1
# 指定故障转移超时时间,默认为3分钟
sentinelfailover-timeout mymaster 180000
# 设置通知脚本,发生故障转移可以向管理员发送通知(可选)
sentinelnotification-script mymaster
/usr/local/redis/notify.sh
# 禁止修改脚本,避免脚本重置
sentineldeny-scripts-reconfig yes
#notify.sh脚本
#!/bin/bash
TO="[email protected]"
SUBJECT="Redisproblem"
CONTEXT="Redisproblem"
echo -e $CONTEXT| mailx -s $SUBJECT $TO
#将哨兵配置文件复制到其他服务器
[root@node4 ~]#scp /usr/local/redis/sentinel.conf 192.168.193.103:/usr/local/redis/
[root@node4 ~]#scp /usr/local/redis/sentinel.conf 192.168.193.103:/usr/local/redis/7000/
**注意:由于此处slave在同一台服务器上部署的两个redis实例,所以需要在slave上修改对应的sentinel.conf中对应的路径,否则启动会报错**
#启动哨兵
#master
[root@node4 ~]#/usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel.conf
#slave
[root@node3redis]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel.conf
[root@node3redis]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/7000/sentinel.conf
#查看日志
[root@node4 ~]# tailf /usr/local/redis/sentinel.log
2843:X 20 Jun 2020 10:22:58.860 # oO0OoO0OoO0Oo Redis is startingoO0OoO0OoO0Oo
2843:X 20 Jun 2020 10:22:58.860 # Redis version=5.0.5, bits=64,commit=00000000, modified=0, pid=2843, just started
2843:X 20 Jun 2020 10:22:58.860 # Configuration loaded
2844:X 20 Jun 2020 10:22:58.866 * Increased maximum number of openfiles to 10032 (it was originally set to 1024).
2844:X 20 Jun 2020 10:22:58.868 * Running mode=sentinel, port=26379.
2844:X 20 Jun 2020 10:22:58.869 # Sentinel ID is60bbf61f011d66cc7438fc9efce9607e49024922
2844:X 20 Jun 2020 10:22:58.869 # +monitor master mymaster 192.168.193.1046379 quorum 2
2844:X 20 Jun 2020 10:22:58.871 * +slave slave 192.168.193.103:6379192.168.193.103 6379 @ mymaster 192.168.193.104 6379
2844:X 20 Jun 2020 10:22:58.872 * +slave slave 192.168.193.103:7000192.168.193.103 7000 @ mymaster 192.168.193.104 6379
2844:X 20 Jun 2020 10:23:18.446 * +sentinel sentinel0cf6ab50c58bfec961b26e98660304d7cc32e322 192.168.193.103 26379 @ mymaster 192.168.193.1046379
#验证主从同步
#在master上
[root@node4 ~]# redis-cli
127.0.0.1:6379> set user wwmgc
OK
127.0.0.1:6379> get user
"wwmgc"
#在slave
[root@node3 redis]# redis-cli
127.0.0.1:6379> get user
"wwmgc"
[root@node3 redis]# redis-cli -p 7000
127.0.0.1:7000> get user
"wwmgc"
#说明数据已经同步
#现将主服务器redis关闭
[root@node4 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN
#查看slave日志变化,发现103的6379已经变为master
[root@node3 redis]# tailf sentinel.log
7887:X 20 Jun 2020 18:23:16.775 # oO0OoO0OoO0Oo Redis is startingoO0OoO0OoO0Oo
7887:X 20 Jun 2020 18:23:16.775 # Redis version=5.0.5, bits=64,commit=00000000, modified=0, pid=7887, just started
7887:X 20 Jun 2020 18:23:16.775 # Configuration loaded
7888:X 20 Jun 2020 18:23:16.778 * Increased maximum number of openfiles to 10032 (it was originally set to 1024).
7888:X 20 Jun 2020 18:23:16.779 * Running mode=sentinel, port=26379.
7888:X 20 Jun 2020 18:23:16.781 # Sentinel ID is0cf6ab50c58bfec961b26e98660304d7cc32e322
7888:X 20 Jun 2020 18:23:16.781 # +monitor master mymaster192.168.193.104 6379 quorum 2
7888:X 20 Jun 2020 18:23:16.783 * +slave slave 192.168.193.103:6379192.168.193.103 6379 @ mymaster 192.168.193.104 6379
7888:X 20 Jun 2020 18:23:16.784 * +slave slave 192.168.193.103:7000192.168.193.103 7000 @ mymaster 192.168.193.104 6379
7888:X 20 Jun 2020 18:23:17.540 * +sentinel sentinel60bbf61f011d66cc7438fc9efce9607e49024922 192.168.193.104 26379 @ mymaster192.168.193.104 6379
7888:X 20 Jun 2020 18:32:24.583 # +sdown master mymaster192.168.193.104 6379
7888:X 20 Jun 2020 18:32:24.696 # +new-epoch 1
7888:X 20 Jun 2020 18:32:24.697 # +vote-for-leader60bbf61f011d66cc7438fc9efce9607e49024922 1
7888:X 20 Jun 2020 18:32:25.688 # +odown master mymaster192.168.193.104 6379 #quorum 2/2
7888:X 20 Jun 2020 18:32:25.688 # Next failover delay: I will notstart a failover before Sat Jun 20 18:38:25 2020
7888:X 20 Jun 2020 18:32:25.797 # +config-update-from sentinel60bbf61f011d66cc7438fc9efce9607e49024922 192.168.193.104 26379 @ mymaster192.168.193.104 6379
7888:X 20 Jun 2020 18:32:25.797 # +switch-master mymaster192.168.193.104 6379 192.168.193.103 6379
7888:X 20 Jun 2020 18:32:25.797 * +slave slave 192.168.193.103:7000192.168.193.103 7000 @ mymaster 192.168.193.103 6379
7888:X 20 Jun 2020 18:32:25.797 * +slave slave 192.168.193.104:6379192.168.193.104 6379 @ mymaster 192.168.193.103 6379
7888:X 20 Jun 2020 18:32:55.881 # +sdown slave 192.168.193.104:6379192.168.193.104 6379 @ mymaster 192.168.193.103 6379
#此时在此查看值发现值还在,并未消失
[root@node3 ~]# redis-cli
127.0.0.1:6379> get user
"wwmgc"
#现在重新启动104
[root@node4 ~]# /usr/local/redis/bin/redis-server/usr/local/redis/6379.conf
发现104已经变为103的slave了
[root@node3 redis]# tailf sentinel.log
…
7888:X 20 Jun 2020 18:35:38.944 * +convert-to-slave slave192.168.193.104:6379 192.168.193.104 6379 @ mymaster 192.168.193.103 6379
不论你在什么时候开始,重要的是开始之后就不要停止!
--end--
了解更多请关注吧
点个小花花,让他们知道你“在看”我