centos7 docker 安装redis sentinel 集群

我在本地的目录/root/redis/sentinel

一   创建 主从的docker-compose.yml ,我的端口号为 6385  6386  6387 ,密码是123456

version: '3.4'
services:
  master:
    image: redis
    container_name: redis-6385
    restart: always
    command: redis-server --port 6379 --requirepass 123456 --masterauth 123456
    ports:
      - 6385:6379
  slave1:
    image: redis
    container_name: redis-6386
    restart: always
    command: redis-server --slaveof redis-6385 6379 --port 6379 --requirepass 123456 --masterauth 123456
    ports:
      - 6386:6379
  slave2:
    image: redis
    container_name: redis-6387
    restart: always
    command: redis-server --slaveof redis-6385 6379 --port 6379 --requirepass 123456 --masterauth 123456
    ports:
      - 6387:6379

 

二  然后启动 docker-compose up -d   ,如图 如果报错  bash: docker-compose: 未找到命令... ,去安装 docker-compose命令

[root@localhost-1 sentinel]# docker-compose up -d
bash: docker-compose: 未找到命令...
[root@localhost-1 sentinel]# docker-compose up -d
Creating network "sentinel_default" with the default driver
Creating redis-6386 ... done
Creating redis-6385 ... done
Creating redis-6387 ... done
[root@localhost-1 sentinel]#

 安装 docker-compose命令

[root@localhost-1 ~]# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   633  100   633    0     0    539      0  0:00:01  0:00:01 --:--:--   539
100 15.4M  100 15.4M    0     0   191k      0  0:01:22  0:01:22 --:--:--  172k
[root@localhost-1 ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost-1 ~]# docker-compose --version
docker-compose version 1.24.1, build 4667896b
[root@localhost-1 ~]#

记住这个 生成的   sentinel_default   后期要使用

三 准备 sentinel 配置文件

创建 sentinel 的docker-compose.yml

#docker-compose.yml
version: '3.4'
services:
  sentinel1:
    image: redis
    container_name: redis-sentinel-26385
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    restart: always
#   network_mode: "host"
    ports:
      - 26385:26385
    volumes:
# 本地文件映射到容器里
      - ./sentinel_26385.conf:/usr/local/etc/redis/sentinel.conf
  sentinel2:
    image: redis
    container_name: redis-sentinel-26386
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    restart: always
    ports:
      - 26386:26386
#   network_mode: "host"
    volumes:
      - ./sentinel_26386.conf:/usr/local/etc/redis/sentinel.conf
  sentinel3:
    image: redis
    container_name: redis-sentinel-26387
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    restart: always
#   network_mode: "host"
    ports:
      - 26387:26387
    volumes:
      - ./sentinel_26387.conf:/usr/local/etc/redis/sentinel.conf
networks:
  default:
    external:
# 前面redis启动时默认生成的网桥
      name: sentinel_default

查询主的内网ip ,即为 172.19.0.3

[root@localhost-1 ~]# docker inspect redis-6385   |grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.19.0.3",
[root@localhost-1 ~]# 

sentinel-26385.conf配置文件 内容

#sentinel_26385.conf
port 26385
dir /tmp
# 自定义集群名,其中 172.23.0.4 为 本机内外 的 ip,6379 为 redis-master容器在本机映射 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
sentinel monitor mymaster 172.19.0.3 6379 2
# 主节点下线10秒开始选新节点
sentinel down-after-milliseconds mymaster 10000
# 指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步,这个数字越小,完成failover所需的时间就越长,但是如果这个数字越大,就意味着越 多的slave因为replication而不可用。可以通过将这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

sentinel-26386.conf配置文件 内容

#sentinel_26386.conf
port 26386
dir /tmp
sentinel monitor mymaster 172.19.0.3 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

 

sentinel-26387.conf配置文件 内容

#sentinel_26387.conf
port 26387
dir /tmp
sentinel monitor mymaster 172.19.0.3 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

启动 docker-compose up -d ,如下图

[root@localhost-1 sentinel]# docker-compose up -d
WARNING: Found orphan containers (redis-6385, redis-6386, redis-6387) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating redis-sentinel-26386 ... done
Creating redis-sentinel-26385 ... done
Creating redis-sentinel-26387 ... done
[root@localhost-1 sentinel]# 

 

 验证 

 

[root@localhost-1 ~]# docker exec -it redis-sentinel-26385 /bin/bash
root@6bcdfb1fdfb0:/data# redis-cli -p 26385
127.0.0.1:26385> sentinel master mymaster
127.0.0.1:26385> sentinel slaves mymaster 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄泉路好走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值