2台真机实战--Redis一主一从两哨兵配置集群和主从切换
- 前言
- 实战
- 真实环境
- 节点分布
- 配置
- 主服务器(192.168.137.23)
- 配置redis.conf
- 配置sentinel.conf
- 从服务器(192.168.137.24)
- 配置redis.conf
- 配置sentinel.conf
- 启动redis
- 整合SpringBoot配置
- 验证
前言
正式环境部署的redis是单例的,突然有一天服务器一个硬盘坏了,好巧不巧,redis的文件都放在这个硬盘上;完了,然后项目各种报错了;好了,现在说正事儿,要求部署redis集群,只有两台服务器,而且资源有限,想法是搞个一主一从两哨兵的模式,这里就做个随记吧,记忆力不好了,也当做是复盘吧。
实战
redis版本是6.2.6源码编译安装,这个就不讲了,安装教程一大堆
真实环境
192.168.137.23 主服务器
192.168.137.24 从服务器
节点分布
服务器 | 端口 | 备注 |
---|---|---|
192.168.137.23 | 6379 | 主节点 |
192.168.137.23 | 26379 | 哨兵1 |
192.168.137.24 | 6379 | 从节点 |
192.168.137.24 | 26379 | 哨兵2 |
配置
主服务器(192.168.137.23)
配置redis.conf
按如下配置进行修改
bind * -::* #设定连接IP
port 6379 #端口号
daemonize yes #允许后台启动
protected-mode no #关闭保护模式;
requirepass 123456 # redis密码 (不配置密码时,忽略)
masterauth 123456 # 主从交互密码 (redis无密码时刻忽略,最好和requirepass一样)
配置sentinel.conf
按如下配置修改
port 26379 #默认端口
daemonize yes #允许后台启动
protected-mode no #关闭保护模式(此配置很重要,不配置将无法实现主从切换)
requirepass 123456 # sentinel密码 (不配置密码时,忽略)
sentinel monitor mymaster 192.168.137.23 6379 1 #配置选举机制,哨兵时刻监控主服务器的6379端口,后面的“1”是当有一票认为它宕机以后就开始主从切换
sentinel auth-pass mymaster 123456 #配置redis交互密码(redis无密码时不需要此配置);
从服务器(192.168.137.24)
配置redis.conf
跟主服务器配置基本一样,就差一个配置slaveof 192.168.137.23 6379
bind * -::* #设定连接IP
port 6379 #端口号
daemonize yes #允许后台启动
protected-mode no #关闭保护模式;
slaveof 192.168.137.23 6379 #声明它是谁的从服务器,除此行外,它和主配置一样;
requirepass 123456 # redis密码 (不配置密码时,忽略)
masterauth 123456 # 主从交互密码 (redis无密码时刻忽略,最好和requirepass一样)
配置sentinel.conf
从服务器哨兵配置与主服务器的哨兵配置完全一样
port 26379 #默认端口
daemonize yes #允许后台启动
protected-mode no #关闭保护模式(此配置很重要,不配置将无法实现主从切换)
requirepass 123456 # sentinel密码 (不配置密码时,忽略)
sentinel monitor mymaster 192.168.137.23 6379 1 #配置选举机制,哨兵时刻监控主服务器的6379端口,后面的“1”是当有一票认为它宕机以后就开始主从切换
sentinel auth-pass mymaster 123456 #配置redis交互密码(redis无密码时不需要此配置);
启动redis
在所有配置修改完成后,启动redis,启动顺序:
主节点->从节点->哨兵
启动命令:
redis: systemctl start redis # redis启动命令
redis-sentinel: redis-sentinel /etc/redis/sentinel.conf #哨兵启动命令,因为我的配置文件是放在/etc/redis/目录下,根据你自己的路径修改
整合SpringBoot配置
spring:
redis:
password: 123456 # redis密码
sentinel:
nodes:
- 192.168.137.23:26379 #哨兵1
- 192.168.137.24:26379 #哨兵2
master: mymaster
password: 123456 # sentinel密码
验证
- 测试主从数据同步
……
49926:S 14 Dec 2022 19:03:06.359 # Error condition on socket for SYNC: Connection refused
49926:S 14 Dec 2022 19:03:07.368 * Connecting to MASTER 192.168.137.23:6379
49926:S 14 Dec 2022 19:03:07.368 * MASTER <-> REPLICA sync started
49926:S 14 Dec 2022 19:03:07.368 # Error condition on socket for SYNC: Connection refused
49926:S 14 Dec 2022 19:03:08.376 * Connecting to MASTER 192.168.137.23:6379
49926:S 14 Dec 2022 19:03:08.376 * MASTER <-> REPLICA sync started
49926:S 14 Dec 2022 19:03:08.376 # Error condition on socket for SYNC: Connection refused
49926:M 14 Dec 2022 19:03:09.200 * Discarding previously cached master state.
49926:M 14 Dec 2022 19:03:09.200 # Setting secondary replication ID to da212d785a8c73c607bc273ca304bc1c7df3e2ee, valid up to offset: 21319. New replication ID is b7fd663ebe164dc1f6221f04cf894832417063f3
49926:M 14 Dec 2022 19:03:09.200 * MASTER MODE enabled (user request from 'id=4 addr=192.168.137.23:51334 laddr=192.168.137.24:6379 fd=9 name=sentinel-d015e455-cmd age=186 idle=0 flags=x db=0 sub=0 psub=0 multi=4 qbuf=188 qbuf-free=40766 argv-mem=4 obl=45 oll=0 omem=0 tot-mem=61468 events=r cmd=exec user=default redir=-1')
49926:M 14 Dec 2022 19:03:09.201 # Could not create tmp config file (Permission denied)
49926:M 14 Dec 2022 19:03:09.201 # CONFIG REWRITE failed: Permission denied
49926:M 14 Dec 2022 19:05:45.761 * Replica 192.168.137.23:6379 asks for synchronization
49926:M 14 Dec 2022 19:05:45.761 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '4a0c5462d86dc1fb2a49e02f6584cb679354c6bf', my replication IDs are 'b7fd663ebe164dc1f6221f04cf894832417063f3' and 'da212d785a8c73c607bc273ca304bc1c7df3e2ee')
49926:M 14 Dec 2022 19:05:45.761 * Starting BGSAVE for SYNC with target: disk
49926:M 14 Dec 2022 19:05:45.764 * Background saving started by pid 49953
49953:C 14 Dec 2022 19:05:45.766 * DB saved on disk
49953:C 14 Dec 2022 19:05:45.766 * RDB: 4 MB of memory used by copy-on-write
49926:M 14 Dec 2022 19:05:45.809 * Background saving terminated with success
49926:M 14 Dec 2022 19:05:45.809 * Synchronization with replica 192.168.137.23:6379 succeeded
49926:M 14 Dec 2022 19:08:06.778 # Connection with replica 192.168.137.23:6379 lost.
49926:M 14 Dec 2022 19:10:19.339 * Replica 192.168.137.23:6379 asks for synchronization
49926:M 14 Dec 2022 19:10:19.339 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for 'bcaa60b2caaa010fcd9ef8b757050dd4e9986a1b', my replication IDs are 'b7fd663ebe164dc1f6221f04cf894832417063f3' and 'da212d785a8c73c607bc273ca304bc1c7df3e2ee')
49926:M 14 Dec 2022 19:10:19.339 * Starting BGSAVE for SYNC with target: disk
49926:M 14 Dec 2022 19:10:19.340 * Background saving started by pid 49956
49956:C 14 Dec 2022 19:10:19.342 * DB saved on disk
49956:C 14 Dec 2022 19:10:19.343 * RDB: 4 MB of memory used by copy-on-write
49926:M 14 Dec 2022 19:10:19.426 * Background saving terminated with success
- 测试主从切换
当主服务器挂掉了,是否会切换到从服务器,并且能读写;当主服务器恢复后从服务器是否会切换回来并且同步从的数据到主;