redis主从+高可用切换+负载均衡

1. redis主从配置

bash 复制代码
# 在master中
cp sentinel.conf /etc/redis/
vim /etc/redis/sentinel.conf
scp /etc/redis/sentinel.conf server2:/etc/redis/
scp /etc/redis/sentinel.conf server3:/etc/redis/
redis-sentinel /etc/redis/sentinel.conf # 启动监控
bash 复制代码
# 在slave中
redis-sentinel /etc/redis/sentinel.conf # 启动监控,在这之前一定要同步好sentinel.conf文件

2. 高可用自动切换

2.1 原master down

2.2 原master恢复为slave


2.3 问题

当客户机和master直接网络畅通,而master和slave直接网络故障,在切换主从过程中,客户向原来的master中写数据,会造成丢失。需要限制在切换主从时候添加数据。

bash 复制代码
min-slaves-to-write=2

3. 负载均衡(redis集群)

无中心化,所有节点均可读写

3.1 配置

bash 复制代码
# 停用之前的环境,防止影响实验
/etc/init.d/redis_6379 stop
bash 复制代码
cd utils/create-cluster/
./create-cluster start # 开启集群,该文件是配置文件,可以在里面加入集群的IP

3.2 拉起

bash 复制代码
# 快速拉起集群
./create-cluster create
bash 复制代码
redis-cli --cluster check 127.0.0.1:30001 # 查看集群

3.3 调用

bash 复制代码
redis-cli -c -p 30004 # 进入某个节点

在任意一节点上写入数据,会被重定向到对应的master。

3.4 故障切换

bash 复制代码
redis-cli -c -p 30002 shutdown # 原先的master故障

3.5 集群不可用

当hash表不完整时候,集群不可用

3.6 在线增加节点

bash 复制代码
vim create-cluster
bash 复制代码
./create-cluster start # 在线开启
 redis-cli --cluster help
 redis-cli --cluster add-node 127.0.0.1:30007 127.0.0.1:30001 # 添加节点
bash 复制代码
redis-cli --cluster add-node 127.0.0.1:30008 127.0.0.1:30001 --cluster-slave --cluster-master-id 6857101497d39b5d9ab59fbbd3b533c96baa1b4e # 添加节点同时,将其作为30007的slave
bash 复制代码
# 交互式手动分配hash
redis-cli --cluster reshard 127.0.0.1:30001 # 后面的节点任意
相关推荐
九皇叔叔1 小时前
【9】PostgreSQL 之 vacuum 死元组清理
数据库·postgresql
风雅的远行者2 小时前
mysql互为主从失效,重新同步
数据库·mysql
晨岳2 小时前
CentOS 安装 JDK+ NGINX+ Tomcat + Redis + MySQL搭建项目环境
java·redis·mysql·nginx·centos·tomcat
执笔诉情殇〆2 小时前
前后端分离(java) 和 Nginx在服务器上的完整部署方案(redis、minio)
java·服务器·redis·nginx·minio
宇钶宇夕3 小时前
S7-1200 系列 PLC 中 SCL 语言的 PEEK 和 POKE 指令使用详解
运维·服务器·数据库·程序人生·自动化
绿蚁新亭3 小时前
Spring的事务控制——学习历程
数据库·学习·spring
都叫我大帅哥3 小时前
🌟 Redis缓存与数据库数据一致性:一场数据世界的“三角恋”保卫战
redis
都叫我大帅哥3 小时前
Redis缓存雪崩:一场本可避免的"雪崩"灾难
redis
不像程序员的程序媛3 小时前
redis的一些疑问
java·redis·mybatis
scilwb4 小时前
占用栅格地图数据集
数据库