转自:http://www.cnblogs.com/fly-piglet/p/9836314.html
目的:
让看看这篇文章的的人可以知道:软件架构、软件的安装、配置、基本运维的操做、高可用测试、也包含我本身,可以节省对应的时间。html
软件架构:
生产环境使用三台服务器搭建redis哨兵集群,3个redis实例(1主2从)+ 3个哨兵实例。生产环境可以保证在哨兵存活两台的状况下,只有一台redis可以继续提供服务(一主两从三哨兵)web
主虚拟机1 | 从虚拟机2 | 从虚拟机3 |
---|---|---|
172.16.48.129 | 172.16.48.130 | 172.16.48.131 |
软件安装:分别在三台机器上经过yum进行redis的下载和安装以及开机启动
# 添加软件安装源 yum install epel-release # 安装redis yum install redis -y # 启动redis、启动redis哨兵 systemctl start redis systemctl start redis-sentinel # 容许开机启动 systemctl enable redis systemctl enable redis-sentinel # 以后进行配置修改:为哨兵集群,重启启动服务
/etc/redis.conf(主库配置)
# 修改redis配置文件:/etc/redis.conf # 1. 修改绑定ip为服务器内网ip地址,作绑定,三台各自填写各自的ip地址 bind 172.16.48.129 # 2. 保护模式修改成否,容许远程链接 protected-mode no # 4. 设定密码 requirepass "123456789" # 5. 设定主库密码与当前库密码同步,保证从库可以提高为主库 masterauth "123456789" # 6. 打开AOF持久化支持 appendonly yes
/etc/redis.conf(两个从库配置)
基本配置和主库相同,bindip地址各自对应各自的。
须要添加主库同步配置redis
# 主库为主虚拟机1的地址 slaveof 172.16.48.129 6379
/etc/redis-sentinel.conf(哨兵配置)
# 修改redis-sentinel配置文件:/etc/redis-sentinel.conf # 1. 绑定的地址 bind 172.19.131.247 # 2. 保护模式修改成否,容许远程链接 protected-mode no # 3. 设定sentinel myid 每一个都不同,使用yum安装的时候,直接就生成了 sentinel myid 04d9d3fef5508f60498ac014388571e719188527 # 4. 设定监控地址,为对应的主redis库的内网地址 sentinel monitor mymaster 172.16.48.129 6379 2 # 5. 设定5秒内没有响应,说明服务器挂了,须要将配置放在sentinel monitor master 127.0.0.1 6379 1下面 sentinel down-after-milliseconds mymaster 5000 # 6. 设定15秒内master没有活起来,就从新选举主 sentinel failover-timeout mymaster 15000 # 7. 表示若是master从新选出来后,其它slave节点能同时并行重新master同步缓存的台数有多少个,显然该值越大,全部slave节点完成同步切换的总体速度越快,但若是此时正好有人在访问这些slave,可能形成读取失败,影响面会更广。最保定的设置为1,只同一时间,只能有一台干这件事,这样其它slave还能继续服务,可是全部slave所有完成缓存更新同步的进程将变慢。 sentinel parallel-syncs mymaster 2 # 8. 主数据库密码,须要将配置放在sentinel monitor master 127.0.0.1 6379 1下面 sentinel auth-pass mymaster 123456789
注意:含有mymaster的配置,都必须放置在sentinel monitor mymaster 172.16.48.129 6379 2以后,不然会出现问题算法
3. 从新启动
# 启动须要按照Master->Slave->Sentinel的顺序进行启动 # 启动redis systemctl restart redis # 启动redis哨兵 systemctl restart redis-sentinel
高可用测试:
1. 链接redis脚本
# 主虚拟机1 redis-cli -h 172.16.48.129 -p 6379 -a 123456789 # 从虚拟机2 redis-cli -h 172.16.48.130 -p 6379 -a 123456789 # 从虚拟机3 redis-cli -h 172.16.48.131 -p 6379 -a 123456789
2. 同步状态查看
# 链接完成后输入命令 info replication # 主库显示以下,便可算完成(包含两个从库ip地址) # Replication role:master connected_slaves:2 slave0:ip=172.16.48.131,port=6379,state=online,offset=188041,lag=1 slave1:ip=172.16.48.130,port=6379,state=online,offset=188041,lag=1 master_repl_offset:188041 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:188040 # 从库显示以下,便可算完成 # Replication role:slave master_host:172.16.48.129 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:174548 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
3. 主库写入测试同步
# 主虚拟机1 set b b # 从虚拟机2 keys * get b # 从虚拟机3 keys * get b
4. 从库只读测试
# 从虚拟机2 set c c # result : (error) READONLY You can't write against a read only slave. # 从虚拟机3 set c c # result : (error) READONLY You can't write against a read only slave.
5. 成功redis-sentinel日志
# 查看日志: tailf /var/log/redis/sentinel.log
成功日志,+slave slave包含两台从库的地址,+sentinel sentinel包含两台哨兵的idspring
57611:X 21 Oct 02:03:27.777 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 57611:X 21 Oct 02:03:27.777 # Sentinel ID is 42975048e2f70d0f4d718f77427930c16bc0b522 57611:X 21 Oct 02:03:27.777 # +monitor master mymaster 172.16.48.129 6379 quorum 2 57611:X 21 Oct 02:03:27.778 * +slave slave 172.16.48.131:6379 172.16.48.131 6379 @ mymaster 172.16.48.129 6379 57611:X 21 Oct 02:03:27.779 * +slave slave 172.16.48.130:6379 172.16.48.130 6379 @ mymaster 172.16.48.129 6379 57611:X 21 Oct 02:03:29.767 * +sentinel sentinel 29222b827e3739b564939c6f20eb610802b48706 172.16.48.130 26379 @ mymaster 172.16.48.129 6379 57611:X 21 Oct 02:03:29.769 * +sentinel sentinel ea3c41804d2840a4393bbdaf0f32dab321267a9c 172.16.48.131 26379 @ mymaster 172.16.48.129 6379
6. 成功sentinel的链接状态
# 主虚拟机1 redis-cli -h 172.16.48.129 -p 26379 INFO Sentinel # result: # 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=172.16.48.129:6379,slaves=2,sentinels= # 从虚拟机2 redis-cli -h 172.16.48.130 -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=172.16.48.129:6379,slaves=2,sentinels=3 # 从虚拟机3 redis-cli -h 172.16.48.131 -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=172.16.48.129:6379,slaves=2,sentinels=3
7. 高可用测试case
哨兵做为对redis实例的监控,经过选举算法保证哨兵的鲁棒性和高可用,因此哨兵至少要部署3台,符合半数原则,须要5或者,7,超过一半,不包含一半存活的时候,才可以选举出leader,才能进行主从的切换功能。
redis服务,至少须要存活一台,才能保证服务正常运行sentinel 选择新 master 的原则是最近可用 且 数据最新 且 优先级最高 且 活跃最久 !
哨兵高可用测试:分别链接对应的redis服务端,手动中止哨兵,中止主reids服务,看主从是否切换成功。
三哨兵状况:redis实例挂掉两台,剩下一台可以成为主,自动切换数据库
# 保持三个哨兵进程都存在的状况下 # 1. 三个终端分别链接redis,使用info replication查看当前链接状态: # 主虚拟机1 redis-cli -h 172.16.48.129 -p 6379 -a 123456789 info replication # 从虚拟机2 redis-cli -h 172.16.48.130 -p 6379 -a 123456789 info replication # 从虚拟机3 redis-cli -h 172.16.48.131 -p 6379 -a 123456789 info replication # 2. 中止当前role:master的对应的redis服务,从新检查状态看是否切换 systemctl stop redis # 虚拟机1的实例转换成为主的redis # 3. 继续中止剩下两台:role:master的对应的redis服务,从新检查链接状态看是否切换 systemctl stop redis # 虚拟机3的实例转换成为了主的redis # 切换顺利,实现高可用
两哨兵状况:redis实例挂掉两台,剩下一台可以成为主,自动切换缓存
# 将所有虚拟机的redis + sentinel从新启动 systemctl start redis systemctl start redis-sentinel # 中止虚拟机1的redis-sentinel,从新执行哨兵的案例测试 systemctl stop redis-sentinel # 分别中止对应master实例的redis,最终剩下一台实例,成为了master,可以自动切换
一哨兵状况:redis实例没法主从切换bash
# 将所有虚拟机的redis + sentinel从新启动 systemctl start redis systemctl start redis-sentinel # 中止虚拟机1和2d的redis-sentinel,从新执行哨兵的案例测试 systemctl stop redis-sentinel # 分别中止对应master实例的redis,最终剩下一台实例,没法实现主从切换
8. web服务链接测试
建立一个web项目,使用项目进行服务器链接验证,暂时不提供。使用spring boot + spring-data-redis进行测试服务器
基本运维操做
基本命令操做
# 启动 systemctl start redis systemctl start redis-sentinel # 重启 systemctl restart redis systemctl restart redis-sentinel # 中止 systemctl stop redis systemctl stop redis-sentinel # 开机启动 systemctl enable redis systemctl enable redis-sentinel # 关闭开机启动 systemctl disable redis systemctl disable redis-sentinel # 卸载,中止redis服务,sentinel服务以后,关闭开机启动,进行卸载 yum remove redis -y
配置文件目录
redis配置文件:/etc/redis.conf
redis-sentinel配置文件:/etc/redis-sentinel.confmarkdown
日志文件目录
redis日志:/var/log/redis/redis.log
redis-sentinel日志:/var/log/redis/sentinel.log
持久化文件目录
redis-dump文件:/var/lib/redis/dump.rdb
redis-appendonly文件:/var/lib/redis/appendonly.aof
参考教程
在Centos中yum安装和卸载软件的使用方法
Markdown插入表格语法
配置sentinel down-after-milliseconds mymaster 5000报错,解决方案
Redis 高可用之 Sentinel 部署与原理