Redis哨兵高可用架构
sentinel哨兵是特殊的redis服务,不提供读写服务,主要用来监控redis实例节点。 哨兵架构下client端第一次从哨兵找出redis的主节点,后续就直接访问redis的主节点,不会每次都通过 sentinel代理访问redis的主节点,当redis的主节点发生变化,哨兵会第一时间感知到,并且将新的redis 主节点通知给client端(这里面redis的client端一般都实现了订阅功能,订阅sentinel发布的节点变动消息)
文件组织格式
redis-sentinel-cluster
├── redis-cluster
│ ├──master
│ │ └── redis.conf
│ ├──slave1
│ │ └── redis.conf
│ ├──slave2
│ │ └── redis.conf
│ └── docker-compose.yml
└── sentinel
├── sentinel-1
│ └── sentinel.conf
├── sentinel-2
│ └── sentinel.conf
├── sentinel-3
│ └── sentinel.conf
└── docker-compose.yml
一、docker-compose搭建Redis主从架构
- 修改主节点redis.conf(redis-sentinel-cluster/redis-cluster/master/redis.conf)
bind 0.0.0.0
port 6380
logfile "6379.log"
daemonize no /*当redis.conf配置文件中daemonize参数设置的yes,这使得redis是以后台启动的方式运行的,由于docker容器在启动时,需要任务在前台运行,否则会启动后立即退出,因此导致redis容器启动后立即退出问题。所以redis.conf中daemonize必须是no*/
masterauth "123456"/*当master服务设置了密码保护时,slav服务连接master的密码*/
requirepass 123456/*设置redis连接密码*/
- 修改从节点1 redis.conf(redis-sentinel-cluster/redis-cluster/slave1/redis.conf)
bind 0.0.0.0
port 6380
logfile "6380.log"
daemonize no /*当redis.conf配置文件中daemonize参数设置的yes,这使得redis是以后台启动的方式运行的,由于docker容器在启动时,需要任务在前台运行,否则会启动后立即退出,因此导致redis容器启动后立即退出问题。所以redis.conf中daemonize必须是no*/
masterauth "123456"/*当master服务设置了密码保护时,slav服务连接master的密码*/
requirepass 123456 /*设置redis连接密码*/
replicaof 192.168.1.205 6379 /*从本机6379的redis实例复制数据,Redis 5.0之前使用slaveof*/
/*从节点上报给master的自己ip,防止nat问题*/
replica-announce-ip "192.168.1.205"
replica-announce-port 6380
- 修改从节点2 redis.conf(修改redis-sentinel-cluster/redis-cluster/slave2/redis.conf)
bind 0.0.0.0
port 6381
logfile "6381.log"
daemonize no /*当redis.conf配置文件中daemonize参数设置的yes,这使得redis是以后台启动的方式运行的,由于docker容器在启动时,需要任务在前台运行,否则会启动后立即退出,因此导致redis容器启动后立即退出问题。所以redis.conf中daemonize必须是no*/
masterauth "123456"/*当master服务设置了密码保护时,slav服务连接master的密码*/
requirepass 123456 /*设置redis连接密码*/
replicaof 192.168.1.205 6379 /*从本机6379的redis实例复制数据,Redis 5.0之前使用slaveof*/
/*从节点上报给master的自己ip,防止nat问题*/
replica-announce-ip "192.168.1.205"
replica-announce-port 6381
- 创建redis-sentinel-cluster/redis-cluster/docker-compose.yml
version: '3'
services:
# master1配置
master:
image: redis:6.0.6
container_name: redis-master
environment: # 环境变量
- TZ=Asia/Shanghai
ports:
- 6379:6379
- 16379:16379
privileged: true # 拥有容器内命令执行的权限
volumes: #文件挂载
- /home/redis-sentinel-cluster/redis-cluster/master/redis.conf:/usr/local/etc/redis/redis.conf
- /home/redis-sentinel-cluster/redis-data/master:/data
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
# slave1配置
slave1:
image: redis:6.0.6
container_name: redis-slave-1
environment: # 环境变量
- TZ=Asia/Shanghai
ports:
- 6380:6380
- 16380:16380
privileged: true # 拥有容器内命令执行的权限
volumes: #文件挂载
- /home/redis-sentinel-cluster/redis-cluster/slave1/redis.conf:/usr/local/etc/redis/redis.conf
- /home/redis-sentinel-cluster/redis-data/slave1:/data
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
# slave2配置
slave2:
image: redis:6.0.6
container_name: redis-slave-2
environment: # 环境变量
- TZ=Asia/Shanghai
ports:
- 6381:6381
- 16381:16381
privileged: true # 拥有容器内命令执行的权限
volumes: #文件挂载
- /home/redis-sentinel-cluster/redis-cluster/slave2/redis.conf:/usr/local/etc/redis/redis.conf
- /home/redis-sentinel-cluster/redis-data/slave2:/data
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
- 生成启动Redis主从容器
docker-compose up -d
- 查看redis是否启动成功
docker ps
- 进入容器查看redis是否实现一主二从功能
docker exec -it 容器Id bash
#进入容器后运行:
redis-cli
#如果设置了验证密码: (本人上面redis.conf中设置的验证密码是123456)
123456
#查询节点信息
info Replication
#到每个节点中查看节点信息,并且到主节点中进行数据存储,到从节点中查看,从节点是只读不能写
-
主节点信息
-
从节点1信息
-
从节点2信息
二、docker-compose搭建Redis哨兵(sentinel)架构
- 创建sentinel.conf文件(redis-sentinel-cluster/sentinel/sentinel-*/sentinel.conf)
port 26379 /*其他两个哨兵节点要不一样的端口*/
daemonize no/*保持no,千万不要yes*/
sentinel monitor mymaster 192.168.1.205 6379 2 /*mymaster这个名字随便取,客户端访问时会用 到,IP 和 端口是主从Redis的主节点IP和端口,2是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),master才算真正失效*/
sentinel auth-pass mymaster Welcome2021! /*验证密码*/
sentinel announce-ip "192.168.1.205"
sentinel announce-port 26379 /*该端口号要跟该文件上面的端口号prot同步*/
- 创建redis-sentinel-cluster/sentinel/docker-compose.yml
version: '3'
services:
# master-1配置
sentinel1:
image: redis:6.0.6
container_name: sentinel-1
ports:
- 26379:26379
privileged: true # 拥有容器内命令执行的权限
volumes:
- /home/redis-sentinel-cluster/sentinel/sentinel-1/sentinel.conf:/usr/local/etc/redis/sentinel.conf
command: sh -c "redis-sentinel /usr/local/etc/redis/sentinel.conf"
# slave1配置
sentinel2:
image: redis:6.0.6
container_name: sentinel-2
ports:
- 26380:26380
privileged: true # 拥有容器内命令执行的权限
volumes:
- /home/redis-sentinel-cluster/sentinel/sentinel-2/sentinel.conf:/usr/local/etc/redis/sentinel.conf
command: sh -c "redis-sentinel /usr/local/etc/redis/sentinel.conf"
# slave2配置
sentinel3:
image: redis:6.0.6
container_name: sentinel-3
ports:
- 26381:26381
privileged: true # 拥有容器内命令执行的权限
volumes:
- /home/redis-sentinel-cluster/sentinel/sentinel-3/sentinel.conf:/usr/local/etc/redis/sentinel.conf
command: sh -c "redis-sentinel /usr/local/etc/redis/sentinel.conf"
- 生成sentinel哨兵架构
docker-compose up -d
- 查看sentinel是否成功
docker ps