今日目标
- 掌握Docker-compose安装Redis哨兵集群
单节点Redis的并发能力是有上限的,要进一步提高Redis的并发能力,就需要搭建主从集群,实现读写分离
- 主节点: 可以对Redis实现读写操作
- 从节点: 只可以对Redis实现读操作
但是,当master节点宕机后,我们就不能写数据到Redis,所以需要搭建一个三节点形成的Sentinel集群,来监管之前的Redis主从集群
1.Redis哨兵
Redis提供了哨兵(Sentinel)机制来实现主从集群的自动故障恢复。
哨兵的结构如图:
图片
共包含6个节点,一个主两个从和三个哨兵。这里我们会在同一台虚拟机中开启6个redis实例,模拟主从集群,信息如下:
IP | PORT | 角色 |
192.168.150.102 | 6380 | master |
192.168.150.102 | 6381 | slave |
192.168.150.102 | 6382 | slave |
192.168.150.102 | 26379 | sentinel |
192.168.150.102 | 26380 | sentinel |
192.168.150.102 | 26381 | sentinel |
哨兵的作用如下:
- 监控:Sentinel 会不断检查您的master和slave是否按预期工作
- 自动故障恢复:如果master故障Sentinel会将一个slave提升为master。当故障实例恢复后也以新的master为主
- 通知:Sentinel充当Redis客户端的服务发现来源,当集群发生故障转移时会将最新信息推送给Redis的客户端
2. 准备实例和配置
要在同一台虚拟机开启6个实例,必须准备6份不同的配置文件和目录,配置文件所在目录也就是工作目录。 确保下面的配置已经更改(位置也别错)
# master
mkdir -p /redis-master-slave-sentinel/redis/master/config /redis-master-slave-sentinel/redis/master/data
cd /redis-master-slave-sentinel/redis/master/config
curl -O -L http://www.zzvips.com/uploads/allimg/lafgs33srxv.gz
tar -zxvf redis-6.0.20.tar.gz
cp /redis-master-slave-sentinel/redis/master/config/redis-6.0.20/redis.conf /redis-master-slave-sentinel/redis/master/config/redis.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /redis-master-slave-sentinel/redis/master/config/redis.conf
# slave-1
mkdir -p /redis-master-slave-sentinel/redis/slave-1/config /redis-master-slave-sentinel/redis/slave-1/data
cd /redis-master-slave-sentinel/redis/slave-1/config/
cp /redis-master-slave-sentinel/redis/master/config/redis-6.0.20/redis.conf /redis-master-slave-sentinel/redis/slave-1/config/redis.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /redis-master-slave-sentinel/redis/slave-1/config/redis.conf
# slave-2
mkdir -p /redis-master-slave-sentinel/redis/slave-2/config /redis-master-slave-sentinel/redis/slave-2/data
cd /redis-master-slave-sentinel/redis/slave-2/config/
cp /redis-master-slave-sentinel/redis/master/config/redis-6.0.20/redis.conf /redis-master-slave-sentinel/redis/slave-2/config/redis.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /redis-master-slave-sentinel/redis/slave-2/config/redis.conf
#redis-sentinel-1
mkdir -p /redis-master-slave-sentinel/sentinel/redis-sentinel-1 /redis-master-slave-sentinel/sentinel/redis-sentinel-1/s1
cd /redis-master-slave-sentinel/sentinel/redis-sentinel-1
cat >> /redis-master-slave-sentinel/sentinel/redis-sentinel-1/sentinel.conf <<EOF
# 配置可参考 http://www.zzvips.com/uploads/allimg/qqcrbinzrq5.conf
# 配置说明 https://redis.io/topics/sentinel
port 26379
dir /tmp
sentinel monitor mymaster 192.168.150.102 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
EOF
#redis-sentinel-2
mkdir -p /redis-master-slave-sentinel/sentinel/redis-sentinel-2 /redis-master-slave-sentinel/sentinel/redis-sentinel-2/s2
cd /redis-master-slave-sentinel/sentinel/redis-sentinel-2
cat >> /redis-master-slave-sentinel/sentinel/redis-sentinel-2/sentinel.conf <<EOF
# 配置可参考 http://www.zzvips.com/uploads/allimg/qqcrbinzrq5.conf
# 配置说明 https://redis.io/topics/sentinel
port 26379
dir /tmp
sentinel monitor mymaster 192.168.150.102 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
EOF
#redis-sentinel-3
mkdir -p /redis-master-slave-sentinel/sentinel/redis-sentinel-3 /redis-master-slave-sentinel/sentinel/redis-sentinel-3/s3
cd /redis-master-slave-sentinel/sentinel/redis-sentinel-3
cat >> /redis-master-slave-sentinel/sentinel/redis-sentinel-3/sentinel.conf <<EOF
# 配置可参考 http://www.zzvips.com/uploads/allimg/qqcrbinzrq5.conf
# 配置说明 https://redis.io/topics/sentinel
port 26379
dir /tmp
sentinel monitor mymaster 192.168.150.102 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
EOF
sentinel.conf说明
port 26379
dir /tmp
sentinel monitor mymaster 192.168.150.102 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel.conf解读
- port 26379:是当前sentinel实例的端口
- sentinel monitor mymaster 192.168.150.102 6380 2:指定主节点信息 ○ mymaster:主节点名称,自定义,任意写 ○ 192.168.150.102 6380:主节点的ip和端口 ○ 2:选举master时的quorum值
- sentinel auth-pass mymaster 123456 :访问主节点时的密码
如果您觉得本文不错,欢迎关注,点赞,收藏支持,您的关注是我坚持的动力!
3. docker-compose-redis-master-slave-sentinel.yml文件的编写
创建名称为docker-compose-redis-master-slave-sentinel.yml用于安装Redis哨兵集群docker-compose文件
version: '3'
# 网桥redis -> 方便相互通讯
networks:
redis:
services:
# ============================ ↓↓↓↓↓↓ redis ↓↓↓↓↓↓ ============================
# 主
redis-master:
image: redis:6.0.20 # 镜像'redis:6.0.20'
container_name: redis-master # 容器名为'redis-master'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
command: redis-server /etc/redis/redis.conf --port 6380 --requirepass 123456 --masterauth 123456 --appendonly no # 启动redis服务并添加密码为:123456, 当主redis下线重启后变成从redis时认证主redis密码:123456, 默认不开启redis-aof方式持久化配置
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "/redis-master-slave-sentinel/redis/master/data:/data"
- "/redis-master-slave-sentinel/redis/master/config/redis.conf:/etc/redis/redis.conf" # `redis.conf`文件内容`http://download.redis.io/redis-stable/redis.conf`
ports: # 映射端口
- "6380:6380"
# 从1
redis-slave-1:
image: redis:6.0.20 # 镜像'redis:6.0.20'
container_name: redis-slave-1 # 容器名为'redis-slave-1'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
command: redis-server /etc/redis/redis.conf --port 6381 --requirepass 123456 --appendonly no --slaveof 192.168.150.102 6380 --masterauth 123456 # 启动redis服务并添加密码为:123456,默认不开启redis-aof方式持久化配置,连接并认证master节点
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "/redis-master-slave-sentinel/redis/slave-1/data:/data"
- "/redis-master-slave-sentinel/redis/slave-1/config/redis.conf:/etc/redis/redis.conf" # `redis.conf`文件内容`http://download.redis.io/redis-stable/redis.conf`
ports: # 映射端口
- "6381:6381"
depends_on:
- redis-master
links:
- redis-master
# 从2
redis-slave-2:
image: redis:6.0.20 # 镜像'redis:6.0.20'
container_name: redis-slave-2 # 容器名为'redis-slave-2'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
command: redis-server /etc/redis/redis.conf --port 6382 --requirepass 123456 --appendonly no --slaveof 192.168.150.102 6380 --masterauth 123456 # 启动redis服务并添加密码为:123456,默认不开启redis-aof方式持久化配置,连接并认证master节点
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "/redis-master-slave-sentinel/redis/slave-2/data:/data"
- "/redis-master-slave-sentinel/redis/slave-2/config/redis.conf:/etc/redis/redis.conf" # `redis.conf`文件内容`http://download.redis.io/redis-stable/redis.conf`
ports: # 映射端口
- "6382:6382"
depends_on:
- redis-master
links:
- redis-master
# ============================ ↓↓↓↓↓↓ sentinel ↓↓↓↓↓↓ ============================
redis-sentinel-1:
image: redis:6.0.20 # 镜像'redis:6.0.20'
container_name: redis-sentinel-1 # 容器名为'redis-sentinel-1'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
command: redis-sentinel /etc/redis/sentinel.conf
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
ports:
- "26379:26379"
volumes:
- "/redis-master-slave-sentinel/sentinel/redis-sentinel-1/sentinel.conf:/etc/redis/sentinel.conf" # `sentinel.conf`文件内容`http://www.zzvips.com/uploads/allimg/qqcrbinzrq5.conf`
depends_on:
- redis-master
- redis-slave-1
- redis-slave-2
links:
- redis-master
- redis-slave-1
- redis-slave-2
redis-sentinel-2:
image: redis:6.0.20 # 镜像'redis:6.0.20'
container_name: redis-sentinel-2 # 容器名为'redis-sentinel-2'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
command: redis-sentinel /etc/redis/sentinel.conf
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
ports:
- "26380:26379"
volumes:
- "/redis-master-slave-sentinel/sentinel/redis-sentinel-2/sentinel.conf:/etc/redis/sentinel.conf" # `sentinel.conf`文件内容`http://www.zzvips.com/uploads/allimg/qqcrbinzrq5.conf`
depends_on:
- redis-master
- redis-slave-1
- redis-slave-2
- redis-sentinel-1
links:
- redis-master
- redis-slave-1
- redis-slave-2
redis-sentinel-3:
image: redis:6.0.20 # 镜像'redis:6.0.20'
container_name: redis-sentinel-3 # 容器名为'redis-sentinel-3'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
command: redis-sentinel /etc/redis/sentinel.conf
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
ports:
- "26381:26379"
volumes:
- "/redis-master-slave-sentinel/sentinel/redis-sentinel-3/sentinel.conf:/etc/redis/sentinel.conf" # `sentinel.conf`文件内容`http://www.zzvips.com/uploads/allimg/qqcrbinzrq5.conf`
depends_on:
- redis-master
- redis-slave-1
- redis-slave-2
- redis-sentinel-1
- redis-sentinel-2
links:
- redis-master
- redis-slave-1
- redis-slave-2
3.1. 运行 -- 哨兵模式(sentinel监视redis主从服务
运行 -- 哨兵模式(sentinel监视redis主从服务,当某个master服务下线时,自动将该master下的某个从服务升级为master服务替代已下线的master服务继续处理请求 -- 即主节点切换)
docker-compose -f docker-compose-redis-master-slave-sentinel.yml -p redis up -d
图片
3.2 测试
- 1.哨兵模式查看
# 连接redis-sentinel-1
docker exec -it redis-sentinel-1 redis-cli -p 26379
- 2.查看redis主信息
sentinel master mymaster
图片
- 查看redis从信息
sentinel slaves mymaster
图片
- 4.查看 哨兵redis-sentinel-1日志
docker logs -f redis-sentinel-1
图片
- 进入redis-slave-1容器
docker exec -it redis-master redis-cli -h 192.168.150.102 -p 6381 -a 123456
图片
- 关闭redis-master主节点
docker stop redis-master
发现主节点从master节点,转换为slave-2节点
图片
- 进入redis-slave-2容器
docker exec -it redis-master redis-cli -h 192.168.150.102 -p 6382 -a 123456
- 7.查看集群状态
info replication
发现现在从节点只剩一个
图片
- 8.重启master节点
docker start redis-master
图片
- 9.再次查看集群状态
info replication
发现master节点添加回来了,但是变成了从节点
图片
3.3. 卸载哨兵集群
docker-compose -f docker-compose-redis-master-slave-sentinel.yml -p redis down
3.4. 集群故障恢复原理
哨兵master自己从当前集群中的slave节点中选出一个新的master,原则是
- 1-首先会判断slave节点与master节点断开时间长短,如果超过指定值(down-after-milliseconds * 10)则会排除该slave节点
- 2-然后判断slave节点的slave-priority值,越小优先级越高,如果是0则永不参与选举
- 3-如果slave-prority一样,则判断slave节点的offset值,越大说明数据越新,优先级越高
- 4-最后是判断slave节点的运行id大小,越小优先级越高。
3.5. 哨兵集群监控原理
Sentinel基于心跳机制监测服务状态,每隔1秒向集群的每个实例发送ping命令:
- 主观下线:如果某sentinel节点发现某实例未在规定时间响应,则认为该实例主观下线。
- 客观下线:若超过指定数量(quorum)的sentinel都认为该实例主观下线,则该实例客观下线。quorum值最好超过Sentinel实例数量的一半。
图片
总结
Sentinel的三个作用是什么
- 监控
- 故障转移
- 通知
Sentinel如何判断一个redis实例是否健康
- 每隔1秒发送一次ping命令,如果超过一定时间没有相向则认为是主观下线
- 如果大多数sentinel都认为实例主观下线,则判定服务下线