基于DockerCompose搭建Redis主从哨兵模式
linux目录结构
内网配置
哨兵配置文件如下,创建3个哨兵配置文件
# sentinel26379.conf sentinel26380.conf sentinel26381.conf 内容如下
protected-mode no
sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
docker-compose.yaml
version: '3'services:redis-master:image: redis:4.0.8container_name: redis-masterports:- "6379:6379"volumes:- redis-master-data:/datacommand: ["redis-server", "--appendonly", "yes"]redis-slave1:image: redis:4.0.8container_name: redis-slave1ports:- "6380:6379"volumes:- redis-slave1-data:/datacommand: ["redis-server", "--slaveof", "redis-master", "6379", "--appendonly", "yes"]depends_on:- redis-masterredis-slave2:image: redis:4.0.8container_name: redis-slave2ports:- "6381:6379"volumes:- redis-slave2-data:/datacommand: ["redis-server", "--slaveof", "redis-master", "6379", "--appendonly", "yes"]depends_on:- redis-masterredis-sentinel-1:image: redis:4.0.8container_name: redis-sentinel-1ports:- "26379:26379"volumes:- ./sentinel26379.conf:/usr/local/etc/redis/sentinel.confcommand: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]depends_on:- redis-master- redis-slave1- redis-slave2redis-sentinel-2:image: redis:4.0.8container_name: redis-sentinel-2ports:- "26380:26379"volumes:- ./sentinel26380.conf:/usr/local/etc/redis/sentinel.confcommand: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]depends_on:- redis-master- redis-slave1- redis-slave2redis-sentinel-3:image: redis:4.0.8container_name: redis-sentinel-3ports:- "26381:26379"volumes:- ./sentinel26381.conf:/usr/local/etc/redis/sentinel.confcommand: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]depends_on:- redis-master- redis-slave1- redis-slave2volumes:redis-master-data:redis-slave1-data:redis-slave2-data:
host版本
version: '3'services:redis-master:image: redis:4.0.8container_name: redis-masternetwork_mode: hostports:- "17000:17000"volumes:- redis-master-data:/datacommand: ["redis-server", "--appendonly", "yes","--port","17000"]redis-slave1:image: redis:4.0.8container_name: redis-slave1network_mode: hostports:- "17001:17001"volumes:- redis-slave1-data:/datacommand: ["redis-server", "--slaveof", "192.168.0.203", "17000", "--appendonly", "yes","--port","17001"]depends_on:- redis-masterredis-slave2:image: redis:4.0.8container_name: redis-slave2network_mode: hostports:- "17002:17002"volumes:- redis-slave2-data:/datacommand: ["redis-server", "--slaveof", "192.168.0.203", "17000", "--appendonly", "yes","--port","17002"]depends_on:- redis-masterredis-sentinel-1:image: redis:4.0.8container_name: redis-sentinel-1network_mode: hostports:- "16000:16000"volumes:- ./sentinel16000.conf:/usr/local/etc/redis/sentinel.confcommand: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]depends_on:- redis-master- redis-slave1- redis-slave2redis-sentinel-2:image: redis:4.0.8container_name: redis-sentinel-2network_mode: hostports:- "16001:16001"volumes:- ./sentinel16001.conf:/usr/local/etc/redis/sentinel.confcommand: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]depends_on:- redis-master- redis-slave1- redis-slave2redis-sentinel-3:image: redis:4.0.8container_name: redis-sentinel-3network_mode: hostports:- "16002:16002"volumes:- ./sentinel16002.conf:/usr/local/etc/redis/sentinel.confcommand: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]depends_on:- redis-master- redis-slave1- redis-slave2volumes:redis-master-data:redis-slave1-data:redis-slave2-data:
protected-mode no
sentinel myid d1ccb49b4c07533de9e7357402dcb79e2f4a4458
sentinel monitor mymaster 192.168.0.203 17000 2
sentinel down-after-milliseconds mymaster 60000
sentinel config-epoch mymaster 0
# Generated by CONFIG REWRITE
port 16000
dir "/data"
############################################################
protected-mode no
sentinel myid d1ccb49b4c07533de9e7357402dcb79e2f4a4458
sentinel monitor mymaster 192.168.0.203 17000 2
sentinel down-after-milliseconds mymaster 60000
sentinel config-epoch mymaster 0
# Generated by CONFIG REWRITE
port 16001
dir "/data"
############################################################
protected-mode no
sentinel myid d1ccb49b4c07533de9e7357402dcb79e2f4a4458
sentinel monitor mymaster 192.168.0.203 17000 2
sentinel down-after-milliseconds mymaster 60000
sentinel config-epoch mymaster 0
# Generated by CONFIG REWRITE
port 16002
dir "/data"