阅读 149

redis 主从 + 哨兵模式集群部署(3 台机器部署方案)

redis 主从 + 哨兵模式集群部署(3 台机器部署方案)

前言

本文只讲如何部署,然后会贴出具体配置,以及如何验证是否部署成功(redis 版本采用 4.0.6 )。既不会介绍 redis 如何安装(本人采用源码安装方式),也不会涉及主从复制,哨兵模式具体原理及工作机制等内容。 后续会再补充 ~

配置

关于 redis 的配置文件 redis.conf 的配置项,请参考

Redis配置文件详解, 下面贴出的配置只会粗略讲解。

机器分配

名称机器 1机器2机器2
host10.211.55.810.211.55.910.211.55.11
端口637963796379
角色1masterslave 01slave 02
角色2sentinel 01sentinel 02sentinel 03

提示

看到这里,请先提前关闭三台机器的防火墙。防火墙不关闭,会出现 master 的数据不会同步到 slave 节点的情况

-- 关闭防火墙! -- 关闭防火墙! -- 关闭防火墙!

机器1 配置(master)(10.211.55.8)

redis.conf

# 如果为 yes 只能通过(127.0.0.1)访问protected-mode no# 端口port 6379# 用守护线程的方式启动daemonize yes# pid 文件pidfile ./redis.pid# 日志logfile ""# 数据库数量databases 16# 开启 rdb 并设置备份规则save 900 1 
save 300 10save 60 10000 

# rdb 以及 aof 备份文件dir ./# rbd 文件名dbfilename dump.rdb# 密码requirepass abc123!@## 主从配置,slave 访问 master 需要密码masterauth  abc123!@## Sentinel 模块的配置slave-priority 100

sentinel.conf

protected-mode nodaemonize yes# sentinel 端口port 26379dir ./# 注意sentinel monitor mymaster 10.211.55.8 6379 2# 密码sentinel auth-pass mymaster  abc123!@# 
sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000logfile ./sentinel.log

机器 2 配置(slave 01)(10.211.55.9)

slave 节点的配置只比 master 多了一个slaveof 10.211.55.8 6379

redis.conf

protected-mode noport 6379daemonize yespidfile ./redis.pidlogfile ""databases 16save 900 1save 300 10save 60 10000dir ./dbfilename dump.rdbrequirepass abc123!@## 跟 master 的区别,多了此配置slaveof 10.211.55.8 6379masterauth abc123!@#slave-priority 100

sentinel.conf

protected-mode nodaemonize yes# sentinel 端口port 26379dir ./sentinel monitor mymaster 10.211.55.8  6379 2sentinel auth-pass mymaster QpLpYnh619!sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000logfile ./sentinel.log

机器 3 配置 (slave 02) (10.211.55.11)

省略。。 跟机器 2 配置一样!!!! 当然如果密码不一样,你就改一下

验证

请确认防火墙已经关闭 !!!!!!

首先验证主从复制,然后再验证哨兵模式是否成功,

源码安装方式,如果不指定参数,redis 启动脚本默认安装在 /usr/local/bin/ 下面

配置文件还是在 redis 的安装目录

验证主从复制

  1. 进入master 的 redis 解压目录

  2. 启动 redis /usr/local/bin/redis-server redis.conf

  3. 分别进入两个 slave 机器的 redis 目录 ,执行步骤 2

  4. 在 master 上通过客户端连接 redis 服务端,即执行以下命令

     	/usr/local/bin/redis-cli -a abc123![@#](https://my.oschina.net/u/2839266)      
     	------------------
     	abc123![@#](https://my.oschina.net/u/2839266) 是密码
  5. 在 redis 命令行上,执行 info replication 命令, 验证主从关系

     	# Replication 	role:master (当前节点为 master) 	connected_slaves:2   (2 个slave 节点) 	slave0:ip=10.211.55.9,port=6379,state=online,offset=535,lag=1 	slave1:ip=10.211.55.11,port=6379,state=online,offset=535,lag=1 	master_replid:dbe7d07a45a521b1feb44117f9872bfde259aec3 	master_replid2:0000000000000000000000000000000000000000 	master_repl_offset:535 	second_repl_offset:-1 	repl_backlog_active:1 	repl_backlog_size:1048576 	repl_backlog_first_byte_offset:1 	repl_backlog_histlen:535
  6. 在 master 上添加数据,看 slave 上是否会同步数据

验证 sentinel

  1. 分别在三台机器上,进入 redis 解压目录,依次执行

     /usr/local/bin/redis-sentinel sentinel.conf
  2. 连接任意 sentinel 节点,

     redis-cli -h 10.211.55.9 -p 26379 -a abc123![@#](https://my.oschina.net/u/2839266)
  3. 在 redis 命令行上,执行 info sentinel 命令, 验证 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=10.211.55.9:6379,slaves=2,sentinels=3

总结

以上配置,亲测有效! 如有疑问,请留言~

redis 主从 + 哨兵模式集群部署(3 台机器部署方案)

原文:https://www.cnblogs.com/yaoyangding/p/14809199.html


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