1.集群环境

1.Linux服务器列表
使用4台CentOS Linux服务器搭建环境,其IP地址如下:
  1. 192.168.110.100
  2. 192.168.110.101
  3. 192.168.110.102
  4. 192.168.110.103
2.Redis服务部署环境
192.168.110.100
启动多个Redis sentinel服务,构成Redis sentinel集群
192.168.110.101
启动Redis服务,设置成主节点
192.168.110.102
启动Redis服务,设置成192.168.110.101的从节点
192.168.110.103
启动Redis服务,设置成192.168.110.101的从节点

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

1.修改redis.conf配置文件
主节点的redis配置文件使用默认的配置文件就可以了,从节点的redis配置文件修改如下:
  1. # Master-Slave replication. Use slaveof to make a Redis instance a copy of
  2. # another Redis server. A few things to understand ASAP about Redis replication.
  3. #
  4. # 1) Redis replication is asynchronous, but you can configure a master to
  5. # stop accepting writes if it appears to be not connected with at least
  6. # a given number of slaves.
  7. # 2) Redis slaves are able to perform a partial resynchronization with the
  8. # master if the replication link is lost for a relatively small amount of
  9. # time. You may want to configure the replication backlog size (see the next
  10. # sections of this file) with a sensible value depending on your needs.
  11. # 3) Replication is automatic and does not need user intervention. After a
  12. # network partition slaves automatically try to reconnect to masters
  13. # and resynchronize with them.
  14. #
  15. # 主从同步。通过 slaveof 配置来实现Redis实例的备份。
  16. # 注意,这里是本地从远端复制数据。也就是说,本地可以有不同的数据库文件、绑定不同的IP、监听不同的端口。
  17. #
  18. # slaveof <masterip> <masterport>
  19. slaveof 192.168.110.1016379
注意:两台从节点都要改。
2.启动Redis主从集群
先启动192.168.110.101主节点,使用默认配置,脚本:
  1. [wch@localhost bin]$ ./redis-server
再启动192.168.110.102和192.168.110.103从节点,使用刚才的配置,脚本:
  1. ./redis-server redis.conf
3.查看集群
192.168.110.101主节点Replication信息
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
  2. # Replication
  3. role:master
  4. connected_slaves:2
  5. slave0:ip=192.168.110.102,port=6379,state=online,offset=659,lag=1
  6. slave1:ip=192.168.110.103,port=6379,state=online,offset=659,lag=0
  7. master_repl_offset:659
  8. repl_backlog_active:1
  9. repl_backlog_size:1048576
  10. repl_backlog_first_byte_offset:2
  11. repl_backlog_histlen:658
192.168.110.102从节点Replication信息
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
  2. # Replication
  3. role:slave
  4. master_host:192.168.110.101
  5. master_port:6379
  6. master_link_status:up
  7. master_last_io_seconds_ago:3
  8. master_sync_in_progress:0
  9. slave_repl_offset:701
  10. slave_priority:100
  11. slave_read_only:1
  12. connected_slaves:0
  13. master_repl_offset:0
  14. repl_backlog_active:0
  15. repl_backlog_size:1048576
  16. repl_backlog_first_byte_offset:0
  17. repl_backlog_histlen:0
192.168.110.103从节点Replication信息
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
  2. # Replication
  3. role:slave
  4. master_host:192.168.110.101
  5. master_port:6379
  6. master_link_status:up
  7. master_last_io_seconds_ago:9
  8. master_sync_in_progress:0
  9. slave_repl_offset:715
  10. slave_priority:100
  11. slave_read_only:1
  12. connected_slaves:0
  13. master_repl_offset:0
  14. repl_backlog_active:0
  15. repl_backlog_size:1048576
  16. repl_backlog_first_byte_offset:0
  17. repl_backlog_histlen:0
此时,存储到192.168.110.101主节点的数据,在从节点中都可以查询到。从节点会备份主节点的数据。

3.配置sentinel集群并启动

1.创建sentinel.conf配置文件
  1. port 26379
  2. # sentinel announce-ip <ip>
  3. # sentinel announce-port <port>
  4. dir /tmp
  5. ################################# master001 #################################
  6. sentinel monitor master001 192.168.110.10163792
  7. # sentinel auth-pass <master-name> <password>
  8. sentinel down-after-milliseconds master001 30000
  9. sentinel parallel-syncs master001 1
  10. sentinel failover-timeout master001 180000
  11. # sentinel notification-script <master-name> <script-path>
  12. # sentinel client-reconfig-script <master-name> <script-path>
  13. # 可以配置多个master节点
  14. ################################# master002 #################################
