redis 哨兵机制环境搭建
Posted exman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis 哨兵机制环境搭建相关的知识,希望对你有一定的参考价值。
Redis哨兵机制,一主二从
注:Redis哨兵切换,建议一主多从
一、一主二从
教程步骤:https://www.cnblogs.com/zwcry/p/9046207.html
二、哨兵配置(sentinel.conf)
cd /usr/local/redis-ms/
1.创建哨兵目录
mkdir -p ./sentinel/26001/tmp
cp /usr/local/redis/sentinel.conf ./sentinel/26001/
2.修改./sentinel/.conf
cd /usr/local/redis-ms/sentinel/
vim ./26001/sentinel.conf
#修改如下键值对
# bind 127.0.0.1 192.168.1.1#注释掉或者值为0.0.0.0
protected-mode no#关闭保护模式
port 26001#端口号
daemonize yes#后台运行
dir /usr/local/redis-ms/sentinel/26001/tmp#解除挂载信息目录
sentinel monitor mymaster 192.168.194.131 6381 1#设置 主名称 ip地址 端口号 参入选举的哨兵数
sentinel down-after-milliseconds mymaster 3000#sentinel心跳检测主3秒内无响应,视为挂掉,开始切换其他从为主
sentinel parallel-syncs mymaster 1#每次最多可以有1个从同步主。一个从同步结束,另一个从开始同步。
sentinel failover-timeout mymaster 18000#主从切换超时时间
三、启动哨兵配置
1.先启主从redis
/usr/local/redis/src/redis-server /usr/local/redis-ms/6381/redis.conf
/usr/local/redis/src/redis-server /usr/local/redis-ms/6382/redis.conf
/usr/local/redis/src/redis-server /usr/local/redis-ms/6383/redis.conf
ps -ef|grep redis
如图已经启动
且信息正确
/usr/local/redis/src/redis-cli -p 6381
info
2.启动哨兵
/usr/local/redis/src/redis-sentinel /usr/local/redis-ms/sentinel/26001/sentinel.conf
ps -ef|grep redis
如图已经启动
且信息正确
/usr/local/redis/src/redis-cli -p 26001
info
3.测试
1)主6381停掉后变成从,原有的两个从,有一个升级为主。
/usr/local/redis/src/redis-cli -p 6381 shutdown
/usr/local/redis/src/redis-cli -p 26001
info
2)6381再次启动后,依然是从
/usr/local/redis/src/redis-server /usr/local/redis-ms/6381/redis.conf
/usr/local/redis/src/redis-cli -p 6381
info
五、哨兵集群
cd /usr/local/redis-ms/sentinel/
1.复制26001
cp -r 26001 26002
2.修改26002/sentinel.conf
vim 26002/sentinel.conf
#将文本中26001替换为26002
:%s/26001/26002/g
3.启动
/usr/local/redis/src/redis-sentinel /usr/local/redis-ms/sentinel/26002/sentinel.conf
info
/usr/local/redis/src/redis-cli -p 26002
6.哨兵开机启动
注:redis开机启动参照https://www.cnblogs.com/zwcry/p/9046207.html
1.创建sh脚本目录
mkdir /usr/local/redis-ms/sentinel/script
cd /usr/local/redis-ms/sentinel/script/
2.编辑start.sh
vim start.sh
#!/bin/sh /usr/local/redis/src/redis-sentinel /usr/local/redis-ms/sentinel/26001/sentinel.conf /usr/local/redis/src/redis-sentinel /usr/local/redis-ms/sentinel/26002/sentinel.conf
3.编辑stop.sh
vim stop.sh
#!/bin/sh /usr/local/redis/src/redis-cli -p 26001 shutdown /usr/local/redis/src/redis-cli -p 26002 shutdown
4.编辑restart.sh
vim restart.sh
#!/bin/sh systemctl stop redis-sentinel systemctl start redis-sentinel
5.编写redis-sentinel.service
cd /usr/lib/systemd/system/
vim redis-sentinel.service
[Unit] Description=redis-sentinel After=redis-ms.service [Service] Type=forking ExecStart=/usr/local/redis-ms/sentinel/script/start.sh ExecStop=/usr/local/redis-ms/sentinel/script/stop.sh ExecReload=/usr/local/redis-ms/sentinel/script/restart.sh [Install] WantedBy=multi-user.target
#建议启动redis服务后,再启动哨兵
6.命令行
改变权限
chmod 777 redis-sentinel.service
chmod 777 /usr/local/redis-ms/sentinel/script/*
进程服务重加载
systemctl daemon-reload
开机启动哨兵
systemctl enable redis-sentinel.service
启动哨兵
systemctl start redis-sentinel.service
关闭哨兵
systemctl stop redis-sentinel.service
重启哨兵
systemctl restart redis-sentinel.service
备注:哨兵集群服务自主关联响应,如果需要更多哨兵集群,按照26002的方式复制26003...26006即可
以上是关于redis 哨兵机制环境搭建的主要内容,如果未能解决你的问题,请参考以下文章