我在本地的目录/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