配置文件说明:
1. port :当前Sentinel服务运行的端口
 
2. dir : Sentinel服务运行时使用的临时文件夹
 
3.sentinel monitor master001 192.168.110.10163792:Sentinel去监视一个名为master001的主redis实例,这个主实例的IP地址为本机地址192.168.110.101,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行
 
4.sentinel down-after-milliseconds master001 30000:指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行
 
5.sentinel parallel-syncs master001 1:指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
 
6.sentinel failover-timeout master001 180000:如果在该时间(ms)内未能完成failover操作,则认为该failover失败
 
7.sentinel notification-script <master-name> <script-path>:指定sentinel检测到该监控的redis实例指向的实例异常时,调用的报警脚本。该配置项可选,但是很常用
2.启动sentinel集群
创建3个sentinel.conf配置文件:sentinel001.conf、sentinel002.conf、sentinel003.conf并修改端口号分别为:26379、36379、46379,并启动服务:
  1. ./redis-sentinel sentinel001.conf
  2. ./redis-sentinel sentinel002.conf
  3. ./redis-sentinel sentinel003.conf
启动三个sentinel服务后会在其控制台看到如下信息:
./redis-sentinel sentinel001.conf,端口:26379
  1. [7743]01Oct06:20:38.162# Sentinel runid is ba6c42e1accc31290e11d5876275e1562564295d
  2. [7743]01Oct06:20:38.162# +monitor master master001 192.168.110.101 6379 quorum 2
  3. [7743]01Oct06:20:39.110*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
  4. [7743]01Oct06:20:39.111*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
  5. [7743]01Oct06:25:07.595*+sentinel sentinel 192.168.110.100:36379192.168.110.10036379@ master001 192.168.110.1016379
  6. [7743]01Oct06:26:11.170*+sentinel sentinel 192.168.110.100:46379192.168.110.10046379@ master001 192.168.110.1016379
./redis-sentinel sentinel002.conf,端口:36379
  1. [7795]01Oct06:25:05.538# Sentinel runid is 52c14768b15837fb601b26328acf150c6bd30682
  2. [7795]01Oct06:25:05.538# +monitor master master001 192.168.110.101 6379 quorum 2
  3. [7795]01Oct06:25:06.505*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
  4. [7795]01Oct06:25:06.515*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
  5. [7795]01Oct06:25:07.557*+sentinel sentinel 192.168.110.100:26379192.168.110.10026379@ master001 192.168.110.1016379
  6. [7795]01Oct06:26:11.168*+sentinel sentinel 192.168.110.100:46379192.168.110.10046379@ master001 192.168.110.1016379
./redis-sentinel sentinel003.conf,端口:46379
  1. [7828]01Oct06:26:09.076# Sentinel runid is c8509594be4a36660b2122b3b81f4f74060c9b04
  2. [7828]01Oct06:26:09.076# +monitor master master001 192.168.110.101 6379 quorum 2
  3. [7828]01Oct06:26:10.063*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
  4. [7828]01Oct06:26:10.071*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
  5. [7828]01Oct06:26:11.516*+sentinel sentinel 192.168.110.100:26379192.168.110.10026379@ master001 192.168.110.1016379
  6. [7828]01Oct06:26:11.674*+sentinel sentinel 192.168.110.100:36379192.168.110.10036379@ master001 192.168.110.1016379
每个sentinel服务能知道其他所有的服务!

4.测试sentinel集群

