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 # 后面的节点任意

