redis 哨兵模式(一)
本文介绍哨兵模式
与简单主从不同 哨兵 有监控功能 监控 各个主从节点 的健康状态 故障转移 自动选主 是由主从演变过来
下面 是 是docker-compose 构建 哨兵
创建文件夹 mkdir -p /home/admin1/yyx/redissentinels/
下面是redis主从 的
redis-master.conf
#bind 127.0.0.1 -::1 # 监听端口 port 6379 # 启动时不打印logo # 这个不重要,想看logo就打开它 always-show-logo no # 设定密码认证 requirepass 123456 protected-mode no appendonly yes # 禁用KEYS命令 # 一方面 KEYS * 命令可以列出所有的键,会影响数据安全 # 另一方面 KEYS 命令会阻塞数据库,在数据库中存储了大量数据时,该命令会消耗很长时间 # 期间对Redis的访问也会被阻塞,而当锁释放的一瞬间,大量请求涌入Redis,会造成Redis直接崩溃 rename-command KEYS "" masterauth "123456"
redis-slave1.conf
# bind 127.0.0.1 # 监听端口 port 6380 always-show-logo no requirepass 123456 protected-mode no appendonly yes rename-command KEYS "" replicaof 192.168.0.168 6379 # 设定连接主节点所使用的密码 masterauth "123456"
redis-slave2.conf
# bind 127.0.0.1 # 监听端口 port 6381 always-show-logo no # 设定密码认证 requirepass 123456 protected-mode no appendonly yes replicaof 192.168.0.168 6379 rename-command KEYS "" # 设定连接主节点所使用的密码 masterauth "123456"
docker-compose.yml
version: '3'
services:
# 主节点的容器
redis-server-master:
image: redis
container_name: redis-server-master
restart: always
# 为了规避Docker中端口映射可能带来的问题
# 这里选择使用host网络
network_mode: bridge
ports:
- 6379:6379
# 指定时区,保证容器内时间正确
environment:
TZ: "Asia/Shanghai"
volumes:
# 映射配置文件和数据目录
- ./redis-master.conf:/usr/local/etc/redis/redis.conf
- ./data/redis-master:/data
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# 从节点1的容器
redis-server-slave-1:
image: redis
container_name: redis-server-slave-1
restart: always
network_mode: host
depends_on:
- redis-server-master
network_mode: bridge
ports:
- 6380:6380
environment:
TZ: "Asia/Shanghai"
volumes:
- ./redis-slave1.conf:/usr/local/etc/redis/redis.conf
- ./data/redis-slave-1:/data
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# 从节点2的容器
redis-server-slave-2:
image: redis
container_name: redis-server-slave-2
restart: always
network_mode: host
depends_on:
- redis-server-master
network_mode: bridge
ports:
- 6381:6381
environment:
TZ: "Asia/Shanghai"
volumes:
- ./redis-slave2.conf:/usr/local/etc/redis/redis.conf
- ./data/redis-slave-2:/data
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
下面是 哨兵
redis-sentinel-1.conf
# bind 127.0.0.1 port 26379 protected-mode no sentinel monitor local-master 192.168.0.168 6379 2 sentinel auth-pass local-master 123456 # master在连续多长时间无法响应PING指令后,就会主观判定节点下线,默认是30秒 # 格式:sentinel down-after-milliseconds <master-name> <milliseconds> sentinel down-after-milliseconds local-master 30000
redis-sentinel-2.conf
# bind 127.0.0.1 port 26380 sentinel monitor local-master 192.168.0.168 6379 2 sentinel auth-pass local-master 123456 protected-mode no # master在连续多长时间无法响应PING指令后,就会主观判定节点下线,默认是30秒 # 格式:sentinel down-after-milliseconds <master-name> <milliseconds> sentinel down-after-milliseconds local-master 30000
redis-sentinel-3.conf
# bind 127.0.0.1 port 26381 sentinel monitor local-master 192.168.0.168 6379 2 sentinel auth-pass local-master 123456 protected-mode no # master在连续多长时间无法响应PING指令后,就会主观判定节点下线,默认是30秒 # 格式:sentinel down-after-milliseconds <master-name> <milliseconds> sentinel down-after-milliseconds local-master 30000
docker-compose.yml
version: '3'
services:
redis-sentinel-1:
image: redis
container_name: redis-sentinel-1
restart: always
# 为了规避Docker中端口映射可能带来的问题
# 这里选择使用host网络
network_mode: bridge
ports:
- 26379:26379
volumes:
- ./redis-sentinel-1.conf:/usr/local/etc/redis/redis-sentinel.conf
# 指定时区,保证容器内时间正确
environment:
TZ: "Asia/Shanghai"
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]
redis-sentinel-2:
image: redis
container_name: redis-sentinel-2
restart: always
network_mode: bridge
ports:
- 26380:26380
volumes:
- ./redis-sentinel-2.conf:/usr/local/etc/redis/redis-sentinel.conf
environment:
TZ: "Asia/Shanghai"
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]
redis-sentinel-3:
image: redis
container_name: redis-sentinel-3
restart: always
network_mode: bridge
ports:
- 26381:26381
volumes:
- ./redis-sentinel-3.conf:/usr/local/etc/redis/redis-sentinel.conf
environment:
TZ: "Asia/Shanghai"
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]
之后docker-compose up -d 启动 docker-compose down 删除
浙公网安备 33010602011771号