docker配置redis哨兵模式
时间: 2024-04-16 07:21:49 浏览: 162
redis(哨兵模式配置)
当使用Docker配置Redis哨兵模式时,可以按照以下步骤进行操作:
1. 首先,确保已经安装了Docker和Docker Compose。
2. 创建一个新的目录,用于存放Redis配置文件和Docker Compose文件。
3. 在该目录下创建一个名为`docker-compose.yml`的文件,并在其中编写以下内容:
```yaml
version: '3'
services:
redis-master:
image: redis
container_name: redis-master
ports:
- "6379:6379"
volumes:
- ./redis-master.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
redis-slave1:
image: redis
container_name: redis-slave1
ports:
- "6380:6379"
volumes:
- ./redis-slave1.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
redis-slave2:
image: redis
container_name: redis-slave2
ports:
- "6381:6379"
volumes:
- ./redis-slave2.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
redis-sentinel1:
image: redis
container_name: redis-sentinel1
ports:
- "26379:26379"
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
command: redis-sentinel /usr/local/etc/redis/sentinel.conf --sentinel
redis-sentinel2:
image: redis
container_name: redis-sentinel2
ports:
- "26380:26379"
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
command: redis-sentinel /usr/local/etc/redis/sentinel.conf --sentinel
redis-sentinel3:
image: redis
container_name: redis-sentinel3
ports:
- "26381:26379"
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
command: redis-sentinel /usr/local/etc/redis/sentinel.conf --sentinel
```
4. 在同一目录下创建以下配置文件:
- `redis-master.conf`:Redis主节点的配置文件,内容如下:
```
bind 0.0.0.0
port 6379
daemonize yes
```
- `redis-slave1.conf`:Redis从节点1的配置文件,内容如下:
```
bind 0.0.0.0
port 6379
daemonize yes
slaveof redis-master 6379
```
- `redis-slave2.conf`:Redis从节点2的配置文件,内容如下:
```
bind 0.0.0.0
port 6379
daemonize yes
slaveof redis-master 6379
```
- `sentinel1.conf`:Redis哨兵1的配置文件,内容如下:
```
bind 0.0.0.0
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
```
- `sentinel2.conf`:Redis哨兵2的配置文件,内容如下:
```
bind 0.0.0.0
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
```
- `sentinel3.conf`:Redis哨兵3的配置文件,内容如下:
```
bind 0.0.0.0
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
```
5. 打开终端,进入到该目录,并执行以下命令启动Redis容器:
```
docker-compose up -d
```
这将启动一个包含Redis主节点、两个从节点和三个哨兵节点的Docker容器。
现在,你已经成功配置了Redis哨兵模式。你可以通过访问`localhost:6379`来访问Redis主节点,`localhost:6380`和`localhost:6381`来访问两个从节点,以及`localhost:26379`、`localhost:26380`和`localhost:26381`来访问三个哨兵节点。
阅读全文