docker-compose 部署redis集群
时间: 2023-12-06 17:38:48 浏览: 194
以下是使用docker-compose部署Redis集群的步骤:
1.创建一个目录,例如redis-cluster,并在该目录中创建一个docker-compose.yaml文件。
2.在docker-compose.yaml文件中编写以下内容:
```yaml
version: '3'
services:
redis-1:
image: redis:6.0.9
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./redis.conf:/usr/local/etc/redis/redis.conf
ports:
- "6379"
networks:
- redis-cluster
redis-2:
image: redis:6.0.9
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./redis.conf:/usr/local/etc/redis/redis.conf
ports:
- "6380"
networks:
- redis-cluster
redis-3:
image: redis:6.0.9
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./redis.conf:/usr/local/etc/redis/redis.conf
ports:
- "6381"
networks:
- redis-cluster
networks:
redis-cluster:
driver: bridge
```
3.在redis-cluster目录中创建一个redis.conf文件,并将以下内容复制到文件中:
```conf
bind 0.0.0.0
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass 1111
masterauth 1111
```
4.在终端中导航到redis-cluster目录,并运行以下命令启动Redis集群:
```shell
docker-compose up -d
```
5.使用以下命令进入redis-1容器:
```shell
docker exec -it redis-cluster_redis-1_1 /bin/bash
```
6.在redis-1容器中,使用以下命令创建Redis集群:
```shell
redis-cli --cluster create 172.20.0.2:6379 172.20.0.3:6379 172.20.0.4:6379 --cluster-replicas 0
```
7.现在,您已经成功地使用docker-compose部署了Redis集群。
阅读全文