redis主从配置(sentinel)

1.集群环境
2.配置并启动Redis主从集群
3.配置sentinel集群并启动
4.测试sentinel集群

集群环境:
一台mac设备,多个端口
127.0.0.1
6379 master
7368 slaver
6377 slaver

2.配置并启动Redis主从集群

1.主节点的redis配置文件使用默认的配置文件就可以了,从节点的redis配置文件修改如下:
slaveof 192.168.110.1016379
如果设置了密码:加上masterauth "123456"
2.启动Redis主从集群

cp /usr/local/etc/redis.conf /usr/local/etc/redis-slaver.conf
修改端口号 port 6378
cp /usr/local/etc/redis.conf /usr/local/etc/redis-slaver2.conf
修改端口号 port 6377
/usr/local/opt/redis/bin/redis-server /usr/local/etc/redis.conf
/usr/local/opt/redis/bin/redis-server /usr/local/etc/redis-slaver.conf
/usr/local/opt/redis/bin/redis-server /usr/local/etc/redis-slaver2.conf

3.查看集群
master slaver 主从信息

info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6378,state=online,offset=30454,lag=1
master_replid:171601e906c8f17b62cd5f6ed40017b766a105e0
master_replid2:572cc0ef6a24c28ce519509f1744f08a2de4e3eb
master_repl_offset:30720
second_repl_offset:21451
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:30720
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6377
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1584929168
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:e0a8a7eef0a33bfe252cd8230fa0a55b09a8aa4a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

3.配置sentinel集群并启动

port 26379
# sentinel announce-ip <ip>
# sentinel announce-port <port>
dir /tmp
################################# mymaster #################################
sentinel monitor mymaster 192.168.110.10163792
sentinel auth-pass mynaster 123456
sentinel down-after-milliseconds master001 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
# 可以配置多个master节点
################################# mymaster #################################

配置文件解析:

  1. port :当前Sentinel服务运行的端口
  2. dir : Sentinel服务运行时使用的临时文件夹
  3. sentinel monitor mymaster 127.0.0.1 6379 2 :Sentinel去监视一个名为mymaster的主redis实例,这个主实例的IP地址为本机地址127.0.0.1,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行
  4. sentinel down-after-milliseconds mymaster 30000:指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行
  5. sentinel parallel-syncs mymaster 1:指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
  6. sentinel failover-timeout mymaster 180000:如果在该时间(ms)内未能完成failover操作,则认为该failover失败
  7. sentinel notification-script :指定sentinel检测到该监控的redis实例指向的实例异常时,调用的报警脚本。该配置项可选,但是很常用

4.启动sentinel集群

cp /usr/local/etc/redis-sentinel.conf /usr/local/etc/redis-sentinel-slaver.conf 
端口修改: port 36379
cp /usr/local/etc/redis-sentinel.conf /usr/local/etc/redis-sentinel-slaver2.conf 
端口修改: port 46379
/usr/local/opt/redis/bin/redis-sentinel /usr/local/etc/redis-sentinel.conf 
/usr/local/opt/redis/bin/redis-sentinel /usr/local/etc/redis-sentinel-slaver.conf 
/usr/local/opt/redis/bin/redis-sentinel /usr/local/etc/redis-sentinel-slaver2.conf 

控制台信息:

21480:X 23 Mar 09:21:47.706 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
21480:X 23 Mar 09:21:47.706 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=21480, just started
21480:X 23 Mar 09:21:47.706 # Configuration loaded
21480:X 23 Mar 09:21:47.708 * Increased maximum number of open files to 10032 (it was originally set to 256).
21480:X 23 Mar 09:21:47.709 # Sentinel ID is e8f988a5971944d83325b840b4325cae8eeff3a4
21480:X 23 Mar 09:21:47.709 # +monitor master mymaster 127.0.0.1 6379 quorum 2
21480:X 23 Mar 09:21:47.710 * +slave slave 127.0.0.1:6377 127.0.0.1 6377 @ mymaster 127.0.0.1 6379
21480:X 23 Mar 09:21:47.711 * +slave slave 127.0.0.1:6378 127.0.0.1 6378 @ mymaster 127.0.0.1 6379
21480:X 23 Mar 09:23:11.183 * +sentinel sentinel f00b99ade5de038be968634564ff655908b760e9 127.0.0.1 36379 @ mymaster 127.0.0.1 6379
21480:X 23 Mar 09:23:20.386 * +sentinel sentinel 42a782d60c6e432ce90876951cbc410ad3a1f793 127.0.0.1 46379 @ mymaster 127.0.0.1 6379
21480:X 23 Mar 09:25:00.407 # +sdown master mymaster 127.0.0.1 6379
21480:X 23 Mar 09:25:00.437 # +new-epoch 

