当前位置:   首页安装配置docker搭建redis主从哨兵集群的实现步骤

docker搭建redis主从哨兵集群的实现步骤

发布日期:2022-07-19 10:21 | 文章来源:CSDN

本文以docker-compose 搭建高可用Redis 主从、哨兵集群为例子

关于redis主从,哨兵集群原理参见:Redis 单机安装/ 哨兵模式集群安装

1:获取redis镜像

docker pull redis:6.2.7

2:创建redis主从+哨兵docker-compose文件

cd /opt/docker/redis
vi docker-compose.yml

docker-compose.yml的内容如下

version: '3'
services:
  master:
    image: redis:6.2.7       ## 镜像
    container_name: redis-master
    command: redis-server /etc/redis/redis.conf --requirepass 123456 --masterauth 123456
    volumes:
    - /opt/docker/redis/data/redis_data1:/data
    - /opt/docker/redis/conf/redis1.conf:/etc/redis/redis.conf
    network_mode: "host"
  slave1:
    image: redis:6.2.7                ## 镜像
    container_name: redis-slave-1
    volumes:
    - /opt/docker/redis/data/redis_data2:/data
    - /opt/docker/redis/conf/redis2.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 
    depends_on:
    - master
    network_mode: "host"
  slave2:
    image: redis:6.2.7                ## 镜像
    container_name: redis-slave-2
    volumes:
    - /opt/docker/redis/data/redis_data3:/data
    - /opt/docker/redis/conf/redis3.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
    depends_on:
    - master
    network_mode: "host"
  sentinel1:
    image: redis:6.2.7       ## 镜像
    container_name: redis-sentinel-1
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - /opt/docker/redis/conf/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
    network_mode: "host"
    depends_on:
    - master
    - slave1
    - slave2
  sentinel2:
    image: redis:6.2.7                ## 镜像
    container_name: redis-sentinel-2          
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - /opt/docker/redis/conf/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
    network_mode: "host"
    depends_on:
    - master
    - slave1
    - slave2
  sentinel3:
    image: redis:6.2.7                ## 镜像
    container_name: redis-sentinel-3          
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - /opt/docker/redis/conf/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
    network_mode: "host"
    depends_on:
    - master
    - slave1
    - slave2

3:redis配置和哨兵配置说明

查看配置文件的目录树

cd /opt/docker/redis
tree ./

结构如下

redis1.conf,redis2.conf,redis3.conf配置如下

#redis1.conf
bind 0.0.0.0
port 6379
protected-mode no
slave-read-only no
 
#redis2.conf
bind 0.0.0.0
port 6380
protected-mode no
slave-read-only no
 
#redis3.conf
bind 0.0.0.0
port 6381
protected-mode no
slave-read-only no

sentinel1.conf,sentinel1.conf,sentinel1.conf配置:

#sentinel1.conf
port 26379
dir /tmp
sentinel monitor mymaster 192.168.18.131 6379 2
sentinel auth-pass mymaster 123456 
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000  
sentinel deny-scripts-reconfig yes
 
#sentinel2.conf
port 26380
dir /tmp
sentinel monitor mymaster 192.168.18.131 6379 2
sentinel auth-pass mymaster 123456 
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000  
sentinel deny-scripts-reconfig yes
 
#sentinel3.conf
port 26381
dir /tmp
sentinel monitor mymaster 192.168.18.131 6379 2
sentinel auth-pass mymaster 123456 
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000  
sentinel deny-scripts-reconfig yes

说明:

【sentinel monitor mymaster 192.168.18.131 6379 2】192.168.18.131为主机的IP地址,6379为redis master的端口号
【sentinel auth-pass mymaster 123456】设置主节点的密码
【sentinel down-after-milliseconds mymaster 30000】表示在一段时间范围内sentinel向master发送的心跳PING没有回复则认为master不可用了。
【sentinel parallel-syncs mymaster 1】的parallel-syncs表示设置在故障转移之后,同时可以重新配置使用新master的slave的数量。数字越低,更多的时间将会用故障转移完成,但是如果slaves配置为服务旧数据,你可能不希望所有的slave同时重新同步master。因为主从复制对于slave是非阻塞的,当停止从master加载批量数据时有一个片刻延迟。通过设置选项为1,确信每次只有一个slave是不可到达的。
【sentinel failover-timeout mymaster 10000 】表示10秒内mymaster还没活过来,则认为master宕机了。

redis_data1,redis_data2,redis_data3为空文件夹,用于存放redis数据文件

4:启动docker-compose

docker-compose up
#或者,后台启动
docker-compose up -d

5:查看启动情况

6:进入主节点查看集群情况

docker exec -it 主节点容器id或者容器名称 bash
redis-cli -p 6379
info replication

到此这篇关于docker搭建redis主从哨兵集群的实现步骤的文章就介绍到这了,更多相关docker redis主从哨兵集群内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

联系我们
关于使用场景和技术架构的更多咨询,请联系我们的销售和技术支持团队。
Yingsoo Host

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部