文章目录
- Docker配置 redis
- 一主二从三哨兵
- 文件目录
- redis compose 文件
- 在 redis 文件路径下执行 docker compose
- sentinel 文件结构
- sentinel compose 配置文件
- sentinel config 配置文件
- 切换到 sentinel执行 compose
- 测试及问题记录
- 解决方案
Docker配置 redis
一主二从三哨兵
文件目录
redis compose 文件
docker-compose.yml
version: '3'
services:
master:
image: redis
container_name: redis-master
command: redis-server --requirepass redis_pwd --masterauth redis_pwd --replica-announce-ip 192.168.84.143 --replica-announce-port 6380
ports:
- 6380:6379
slave1:
image: redis
container_name: redis-slave-1
ports:
- 6381:6379
command: redis-server --slaveof redis-master 6379 --masterauth redis_pwd --requirepass redis_pwd --replica-announce-ip 192.168.84.143 --replica-announce-port 6381
slave2:
image: redis
container_name: redis-slave-2
ports:
- 6382:6379
command: redis-server --slaveof redis-master 6379 --masterauth redis_pwd --requirepass redis_pwd --replica-announce-ip 192.168.84.143 --replica-announce-port 6382
在 redis 文件路径下执行 docker compose
(base) wenxiaoyu@wenshuaishuaideMacBook-Pro redis % pwd
/Users/wenxiaoyu/env/docker-test/redis
(base) wenxiaoyu@wenshuaishuaideMacBook-Pro redis % docker compose up -d
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
(base) wenxiaoyu@wenshuaishuaideMacBook-Pro redis % docker compose up -d
[+] Running 9/9
⠿ master Pulled 10.4s
⠿ slave2 Pulled 10.4s
⠿ 6064e7e5b6af Pull complete 4.1s
⠿ f6bd55a0e6ff Pull complete 4.2s
⠿ 7525aa3c12b6 Pull complete 4.3s
⠿ 9aeda440bf83 Pull complete 4.6s
⠿ 5e63a885a59a Pull complete 4.6s
⠿ 595b087d4909 Pull complete 4.7s
⠿ slave1 Pulled 10.4s
[+] Running 4/4
⠿ Network redis_default Created 0.1s
⠿ Container redis-master Started 0.7s
⠿ Container redis-slave-1 Started 0.7s
⠿ Container redis-slave-2 Started 0.7s
sentinel 文件结构
sentinel compose 配置文件
version: '3'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
default:
name: redis_default
external: true
sentinel config 配置文件
port 26379
dir /tmp
sentinel announce-ip 172.20.0.1
sentinel announce-port 26379
sentinel monitor mymaster 172.20.0.1 6380 2
sentinel auth-pass mymaster redis_pwd
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
切换到 sentinel执行 compose
(base) wenxiaoyu@wenshuaishuaideMacBook-Pro docker-test % cd sentinel
(base) wenxiaoyu@wenshuaishuaideMacBook-Pro sentinel % docker compose up -d
[+] Running 3/3
⠿ Container redis-sentinel-2 Started 0.7s
⠿ Container redis-sentinel-3 Started 0.6s
⠿ Container redis-sentinel-1 Started 0.6s
测试及问题记录
# redis-cli -p 26379
127.0.0.1:26379> info
# Server
redis_version:7.0.5
redis_git_sha1:00000000
redis_git_dirty:0
...
master0:name=mymaster,status=sdown,address=172.20.0.1:6380,slaves=0,sentinels=1
问题,slaves=0,sentinels=1
status=sdown
解决方案
(base) wenxiaoyu@wenshuaishuaideMacBook-Pro sentinel % docker inspect e1cc
注意此处的 elcc 是我的 redis=master 容器 id, 你需要修改为你的 id
然后寻找Gateway
将 sentinel 的三个 config 文件中的 ip 修改为查到的 ip
问题解决