Sentinel介绍
Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:
监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 Sentinel 进程(progress), 这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息, 并使用投票协议(agreement protocols)来决定是否执行自动故障迁移, 以及选择哪个从服务器作为新的主服务器。
虽然 Redis Sentinel 释出为一个单独的可执行文件 redis-sentinel , 但实际上它只是一个运行在特殊模式下的 Redis 服务器, 你可以在启动一个普通 Redis 服务器时通过给定 –sentinel 选项来启动 Redis Sentinel 。
Sentinel架构图
Sentinel服务部署
sentinel服务做redis主从集群监控、提醒、自动故障转移,所以需要体现做一个主从。
本次我做的是在一台服务器上启动三个端口进行测试sentinel服务的切换功能。
主从架构地址端口和角色
IP | 端口 | 角色 | version |
192.168.56.15 | 6379 | master | 3.2.9 |
192.168.56.15 | 6380 | slave | 3.2.9 |
192.168.56.15 | 6381 | slave | 3.2.9 |
192.168.56.15 | 26370 | sentinel | 3.2.9 |
查看一下主从复制状态
[root@local-test-node2 conf]# /usr/local/redis/src/redis-cli info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=4667,lag=1 slave1:ip=127.0.0.1,port=6381,state=online,offset=4667,lag=0 master_repl_offset:4667 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:4666
测试主从同步是否正常
[root@local-test-node2 conf]# /usr/local/redis/src/redis-cli 127.0.0.1:6379> set a '1234' OK
[root@local-test-node2 ~]# /usr/local/redis/src/redis-cli -p 6380 127.0.0.1:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:13671 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6380> get a "1234"
启动sentinel服务
1.创建配置文件。
在安装完成后有一份配置文件,需要手动修改一下参数。
[root@local-test-node2 redis]# pwd /usr/local/redis [root@local-test-node2 redis]# mkdir conf -p [root@local-test-node2 redis]# cp sentinel.conf conf/sentinel.conf.bak [root@local-test-node2 redis]# egrep -v "^#|^$" conf/sentinel.conf.bak >conf/sentinel.conf
2.修改配置文件。
[root@local-test-node2 redis]# vim conf/sentinel.conf
port 26379 dir /tmp daemonize yes logfile "/usr/local/redis/logs/sentinel.log" sentinel monitor mymaster 127.0.0.1 6379 1 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 #sentinel notification-script <master-name> <script-path>
配置文件编写格式如下
sentinel <选项的名字> <主服务器的名字> <选项的值>
monitor:监控master的地址和端口。
down-after-milliseconds:Sentinel 认为服务器已经断线所需的毫秒数。
parallel-syncs:在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步, 这个数字越小, 完成故障转移所需的时间就越长。
failover-timeout:sentinel集群中的sentinel不会再同一时刻并发去failover同一个master,第一个进行failover的sentinel如果失败了,另外一个将会这个设置时间过后进行重新进行failover,以此类推。
notification-script:执行脚本进行一些报警的脚本或者vip切换的使用。
3.启动测试
[root@local-test-node2 redis]# /usr/local/redis/src/redis-sentinel /usr/local/redis/conf/sentinel.conf [root@local-test-node2 redis]# /usr/local/redis/src/redis-cli -p 26379 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=1
关闭master
127.0.0.1:6379> shutdown not connected> quit [root@local-test-node2 conf]# netstat -lntup|grep redis tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 22639/redis-sentine tcp 0 0 192.168.56.15:6380 0.0.0.0:* LISTEN 22594/redis-server tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 22594/redis-server tcp 0 0 192.168.56.15:6381 0.0.0.0:* LISTEN 22599/redis-server tcp 0 0 127.0.0.1:6381 0.0.0.0:* LISTEN 22599/redis-server tcp 0 0 :::26379 :::* LISTEN 22639/redis-sentine
查看slave状态
127.0.0.1:6380> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=1529,lag=0 master_repl_offset:1529 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:1528
已经自动提升为主了。查看一下sentinel状态,发现也变了。
[root@local-test-node2 redis]# /usr/local/redis/src/redis-cli -p 26379 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=1
现在把master启动看看
[root@local-test-node2 conf]# /usr/local/redis/src/redis-server /usr/local/redis/conf/redis6379.conf [root@local-test-node2 conf]# /usr/local/redis/src/redis-cli info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:7507 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
已经成功变成从节点了,现在就待变现在这个sentinel服务已经正常的可以使用。
测试一下是否能把数据复制过来
127.0.0.1:6380> set b '456' OK
[root@local-test-node2 conf]# /usr/local/redis/src/redis-cli get b "456"
数据同步没有问题。