1.停止192.168.110.101主节点
停止192.168.110.101Redis主节点后,在查看Replication信息如下:
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
  2. Could not connect to Redis at 192.168.110.101:6379:Connection refused
  3. [wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
  4. # Replication
  5. role:slave
  6. master_host:192.168.110.103
  7. master_port:6379
  8. master_link_status:up
  9. master_last_io_seconds_ago:1
  10. master_sync_in_progress:0
  11. slave_repl_offset:29128
  12. slave_priority:100
  13. slave_read_only:1
  14. connected_slaves:0
  15. master_repl_offset:0
  16. repl_backlog_active:0
  17. repl_backlog_size:1048576
  18. repl_backlog_first_byte_offset:0
  19. repl_backlog_histlen:0
  20. [wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
  21. # Replication
  22. role:master
  23. connected_slaves:1
  24. slave0:ip=192.168.110.102,port=6379,state=online,offset=30456,lag=1
  25. master_repl_offset:30456
  26. repl_backlog_active:1
  27. repl_backlog_size:1048576
  28. repl_backlog_first_byte_offset:2
  29. repl_backlog_histlen:30455
  30. [wch@localhost bin]$
发现192.168.110.101Redis主节点已经不能连接,192.168.110.103成了主节点!
2.再启动192.168.110.101主节点
再启动192.168.110.101Redis主节点后,在查看Replication信息如下:
  1. ### 启动脚本,仍然使用默认配置
  2. [wch@localhost bin]$ ./redis-server
  3. [wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
  4. # Replication
  5. role:slave
  6. master_host:192.168.110.103
  7. master_port:6379
  8. master_link_status:up
  9. master_last_io_seconds_ago:1
  10. master_sync_in_progress:0
  11. slave_repl_offset:57657
  12. slave_priority:100
  13. slave_read_only:1
  14. connected_slaves:0
  15. master_repl_offset:0
  16. repl_backlog_active:0
  17. repl_backlog_size:1048576
  18. repl_backlog_first_byte_offset:0
  19. repl_backlog_histlen:0
  20.  
  21. [wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
  22. # Replication
  23. role:slave
  24. master_host:192.168.110.103
  25. master_port:6379
  26. master_link_status:up
  27. master_last_io_seconds_ago:0
  28. master_sync_in_progress:0
  29. slave_repl_offset:60751
  30. slave_priority:100
  31. slave_read_only:1
  32. connected_slaves:0
  33. master_repl_offset:0
  34. repl_backlog_active:0
  35. repl_backlog_size:1048576
  36. repl_backlog_first_byte_offset:0
  37. repl_backlog_histlen:0
  38.  
  39. [wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
  40. # Replication
  41. role:master
  42. connected_slaves:2
  43. slave0:ip=192.168.110.102,port=6379,state=online,offset=63247,lag=1
  44. slave1:ip=192.168.110.101,port=6379,state=online,offset=63247,lag=1
  45. master_repl_offset:63393
  46. repl_backlog_active:1
  47. repl_backlog_size:1048576
  48. repl_backlog_first_byte_offset:2
  49. repl_backlog_histlen:63392
  50. [wch@localhost bin]$
发现192.168.110.101节点启动后还再集群中,只不过成了从节点,192.168.110.103仍然是主节点,但是现在又有两个从节点了!
3.只留下一个sentinel服务,再停止192.168.110.103主节点,查看Redis集群是否出现新的主节点
停止sentinel服务,只留下一个sentinel服务,再停止Redis主节点,查看Replication信息如下:
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
  2. # Replication
  3. role:slave
  4. master_host:192.168.110.103
  5. master_port:6379
  6. master_link_status:down
  7. master_last_io_seconds_ago:-1
  8. master_sync_in_progress:0
  9. slave_repl_offset:184231
  10. master_link_down_since_seconds:43
  11. slave_priority:100
  12. slave_read_only:1
  13. connected_slaves:0
  14. master_repl_offset:0
  15. repl_backlog_active:0
  16. repl_backlog_size:1048576
  17. repl_backlog_first_byte_offset:0
  18. repl_backlog_histlen:0
  19. [wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
  20. # Replication
  21. role:slave
  22. master_host:192.168.110.103
  23. master_port:6379
  24. master_link_status:down
  25. master_last_io_seconds_ago:-1
  26. master_sync_in_progress:0
  27. slave_repl_offset:184231
  28. master_link_down_since_seconds:52
  29. slave_priority:100
  30. slave_read_only:1
  31. connected_slaves:0
  32. master_repl_offset:0
  33. repl_backlog_active:0
  34. repl_backlog_size:1048576
  35. repl_backlog_first_byte_offset:0
  36. repl_backlog_histlen:0
  37. [wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
  38. Could not connect to Redis at 192.168.110.103:6379:Connection refused
发现192.168.110.103主节点已经不能连接了,也不存在Redis主节点,集群中无主节点了!!!分析原因是:sentinel.conf配置的sentinel monitor master001 192.168.110.10163792最后一个参数是2导致,若是但节点此配置的最后一个参数要使用是1。(此原因我已证实)
注意:在生产环境下建议sentinel节点的数量能在3个以上,并且最好不要在同一台机器上(使用同一网卡)。
-------------------------------------------------------------------------------------------------------------------------------

02.Redis主从集群的Sentinel配置的更多相关文章

  1. Redis主从集群的Sentinel配置

    http://www.cnblogs.com/LiZhiW/p/4851631.html

  2. docker搭建redis主从集群和sentinel哨兵集群,springboot客户端连接

    花了两天搭建redis主从集群和sentinel哨兵集群,讲一下springboot客户端连接测试情况 redis主从集群 从网上查看说是有两种方式:一种是指定配置文件,一种是不指定配置文件 引用地址 ...

  3. Redis主从集群以及Sentinel的配置

    安装完redis后,修改几个redis从节点的配置文件redis.conf,主要是加入主节点位置 slaveof 另外需要修改的地方包括,这样允许其他的从节点连入 bind 0.0.0.0 prote ...

  4. 一、全新安装搭建redis主从集群

    前言· 这里分为三篇文章来写我是如何重新搭建redis主从集群和哨兵集群的及原本服务器上有单redis如何通过升级脚本来实现redis集群.(redis结构:主-从(备)-从(备)) 至于为什么要搭建 ...

  5. redis主从集群搭建及容灾部署(哨兵sentinel)

    Redis也用了一段时间了,记录一下相关集群搭建及配置详解,方便后续使用查阅. 提纲 Redis安装 整体架构 Redis主从结构搭建 Redis容灾部署(哨兵sentinel) Redis常见问题 ...

  6. Redis主从集群及哨兵模式

    本次实验环境准备用一台服务器模拟3台redis服务器,1主2从 主从集群搭建 第一步:安装Redis 安装Redis,参考前面安装Redis文章,保证单机使用没有问题. 第二步:配置服务器文件 定位到 ...

  7. Redis Cluster集群搭建与配置

    Redis Cluster是一种服务器sharding分片技术,关于Redis的集群方案应该怎么做,请参考我的另一篇博客http://www.cnblogs.com/xckk/p/6134655.ht ...

  8. Redis 主从集群搭建及哨兵模式配置

    最近搭建了redis集群及哨兵模式,为方便以后查看特此记录下来: 1.Redis安装 2.主从架构 2.1 Redis主从架构图 2.2Redis主从结构搭建 Redis集群不用安装多个Redis,只 ...

  9. Redis主从,集群部署及迁移

    工作中有时会遇到需要把原Redis集群下线,迁移到另一个新的Redis集群的需求(如机房迁移,Redis上云等原因).此时原Redis中的数据需要如何操作才可顺利迁移到一个新的Redis集群呢? 本节 ...

随机推荐

  1. SQL Server存储过程(转载)

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  2. Java打印温度转换表

    按5度的增量打印出一个从摄氏温度到华氏温度的转换表.转换公式为h=c*9/5+32,其中h为华氏温度,c为摄氏温度. 主要是“按5度的增量”这个要求,一般摄氏温度的起始分别为0度和40度,所以循环可以 ...

  3. scrapy爬虫框架入门教程

    scrapy安装请参考:安装指南. 我们将使用开放目录项目(dmoz)作为抓取的例子. 这篇入门教程将引导你完成如下任务: 创建一个新的Scrapy项目 定义提取的Item 写一个Spider用来爬行 ...

  4. python爬虫-urllib模块

    urllib 模块是一个高级的 web 交流库,其核心功能就是模仿web浏览器等客户端,去请求相应的资源,并返回一个类文件对象.urllib 支持各种 web 协议,例如:HTTP.FTP.Gophe ...

  5. 实战MySQL集群,试用CentOS 6下的MariaDB-Galera集成版

    说起mysql的集群估计很多人会首先想起mysql自带的replication或者mysql-mmm.mysql-mmm其实也是基于mysql自带的replication的,不过封装的更好用一些,但是 ...

  6. oracle 表迁移方法 (二) 约束不失效

    DB:11.2.0.3.0 在oracle 表迁移方法 (一)中,只是move了一张普通的表,如果表的字段带有主键约束呢 ? [oracle@db01 ~]$ sqlplus / as sysdba ...

  7. Tornado服务器的学习

    Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的框架)有着明显的区别:它是非阻塞 ...

  8. 六大Nagios常见问题解决办法

    Nagios常见问题1: It appears as though you do not have permission to view information for any of the host ...

  9. XCode6之后预编译文件的创建

    首先,在你的项目创建一个.pch预编译头文件(一直点Next)

  10. [转]windows 软链接的建立及删除

    [转]windows 软链接的建立及删除 http://blog.chinaunix.net/uid-74941-id-3764093.html 1.建立举例 ##建立d:develop链接目录,指向 ...