Redis-哨兵机制,实现高可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis-哨兵机制,实现高可用相关的知识,希望对你有一定的参考价值。
概述
Redis哨兵为Redis提供了高可用性。实际上这意味着你可以使用哨兵模式创建一个可以不用人为干预而应对各种故障的Redis部署。
哨兵模式还提供了其他的附加功能,如监控,通知,为客户端提供配置。
下面是在宏观层面上哨兵模式的功能列表:
监控:哨兵不断的检查master和slave是否正常的运行。
通知:当监控的某台Redis实例发生问题时,可以通过API通知系统管理员和其他的应用程序。
自动故障转移:如果一个master不正常运行了,哨兵可以启动一个故障转移进程,将一个slave升级成为master,其他的slave被重新配置使用新的master,并且应用程序使用Redis服务端通知的新地址。
配置提供者:哨兵作为Redis客户端发现的权威来源:客户端连接到哨兵请求当前可靠的master的地址。如果发生故障,哨兵将报告新地址。
IP划分:
10.0.0.140 redis-Master
10.0.0.141 redis-Slave1
10.0.0.142 redis-Slave2
拓扑图:
操作过程:
三台安装配置过程一样
1.安装依赖包
[[email protected]?~]#?yum?install?gcc?gcc-c++?-y
2.下载安装包,解压
[[email protected]?~]#?cd?/usr/local/src/ [[email protected]?src]#?wget?http://download.redis.io/releases/redis-4.0.1.tar.gz [[email protected]?src]#?tar?-zxvf?redis-4.0.1.tar.gz
3创建redis用户和目录
[[email protected]?~]#?useradd?redis [[email protected]?~]#?mkdir?/usr/local/redis
4.将解压的redis移动到刚创建的目录,并赋予权限
[[email protected]?~]#?cd?/usr/local/src/ [[email protected]?src]#?mv?redis-4.0.1/*?/usr/local/redis [[email protected]?src]#?chown?-R?redis:redis?/usr/local/redis
5.编译
[[email protected]?~]#?cd?/usr/local/redis/ [[email protected]?redis]#?make?MALLOC=libc [[email protected]?redis]#?make?&&?make?install
6.编辑配置文件
[[email protected] ~]# vim /usr/local/redis/redis.conf?
daemonize?yes????#默认no,该参数用于定制redis服务是否以守护模式进行 protected-mode?no???#保护模式是否开启 bind?10.0.0.140?#本机ip logfile?"/var/log/redis/redis.log"???#日志存放位置 dir?/usr/local/redis/rdb???#数据存放位置 supervised?systemd????#systemd?是一个类似?supervise?的守护进程,用于管理需要后台运行的服务。
7.创建配置文件所需的目录,并重新赋权
[[email protected]?~]#?mkdir?/usr/local/redis/rdb [[email protected]?~]#?chown?-R?redis:redis?/usr/local/redis/ [[email protected]?~]#?mkdir?/var/log/redis [[email protected]?~]#?chown?-R?redis:redis?/var/log/redis
8.创建启动脚本,跟脚本执行权限
[[email protected] ~]# cat /usr/lib/systemd/system/redis.service
[Unit] Description=Redis?persistent?key-value?database After=network.target [Service] User=redis Group=redis ExecStart=/usr/local/redis/src/redis-server?/usr/local/redis/redis.conf?--daemonize?no ExecStop=/usr/local/redis/src/redis-cli?-h?10.0.0.140?-p?6379?shutdown Restart=always [Install] WantedBy=multi-user.target
[[email protected] ~]# chmod 750 /usr/lib/systemd/system/redis.service
9.启动测试
[[email protected] ~]#?systemctl enable redis.service
[[email protected] ~]# systemctl start redis.service
[[email protected] ~]# systemctl status redis.service
●?redis.service?-?Redis?persistent?key-value?database ???Loaded:?loaded?(/usr/lib/systemd/system/redis.service;?enabled;?vendor?preset:?disabled) ???Active:?active?(running)?since?一?2017-12-11?13:10:01?CST;?5s?ago ?Main?PID:?4514?(redis-server) ???CGroup:?/system.slice/redis.service ???????????└─4514?/usr/local/redis/src/redis-server?10.0.0.140:6379 12月?11?13:10:01?redis-Master?systemd[1]:?Started?Redis?persistent?key-value?database. 12月?11?13:10:01?redis-Master?systemd[1]:?Starting?Redis?persistent?key-value?database...
三台都要配置哨兵
1.先配置主从(10.0.0.140是Master),另外两台为Slave,
Slave配置如下
[[email protected]?~]#?vim?/usr/local/redis/redis.conf? slaveof?10.0.0.140?6379
[[email protected]?~]#?vim?/usr/local/redis/redis.conf? slaveof?10.0.0.140?6379
2.配置哨兵(只改IP)
[[email protected]~]# vim /usr/local/redis/sentinel.conf?
sentinel?monitor?mymaster?10.0.0.140?6379?2?#(只改IP) protected-mode?no
配置完Redis和Sentinel之后,按顺序启动各个角色。启动顺序如下:
Master->Slave->Sentinel,要确保按照这个顺序依次启动。
Sentinel的启动命令和Redis类似。
测试:
[[email protected] ~]# cd /usr/local/redis/
[[email protected] redis]# redis-sentinel sentinel.conf?
4559:X?11?Dec?13:26:34.859?#?oO0OoO0OoO0Oo?Redis?is?starting?oO0OoO0OoO0Oo 4559:X?11?Dec?13:26:34.859?#?Redis?version=4.0.1,?bits=64,?commit=00000000,?modified=0,?pid=4559,?just?started 4559:X?11?Dec?13:26:34.859?#?Configuration?loaded 4559:X?11?Dec?13:26:34.860?*?Increased?maximum?number?of?open?files?to?10032?(it?was?originally?set?to?1024). ????????????????_._?????????????????????????????????????????????????? ???????????_.-``__?‘‘-._????????????????????????????????????????????? ??????_.-``????`.??`_.??‘‘-._???????????Redis?4.0.1?(00000000/0)?64?bit ??.-``?.-```.??```\/????_.,_?‘‘-._??????????????????????????????????? ?(????‘??????,???????.-`??|?`,????)?????Running?in?sentinel?mode ?|`-._`-...-`?__...-.``-._|‘`?_.-‘|?????Port:?26379 ?|????`-._???`._????/?????_.-‘????|?????PID:?4559 ??`-._????`-._??`-./??_.-‘????_.-‘??????????????????????????????????? ?|`-._`-._????`-.__.-‘????_.-‘_.-‘|?????????????????????????????????? ?|????`-._`-._????????_.-‘_.-‘????|???????????http://redis.io???????? ??`-._????`-._`-.__.-‘_.-‘????_.-‘??????????????????????????????????? ?|`-._`-._????`-.__.-‘????_.-‘_.-‘|?????????????????????????????????? ?|????`-._`-._????????_.-‘_.-‘????|?????????????????????????????????? ??`-._????`-._`-.__.-‘_.-‘????_.-‘??????????????????????????????????? ??????`-._????`-.__.-‘????_.-‘??????????????????????????????????????? ??????????`-._????????_.-‘??????????????????????????????????????????? ??????????????`-.__.-‘??????????????????????????????????????????????? 4559:X?11?Dec?13:26:34.862?#?WARNING:?The?TCP?backlog?setting?of?511?cannot?be?enforced?because?/proc/sys/net/core/somaxconn?is?set?to?the?lower?value?of?128. 4559:X?11?Dec?13:26:34.863?#?Sentinel?ID?is?33890665f4d321bbf024646efdd169a16713e953 4559:X?11?Dec?13:26:34.863?#?+monitor?master?mymaster?10.0.0.140?6379?quorum?2 4559:X?11?Dec?13:26:34.864?*?+slave?slave?10.0.0.141:[email protected]?mymaster?10.0.0.140?6379 4559:X?11?Dec?13:26:34.865?*?+slave?slave?10.0.0.142:[email protected]?mymaster?10.0.0.140?6379 4559:X?11?Dec?13:27:01.069?*?+sentinel?sent[email protected]?mymaster?10.0.0.140?6379 4559:X?11?Dec?13:27:12.489?*?+sentinel?sent[email protected]?mymaster?10.0.0.140?6379
以客户端方式查看主从
[[email protected]?~]#?cd?/usr/local/redis/ [[email protected]?redis]#?src/redis-cli?-h?10.0.0.140?-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=10.0.0.140:6379,slaves=2,sentinels=3
模拟故障将Master,down
哨兵模式会自动切换Master
[[email protected] redis]# src/redis-sentinel sentinel.conf?
14157:X?11?Dec?13:26:59.038?#?oO0OoO0OoO0Oo?Redis?is?starting?oO0OoO0OoO0Oo 14157:X?11?Dec?13:26:59.038?#?Redis?version=4.0.1,?bits=64,?commit=00000000,?modified=0,?pid=14157,?just?started 14157:X?11?Dec?13:26:59.038?#?Configuration?loaded 14157:X?11?Dec?13:26:59.039?*?Increased?maximum?number?of?open?files?to?10032?(it?was?originally?set?to?1024). ????????????????_._?????????????????????????????????????????????????? ???????????_.-``__?‘‘-._????????????????????????????????????????????? ??????_.-``????`.??`_.??‘‘-._???????????Redis?4.0.1?(00000000/0)?64?bit ??.-``?.-```.??```\/????_.,_?‘‘-._??????????????????????????????????? ?(????‘??????,???????.-`??|?`,????)?????Running?in?sentinel?mode ?|`-._`-...-`?__...-.``-._|‘`?_.-‘|?????Port:?26379 ?|????`-._???`._????/?????_.-‘????|?????PID:?14157 ??`-._????`-._??`-./??_.-‘????_.-‘??????????????????????????????????? ?|`-._`-._????`-.__.-‘????_.-‘_.-‘|?????????????????????????????????? ?|????`-._`-._????????_.-‘_.-‘????|???????????http://redis.io???????? ??`-._????`-._`-.__.-‘_.-‘????_.-‘??????????????????????????????????? ?|`-._`-._????`-.__.-‘????_.-‘_.-‘|?????????????????????????????????? ?|????`-._`-._????????_.-‘_.-‘????|?????????????????????????????????? ??`-._????`-._`-.__.-‘_.-‘????_.-‘??????????????????????????????????? ??????`-._????`-.__.-‘????_.-‘??????????????????????????????????????? ??????????`-._????????_.-‘??????????????????????????????????????????? ??????????????`-.__.-‘??????????????????????????????????????????????? 14157:X?11?Dec?13:26:59.041?#?WARNING:?The?TCP?backlog?setting?of?511?cannot?be?enforced?because?/proc/sys/net/core/somaxconn?is?set?to?the?lower?value?of?128. 14157:X?11?Dec?13:26:59.042?#?Sentinel?ID?is?8583cec1a7634843d0141f9a7e00e5ee5f0b4293 14157:X?11?Dec?13:26:59.042?#?+monitor?master?mymaster?10.0.0.140?6379?quorum?2 14157:X?11?Dec?13:26:59.043?*?+slave?slave?10.0.0.141:[email protected]?mymaster?10.0.0.140?6379 14157:X?11?Dec?13:26:59.044?*?+slave?slave?10.0.0.142:[email protected]?mymaster?10.0.0.140?6379 14157:X?11?Dec?13:26:59.359?*?+sentinel?sent[email protected]?mymaster?10.0.0.140?6379 14157:X?11?Dec?13:27:12.474?*?+sentinel?sent[email protected]?mymaster?10.0.0.140?6379 14157:X?11?Dec?13:31:43.607?#?+sdown?master?mymaster?10.0.0.140?6379 14157:X?11?Dec?13:31:43.607?#?+sdown?sent[email protected]?mymaster?10.0.0.140?6379 14157:X?11?Dec?13:31:48.835?#?+new-epoch?1 14157:X?11?Dec?13:31:50.877?#?+vote-for-leader?715f518a156837119ddf282b86b4fae992b4602c?1 14157:X?11?Dec?13:31:50.877?#?+tilt?#tilt?mode?entered 14157:X?11?Dec?13:32:20.887?#?-tilt?#tilt?mode?exited 14157:X?11?Dec?13:32:20.887?#?+odown?master?mymaster?10.0.0.140?6379?#quorum?2/2 14157:X?11?Dec?13:32:20.887?#?Next?failover?delay:?I?will?not?start?a?failover?before?Mon?Dec?11?13:37:51?2017 14157:X?11?Dec?13:32:21.759?#?+config-update-from?sent[email protected]?mymaster?10.0.0.140?6379 14157:X?11?Dec?13:32:21.759?#?+switch-master?mymaster?10.0.0.140?6379?10.0.0.142?6379 14157:X?11?Dec?13:32:21.760?*?+slave?slave?10.0.0.141:[email protected]?mymaster?10.0.0.142?6379 14157:X?11?Dec?13:32:21.760?*?+slave?slave?10.0.0.140:[email protected]?mymaster?10.0.0.142?6379
以客户端方式查看主从,实现了Master的自动切换
[[email protected]?redis]#?src/redis-cli?-h?10.0.0.142?-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=10.0.0.142:6379,slaves=2,sentinels=3
以上是关于Redis-哨兵机制,实现高可用的主要内容,如果未能解决你的问题,请参考以下文章