Redis(八):哨兵(Sentinel)部署

本文基于 Docker Engine 和 Docker Compose 环境,单机部署 Redis Sentinel

1、部署环境

Docker version 18.09.6, build 481bc77156
Linux 5.1.3-1.el7.elrepo.x86_64
docker-compose version 1.24.0, build 0aa59064

2、部署架构

本文部署架构为一主多从三哨兵,因服务器条件有限,该架构在一台服务器中进行部署,仅供参考
image

3、部署步骤

3.1、安装Docker

Docker 安装参见:https://docs.docker.com/install/linux/docker-ce/centos/

3.2、安装Docker Compose

1、执行以下命令安装当前稳定版本的Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

2、赋予可执行权限

sudo chmod +x /usr/local/bin/docker-compose

如遇其他问题可前往查看官方文档:https://docs.docker.com/compose/install/

3.3、创建Docker Compose Redis 模板

docker-redis-compose.txt

version: '2'
services:
  master:
    image: redis       
    container_name: redis-master
    command: redis-server --requirepass 123456
    ports:
    - "6379:6379"
    networks:
    - sentinel-master
  slave1:
    image: redis               
    container_name: redis-slave-1
    ports:
    - "6380:6379"           
    command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 
    depends_on:
    - master
    networks:
    - sentinel-master
  slave2:
    image: redis
    container_name: redis-slave-2
    ports:
    - "6381:6379"
    command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
    depends_on:
    - master
    networks:
    - sentinel-master
networks:
  sentinel-master:

3.4、创建Docker Compose Redis Sentinel 模板

docker-redis-sentinel-compose.txt

version: '2'
services:
  sentinel_01:
    image: redis  
    container_name: redis-sentinel-01
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - "/data/redis/conf:/usr/local/etc/redis"
  sentinel_02:
    image: redis
    container_name: redis-sentinel-02
    ports:
    - "26380:26379"           
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - "/data/redis/conf:/usr/local/etc/redis"
  sentinel_03:
    image: redis
    container_name: redis-sentinel-03
    ports:
    - "26381:26379"           
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - /data/redis/conf:/usr/local/etc/redis
networks:
  default:
    external:
      name: redis_sentinel_master

哨兵的配置文件如下:

port 26379
dir "/tmp"
sentinel monitor redis-master 【服务器IP】 6379 2
sentinel failover-timeout redis-master 10000
sentinel auth-pass redis-master 123456
port:sentinel节点的端口
dir:监控日志
sentinel monitor redis-master:当前监控主节点
    2:代表判断主节点失败至少需要2个Sentinel节点同意
    redis-master:是主节点的别名
sentinel failover-timeout:故障转移超时时间为10000毫秒

4、启动集群

4.1、启动Redis主从复制集群

docker-compose -f docker-redis-compose.txt up -d

4.2、启动Redis Sentinel 集群

docker-compose -f docker-redis-sentinel-compose.txt up -d

5、查看集群运行情况

[root@xry compose]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
30da5d04a142        redis               "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        6379/tcp                             redis-sentinel-01
5d8971c6585e        redis               "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        6379/tcp, 0.0.0.0:26381->26379/tcp   redis-sentinel-03
475063baae05        redis               "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        6379/tcp, 0.0.0.0:26380->26379/tcp   redis-sentinel-02
48f7e2d54787        redis               "docker-entrypoint.s…"   17 seconds ago      Up 16 seconds       0.0.0.0:6380->6379/tcp               redis-slave-1
2c570706b6d8        redis               "docker-entrypoint.s…"   17 seconds ago      Up 16 seconds       0.0.0.0:6381->6379/tcp               redis-slave-2
96732d1ae66c        redis               "docker-entrypoint.s…"   17 seconds ago      Up 16 seconds       0.0.0.0:6379->6379/tcp               redis-master
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值