Docker 搭建Redis集群(哨兵模式)

一、简介

Redis集群的哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。 哨兵模式作用:

  • 通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。

  • 当哨兵监测到master宕机,会自动将slave切换成master,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。

除了监控Redis服务之外,哨兵之间也会互相监控。本文采用一主、双从、三哨兵方式

二、配置

部署方式为:docker compose:

第一步:创建redis docker-compose.yml配置文件

复制代码
version: '3.4'
services:
  master:
    image: redis
    container_name: redis-master
    restart: always
    command: redis-server --port 16380 --requirepass 123456   # 16380 是定义的主库端口,默认:6379;  --requirepass 123456 是redis密码。
    ports:
      - 16380:16380   # 将容器的16380端口映射到宿主机的16380端口上,第一个16380为宿主机端口。

  slave1:
    image: redis
    container_name: redis-slave-1
    restart: always
    command: redis-server --slaveof 127.0.0.1 16380 --port 16381 --requirepass 123456 --masterauth 123456   # 127.0.0.1 16380 为主库ip和端口(此处我们使用宿主机的ip和主库映射出来的端口);16381 是定义的从库端口,默认:6379;  --requirepass 123456 是redis密码; --masterauth 123456 是主库的密码。
    ports:
      - 16381:16381


  slave2:
    image: redis
    container_name: redis-slave-2
    restart: always
    command: redis-server --slaveof 127.0.0.1 16380 --port 16382 --requirepass 123456 --masterauth 123456   # 127.0.0.1 16380 为主库ip和端口(此处我们使用宿主机的ip和主库映射出来的端口);16382 是定义的从库端口,默认:6379;  --requirepass 123456 是redis密码; --masterauth 123456 是主库的密码。
    ports:
      - 16382:16382
复制代码

第二步:执行启动命令 在当前目录下执行启动命令

docker-compose -f docker-compose.yml up -d

第三步:创建sentinel docker-compose.yml  sentinel1.conf   sentinel2.conf  sentinel3.conf配置文件

docker-compose.yml

复制代码
version: '3.4'
services:
  sentinel1:
    image: redis
    container_name: redis-sentinel-1
    command: redis-sentinel /home/ec2-user/dockerfile/sentinel/sentinel.conf # 自定义路径,可更改,但是需要和volumes中的路径相同。
    restart: always
    ports:
      - 26380:26380
    volumes:
      - ./sentinel1.conf:/home/ec2-user/dockerfile/sentinel/sentinel.conf # 自定义路径,可更改,但是需要和command中的路径相同。

  sentinel2:
    image: redis
    container_name: redis-sentinel-2
    command: redis-sentinel /home/ec2-user/dockerfile/sentinel/sentine2.conf
    restart: always
    ports:
      - 26381:26381
    volumes:
      - ./sentinel2.conf:/home/ec2-user/dockerfile/sentinel/sentine2.conf

  sentinel3:
    image: redis
    container_name: redis-sentinel-3
    command: redis-sentinel /home/ec2-user/dockerfile/sentinel/sentine3.conf
    restart: always
    ports:
      - 26382:26382
    volumes:
      - ./sentinel3.conf:/home/ec2-user/dockerfile/sentinel/sentine3.conf
复制代码

sentinel1.conf

复制代码
port 26380
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 127.0.0.1 16380 2  # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
sentinel auth-pass mymaster 123456 # 主机密码
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
复制代码

sentinel2.conf

复制代码
port 26381
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 127.0.0.1 16380 2  # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
sentinel auth-pass mymaster 123456 # 主机密码
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
复制代码

sentinel3.conf

复制代码
port 26382
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 127.0.0.1 16380 2  # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
sentinel auth-pass mymaster 123456 # 主机密码
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
复制代码

第四步:执行启动命令 在当前目录下执行启动命令

docker-compose -f docker-compose.yml up -d

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/16933475.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(1107)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起