5.测试sentinel集群

关闭主服务器 127.0.0.1:6379

ps aux | grep redis
kill -9 xxxx

查看各个服务器的信息

第二台slaver:6377 成为了主服务器

info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6378,state=online,offset=30454,lag=1
master_replid:171601e906c8f17b62cd5f6ed40017b766a105e0
master_replid2:572cc0ef6a24c28ce519509f1744f08a2de4e3eb
master_repl_offset:30720
second_repl_offset:21451
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:30720

第一台slaver:6378还是从服务器

info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6377
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:32729
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:171601e906c8f17b62cd5f6ed40017b766a105e0
master_replid2:572cc0ef6a24c28ce519509f1744f08a2de4e3eb
master_repl_offset:32729
second_repl_offset:21451
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:32729

从控制台可以看出,sentinel集群发生了选举

21641:X 23 Mar 09:23:18.362 # Sentinel ID is 42a782d60c6e432ce90876951cbc410ad3a1f793
21641:X 23 Mar 09:23:18.362 # +monitor master mymaster 127.0.0.1 6379 quorum 2
21641:X 23 Mar 09:23:18.364 * +slave slave 127.0.0.1:6377 127.0.0.1 6377 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:23:18.364 * +slave slave 127.0.0.1:6378 127.0.0.1 6378 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:23:19.292 * +sentinel sentinel f00b99ade5de038be968634564ff655908b760e9 127.0.0.1 36379 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:23:19.715 * +sentinel sentinel e8f988a5971944d83325b840b4325cae8eeff3a4 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.379 # +sdown master mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.435 # +odown master mymaster 127.0.0.1 6379 #quorum 2/2
21641:X 23 Mar 09:25:00.435 # +new-epoch 1
21641:X 23 Mar 09:25:00.435 # +try-failover master mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.436 # +vote-for-leader 42a782d60c6e432ce90876951cbc410ad3a1f793 1
21641:X 23 Mar 09:25:00.437 # e8f988a5971944d83325b840b4325cae8eeff3a4 voted for 42a782d60c6e432ce90876951cbc410ad3a1f793 1
21641:X 23 Mar 09:25:00.438 # f00b99ade5de038be968634564ff655908b760e9 voted for 42a782d60c6e432ce90876951cbc410ad3a1f793 1
21641:X 23 Mar 09:25:00.502 # +elected-leader master mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.502 # +failover-state-select-slave master mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.559 # +selected-slave slave 127.0.0.1:6377 127.0.0.1 6377 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.559 * +failover-state-send-slaveof-noone slave 127.0.0.1:6377 127.0.0.1 6377 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.637 * +failover-state-wait-promotion slave 127.0.0.1:6377 127.0.0.1 6377 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.646 # +promoted-slave slave 127.0.0.1:6377 127.0.0.1 6377 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.646 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:00.696 * +slave-reconf-sent slave 127.0.0.1:6378 127.0.0.1 6378 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:01.520 # -odown master mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:01.687 * +slave-reconf-inprog slave 127.0.0.1:6378 127.0.0.1 6378 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:01.687 * +slave-reconf-done slave 127.0.0.1:6378 127.0.0.1 6378 @ mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:01.753 # +failover-end master mymaster 127.0.0.1 6379
21641:X 23 Mar 09:25:01.753 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6377
21641:X 23 Mar 09:25:01.753 * +slave slave 127.0.0.1:6378 127.0.0.1 6378 @ mymaster 127.0.0.1 6377
21641:X 23 Mar 09:25:01.753 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6377
21641:X 23 Mar 09:25:31.792 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6377
发布了97 篇原创文章 · 获赞 59 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/b1303110335/article/details/105042417
今日推荐