1.服务器规划(示例)
服务器: (要求redis主从能使用的内存容量一致,并且均配置maxmemory)
192.168.127.8 redis-7003 主节点 redis-sentinel-7013 redis哨兵1
192.168.127.7 redis-7002 从节点 redis-sentinel-7012 redis哨兵2
192.168.127.6 redis-sentinel-7011 redis哨兵3
说明:当主节点下线后,三个哨兵投票决定谁是leader,2个以上的哨兵认为主节点下线了(主观下线),leader会去做故障转移的工作
问题:1.当进行了一次主从切换后,只有一个reids节点还在运行,如果需要再次切换主从,需要把原来宕机的redis节点启动起来
2.主从节点切换后,客户端需要切换请求的IP地址,jedis客户端无法实现IP地址的转换,使用原来的jedis客户端就无法正常访问,需要使用 sharded-jedis-sentinel-pool
2.在一台机器上安装redis
3.复制redis安装后的文件夹到其它机器,并重命名为redis-${port}
4.在各台机器上复制一个redis安装文件夹 , 并重命名为redis-sentinel-${port}
5.配置redis主节点
port 7003
dir "/usr/local/redis4/data"
logfile "7003.log"
daemonize yes
pidfile "/var/run/redis_7003.pid"
#关闭redis保护模式,保护模式关闭后,能访问到服务器的客户端都能够连接到此redis
protected -mode no
#主节点密码
requirepass "2329257"
#最大内存使用量
maxmemory 1gb
|
6.配置redis从节点
port 7002
dir "/usr/local/redis4/data"
logfile "7002.log"
daemonize yes
pidfile "/var/run/redis_7002.pid"
#关闭redis保护模式,保护模式关闭后,能访问到服务器的客户端都能够连接到此redis
protected -mode no
#从节点连接主节点的密码
masterauth "2329257"
#最大内存使用量
maxmemory 1gb
|
7.配置redis哨兵节点(有三个哨兵,配置仅仅是端口不同,这里只配置一个作为示例)
port 7012
dir "/usr/local/redis4-sentinel/data"
logfile "7012.log"
daemonize yes
pidfile "/var/run/redis_7012.pid"
#关闭redis保护模式,保护模式关闭后,能访问到服务器的客户端都能够连接到此redis
protected -mode no
sentinel deny-scripts-reconfig yes
#配置主节点的IP和端口 并设置需要 2 个以上的哨兵认为主节点宕机才会切换主从
sentinel monitor mymaster 192.168 . 127.8 7003 2
#sentinel 定期通过ping访问主节点,如果超过150s未响应,就认为主节点宕机了
sentinel down-after-milliseconds mymaster 150000
#主节点的访问密码
sentinel auth-pass mymaster 2329257
#故障转移时最多可以有 2 从节点同时对新主节点进行数据同步
sentinel config-epoch mymaster 2
sentinel leader-epoch mymaster 2
#故障转移时间,当决定故障转移后到转移完成的最大时间
sentinel failover-timeout mymaster 180000
|
8.启动redis主节点
bin/redis-server conf/redis-7003.conf
9.启动redis从节点
bin/redis-server conf/redis-7002.conf
10.启动redis哨兵节点
bin/redis-sentinel conf/redis-7013.conf
bin/redis-sentinel conf/redis-7012.conf
bin/redis-sentinel conf/redis-7011.conf
11.在主节点和从节点观察主从状态(当前主节点有一个从节点 192.128.127.7:7002)
在主节点查看
# Replication
role:master
connected_slaves: 1
slave0:ip= 192.168 . 127.7 ,port= 7002 ,state=online,offset= 444947 ,lag= 0
master_replid:f526d934f5f315888731e918f97f0d14ad2724e2
master_replid2:4c6db5630cb19eeebea9796fde82f80188bf1926
master_repl_offset: 445101
second_repl_offset: 25084
repl_backlog_active: 1
repl_backlog_size: 1048576
repl_backlog_first_byte_offset: 1
repl_backlog_histlen: 445101
|
在从节点查看(当前从节点有一个主节点 192.128.127.8:7003)
# Replication
role:slave
master_host: 192.168 . 127.8
master_port: 7003
master_link_status:up
master_last_io_seconds_ago: 1
master_sync_in_progress: 0
slave_repl_offset: 435231
slave_priority: 100
slave_read_only: 1
connected_slaves: 0
master_replid:f526d934f5f315888731e918f97f0d14ad2724e2
master_replid2: 0000000000000000000000000000000000000000
master_repl_offset: 435231
second_repl_offset:- 1
repl_backlog_active: 1
repl_backlog_size: 1048576
repl_backlog_first_byte_offset: 429707
repl_backlog_histlen: 5525
|
12.将主节点下线,观察自动故障转移的过程
查看sentinel的日志会发现有一句日志,显示主从节点切换的情况
+ switch -master mymaster 192.168 . 127.8 7003 192.168 . 127.7 7002
|
在从节点查询info replication,发现原来的从节点变成了主节点,没有从节点
# Replication
role:master
connected_slaves: 0
master_replid:f526d934f5f315888731e918f97f0d14ad2724e2
master_replid2:4c6db5630cb19eeebea9796fde82f80188bf1926
master_repl_offset: 399326
second_repl_offset: 25084
repl_backlog_active: 1
repl_backlog_size: 1048576
repl_backlog_first_byte_offset: 1
repl_backlog_histlen: 399326
|