Redis的哨兵模式和集群模式是用于提高Redis系统可用性和分布式能力的两种模式。以下是如何使用Docker Compose来搭建Redis哨兵模式和集群模式的示例。
哨兵模式(Sentinel Mode)
Redis哨兵模式通过哨兵节点监控主从节点,自动进行故障转移,保证高可用性。
Docker Compose 文件
version: '2.1'
services:
redis-master:
image: redis:latest
container_name: redis-master
ports:
- "6379:6379"
volumes:
- ./data/master:/data
command: ["redis-server", "--appendonly", "yes"]
redis-slave1:
image: redis:latest
container_name: redis-slave1
depends_on:
- redis-master
volumes:
- ./data/slave1:/data
command: ["redis-server", "--appendonly", "yes", "--slaveof", "redis-master", "6379"]
redis-slave2:
image: redis:latest
container_name: redis-slave2
depends_on:
- redis-master
volumes:
- ./data/slave2:/data
command: ["redis-server", "--appendonly", "yes", "--slaveof", "redis-master", "6379"]
sentinel1:
image: redis:latest
container_name: sentinel1
depends_on:
- redis-master
- redis-slave1
- redis-slave2
ports:
- "26379:26379"
command: >
sh -c 'redis-sentinel /usr/local/etc/sentinel.conf'
volumes:
- ./conf/sentinel1.conf:/usr/local/etc/sentinel.conf
sentinel2:
image: redis:latest
container_name: sentinel2
depends_on:
- redis-master
- redis-slave1
- redis-slave2
ports:
- "26380:26379"
command: >
sh -c 'redis-sentinel /usr/local/etc/sentinel.conf'
volumes:
- ./conf/sentinel2.conf:/usr/local/etc/sentinel.conf
sentinel3:
image: redis:latest
container_name: sentinel3
depends_on:
- redis-master
- redis-slave1
- redis-slave2
ports:
- "26381:26379"
command: >
sh -c 'redis-sentinel /usr/local/etc/sentinel.conf'
volumes:
- ./conf/sentinel3.conf:/usr/local/etc/sentinel.conf
哨兵配置文件(sentinel.conf)
哨兵配置文件内容可以如下:
在不同服务器上应当修改redis-master 6379为主节点ip与端口
port 26379
sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
需要创建三个类似的配置文件sentinel1.conf
, sentinel2.conf
, sentinel3.conf
,内容相同。
配置好后,直接在docker-compose.yml所在目录下使用docker compose up -d 启动即可
集群模式(Cluster Mode)
Redis集群模式通过分片(sharding)实现数据的分布式存储和高可用性。
Docker Compose 文件
version: '2.1'
services:
redis-node1:
image: redis:latest
container_name: redis-node1
ports:
- "7001:6379"
volumes:
- ./data/node1:/data
command: ["redis-server", "--cluster-enabled", "yes", "--appendonly", "yes", "--cluster-config-file", "/data/nodes.conf", "--port", "6379"]
redis-node2:
image: redis:latest
container_name: redis-node2
ports:
- "7002:6379"
volumes:
- ./data/node2:/data
command: ["redis-server", "--cluster-enabled", "yes", "--appendonly", "yes", "--cluster-config-file", "/data/nodes.conf", "--port", "6379"]
redis-node3:
image: redis:latest
container_name: redis-node3
ports:
- "7003:6379"
volumes:
- ./data/node3:/data
command: ["redis-server", "--cluster-enabled", "yes", "--appendonly", "yes", "--cluster-config-file", "/data/nodes.conf", "--port", "6379"]
redis-node4:
image: redis:latest
container_name: redis-node4
ports:
- "7004:6379"
volumes:
- ./data/node4:/data
command: ["redis-server", "--cluster-enabled", "yes", "--appendonly", "yes", "--cluster-config-file", "/data/nodes.conf", "--port", "6379"]
redis-node5:
image: redis:latest
container_name: redis-node5
ports:
- "7005:6379"
volumes:
- ./data/node5:/data
command: ["redis-server", "--cluster-enabled", "yes", "--appendonly", "yes", "--cluster-config-file", "/data/nodes.conf", "--port", "6379"]
redis-node6:
image: redis:latest
container_name: redis-node6
ports:
- "7006:6379"
volumes:
- ./data/node6:/data
command: ["redis-server", "--cluster-enabled", "yes", "--appendonly", "yes", "--cluster-config-file", "/data/nodes.conf", "--port", "6379"]
初始化集群
使用docker compose up -d 启动容器后,需要运行以下命令初始化集群:
注意:这里必须用主机ip,尝试使用了容器id是不行的,后续连接端口需要使用7001 - 7006
docker exec -it redis-node1 redis-cli --cluster create \
192.168.16.4:6379 192.168.16.6:6379 192.168.16.7:6379 \
192.168.16.3:6379 192.168.16.5:6379 192.168.16.2:6379 \
--cluster-replicas 1
总结
上述Docker Compose文件分别展示了如何搭建Redis哨兵模式和集群模式的环境。通过这种方式,可以快速创建高可用、可扩展的Redis服务。