哨兵基础认识
关于 Redis 哨兵的介绍:
-
一个哨兵就是一个 redis-sentinel 服务,默认使用 26379 端口。和 Redis 服务是分开的,可以理解为一个哨兵就是一台额外的服务器,用来监控 Redis 服务能不能正常运行。
-
多个哨兵就是多个 redis-sentinel 服务,每个可以部署在不同的机器上,甚至可以全部部署在同一台机器上,监听不同端口就行了,但这样就失去了多个哨兵的意义。
-
所有的哨兵都是监听同一个 Redis 服务的,就是 Master 服务,也就是说哨兵的数量和 Redis 服务的数量没有任何关系。
-
哨兵只需要监听 Redis Master 服务,因为 Slave 信息会注册在 Master 上,所有哨兵通过 Master 就可以知道所有的 Slave 服务信息了。
-
每个哨兵只需要知道当前的 Redis Master 服务是谁就行了,不需要知道其他哨兵的信息,因为他们会在 Master 上通过相互沟通得知当前其他哨兵的情况。
目录结构
sentinel/
├── docker-compose.yml
├── master
│ ├── data/
│ └── redis.conf
├── slave1
│ ├── data/
│ └── redis.conf
├── slave2
| ├── data/
| └── redis.conf
├── sentinel1
│ └── sentinel.conf
├── sentinel2
│ └── sentinel.conf
└── sentinel3
└── sentinel.conf
docker-compose.yml
version: "3"
networks:
redis-replication:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24
services:
master:
image: redis
container_name: redis-master
ports:
- "6380:6379"
volumes:
- "./master/redis.conf:/etc/redis.conf"
- "./master/data:/data"
command: ["redis-server", "/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.101
slave1:
image: redis
container_name: redis-slave-1
ports:
- "6381:6379"
volumes:
- "./slave1/redis.conf:/etc/redis.conf"
- "./slave1/data:/data"
command: ["redis-server", "/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.102
slave2:
image: redis
container_name: redis-slave-2
ports:
- "6382:6379"
volumes:
- "./slave2/redis.conf:/etc/redis.conf"
- "./slave2/data:/data"
command: ["redis-server", "/etc/redis.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.103
sentinel1:
image: redis
container_name: redis-sentinel-1
ports:
- "26380:26379"
volumes:
- "./sentinel1/sentinel.conf:/etc/sentinel.conf"
command: ["/bin/bash", "-c", "cp /etc/sentinel.conf /sentinel.conf && redis-sentinel /sentinel.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.201
sentinel2:
image: redis
container_name: redis-sentinel-2
ports:
- "26381:26379"
volumes:
- "./sentinel2/sentinel.conf:/etc/sentinel.conf"
command: ["/bin/bash", "-c", "cp /etc/sentinel.conf /sentinel.conf && redis-sentinel /sentinel.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.202
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- "26382:26379"
volumes:
- "./sentinel3/sentinel.conf:/etc/sentinel.conf"
command: ["/bin/bash", "-c", "cp /etc/sentinel.conf /sentinel.conf && redis-sentinel /sentinel.conf"]
restart: always
networks:
redis-replication:
ipv4_address: 172.25.0.203
master
port 6379
pidfile /var/run/redis_6379.pid
protected-mode no
timeout 0
tcp-keepalive 300
loglevel notice
################################# REPLICATION #################################
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
##################################### RDB #####################################
dbfilename dump.rdb
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dir ./
##################################### AOF #####################################
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
aof-load-truncated yes
aof-use-rdb-preamble no
slave1 & slave2
port 6379
pidfile /var/run/redis_6379.pid
protected-mode no
timeout 0
tcp-keepalive 300
loglevel notice
################################# REPLICATION #################################
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
#slaveof 172.25.0.101 6379
replicaof 172.25.0.101 6379
##################################### RDB #####################################
dbfilename dump.rdb
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dir ./
##################################### AOF #####################################
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
aof-load-truncated yes
aof-use-rdb-preamble no
sentinel1 & sentinel2 & sentinel3
# 所有哨兵端口都一致,因为使用 Docker 桥接网络映射
port 26379
# 哨兵设置,所有哨兵皆一致,都指向 Master
sentinel monitor mymaster 172.25.0.101 6379 2
sentinel parallel-syncs mymaster 1
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 180000
bind 0.0.0.0
protected-mode no
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile ""
dir /tmp
启动服务
docker-compose up -d
停止容器
docker stop redis-master redis-slave-1 redis-slave-2 redis-sentinel-1 redis-sentinel-2 redis-sentinel-3
查看容器配置是否正常
docker logs --tail 100 redis-master