Redis高可用架构-一主两从三哨兵

444 阅读2分钟

今天完成架构图中的redis高可用哨兵模式。 redis采用最新稳定版本5.0.7

项目架构图

机器规划

主机名 IP地址 用途 其他
0_redis_master1 192.168.1.210/24 redis主
0_redis_slave1 192.168.1.211/24 redis从1
0_redis_slave2 192.168.1.212/24 redis从2
0_redis_sentinel1 192.168.1.217/24 哨兵1
0_redis_sentinel2 192.168.1.218/24 哨兵2
0_redis_sentinel3 192.168.1.219/24 哨兵3

Redis主从搭建

主安装配置
# 编译安装
lqh@lqh:/srv/ftp/software$ for i in 0 1 2 7 8 9; do ssh root@192.168.1.21$i 'yum install -y gcc && tar -xf redis-5.0.7.tar.gz && cd redis-5.0.7 && make && make install';done
# 安装配置redis主
[root@redis_master1 utils]# ./install_server.sh 
# 编辑主库配置文件
[root@redis_master1 utils]# vim /etc/redis/6379.conf
bind 0.0.0.0 # 监听地址
requirepass 123456 # 设置验证密码
logfile "/var/log/redis.log" #日志文件
appendonly yes #开启AOF持久化
appendfsync always #发生改变即刻同步,性能略低于everysec
# 备注: 一般情况下官方推荐使用每秒同步。If unsure, use "everysec". 
# 修改服务配置文件
[root@redis_master1 ~]# vim /etc/init.d/redis_6379
PASS=123456 # 开头添加PASS变量
$CLIEXEC -p $REDISPORT -a $PASS shutdown # 停止指令中使用身份认证
[root@redis_master1 utils]# systemctl start redis_6379
[root@redis_master1 utils]# systemctl status redis_6379
● redis_6379.service - LSB: start and stop redis_6379
   Loaded: loaded (/etc/rc.d/init.d/redis_6379; bad; vendor preset: disabled)
   Active: active (running) since Fri 2020-02-21 14:53:36 CST; 6s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 16026 ExecStop=/etc/rc.d/init.d/redis_6379 stop (code=exited, status=0/SUCCESS)
  Process: 16140 ExecStart=/etc/rc.d/init.d/redis_6379 start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/redis_6379.service
           └─16142 /usr/local/bin/redis-server 0.0.0.0:6379

Feb 21 14:53:36 redis_master1 systemd[1]: Starting LSB: start and stop redis_6379...
Feb 21 14:53:36 redis_master1 redis_6379[16140]: Starting Redis server...
Feb 21 14:53:36 redis_master1 systemd[1]: Started LSB: start and stop redis_6379.
# 设置为开机启动
[root@redis_master1 utils]# systemctl enable redis_6379
redis_6379.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig redis_6379 on
从安装配置:相比主就多了slaveof 和 masterauth,其他的和master一致就好。顺便把master的启动文件scp到两台从即刻。

请注意:这里有坑,后面遇到了后面解决的,请注意后面!

[root@redis_slave1 utils]# vim /etc/redis/6379.conf
slaveof 192.168.1.210 6379
masterauth 123456
# 验证一下启动成功了没
[root@redis_slave1 utils]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.1.210
master_port:6379
master_link_status:up
# 最后不要忘记添加开机启动
[root@redis_slave1 utils]# systemctl enable redis_6379
# 最后的主从测试

# master上设置
[root@redis_master1 ~]# redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set keya test
OK
# slave1上面查
[root@redis_slave1 ~]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> get keya
"test"
# slave2上面查
[root@redis_slave2 utils]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> get keya
"test"

多哨兵模式搭建

哨兵信息
哨兵主机 哨兵端口
哨兵1:217 56667
哨兵2: 218 56668
哨兵3:219 56669
哨兵安装
  • 安装和redis一样,只是不用执行安装脚本 还是需要编译的。
lqh@lqh:/srv/ftp/software$ for i in 7 8 9;do ssh root@192.168.1.21$i 'mkdir /etc/redis && cp ~/redis-5.0.7/sentinel.conf /etc/redis/';done
# 编辑配置文件
[root@redis_sentinel1 ~]# vim /etc/redis/sentinel.conf
bind 0.0.0.0 # 监听地址
port 56667   # 监听端口
logfile "/usr/log/redis_sentinel.log" # 日志文件
sentinel monitor redis_master1 192.168.1.210 6379 2 
# 监视谁 取个名字 IP地址 端口 几个哨兵同意允许再次选举主服务器通常为 哨兵总数/2 + 1(遵循投票过半原则)
sentinel auth-pass redis_master1 123456 # 身份认证
sentinel down-after-milliseconds redis_master1 5000 # ping多久不通则认为master down掉了毫秒单位
sentinel parallel-syncs redis_master1 1 #指定可以有多少个Redis服务同步新的主机,因为3台那么掉了1台,就还剩下一台从了,默认即可。
sentinel failover-timeout redis_master1 180000 #超过3分组则故障转移失败。
  • 基本配置一样只需要改端口,我直接scp过去好了。
  • 启动三台redis和哨兵,进行测试关闭master的主机,看是否切换成功。
> 192.168.1.211@6379 connected!
> info replication
# Replication
role:slave
master_host:192.168.1.210
master_port:6379
  • 现在关闭master机器,再查询哨兵选举情况。(测试切换成功)
> info replication
# Replication
role:slave
master_host:192.168.1.212
master_port:6379
  • 启动master,查看master角色,注意与master的连接是关闭的。(一想可能是身份认证,master没有配置从连主的身份认证)
# Replication
role:slave
master_host:192.168.1.212
master_port:6379
master_link_status:down
  • 这个时候去查看日志,遇到问题的解决思路:大胆猜想 -> 寻求真理-> 谨慎处理 -> 反复验证 -> 总结分析 MASTER aborted replication with an error: NOAUTH Authentication required
  • 但是重要的来了,当master机器恢复的时候,无法同步所以导致master_link_status:down
  • 解决:
[root@redis_master1 log]# echo 'masterauth 123456' >> /etc/redis/6379.conf 
[root@redis_master1 log]# systemctl restart redis_6379
[root@redis_master1 log]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.1.212
master_port:6379
master_link_status:up
写入开机脚本中。
[root@redis_sentinel1 redis-5.0.7]# chmod +x /etc/rc.d/rc.local
[root@redis_sentinel1 redis-5.0.7]# echo 'redis-sentinel /etc/redis/sentinel.conf '>> /etc/rc.d/rc.local