dockercompose安装redis
时间: 2023-12-02 08:43:03 浏览: 228
Docker安装redis
5星 · 资源好评率100%
以下是使用Docker Compose部署Redis集群的步骤:
1. 在服务器上安装Docker和Docker Compose。
2. 创建一个目录来存储Redis配置文件和数据文件。
```shell
mkdir -p /usr/local/redis/data /usr/local/redis/logs /usr/local/redis/conf
chmod -R 777 /usr/local/redis/data*
chmod -R 777 /usr/local/redis/logs*
```
3. 创建一个Redis配置文件。
```shell
cd /usr/local/redis/conf
vim redis.conf
```
在配置文件中添加以下内容:
```
bind 0.0.0.0
protected-mode no
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
```
4. 创建一个Docker Compose文件。
```shell
cd /usr/local/redis
vim docker-compose.yml
```
在文件中添加以下内容:
```
version: '3'
services:
redis1:
image: redis:6.2.5
container_name: redis1
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis1:/data
- ./logs/redis1:/logs
ports:
- "6379:6379"
networks:
redis-cluster:
ipv4_address: 172.16.238.10
redis2:
image: redis:6.2.5
container_name: redis2
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis2:/data
- ./logs/redis2:/logs
ports:
- "6380:6379"
networks:
redis-cluster:
ipv4_address: 172.16.238.11
redis3:
image: redis:6.2.5
container_name: redis3
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis3:/data
- ./logs/redis3:/logs
ports:
- "6381:6379"
networks:
redis-cluster:
ipv4_address: 172.16.238.12
redis4:
image: redis:6.2.5
container_name: redis4
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis4:/data
- ./logs/redis4:/logs
ports:
- "6382:6379"
networks:
redis-cluster:
ipv4_address: 172.16.238.13
redis5:
image: redis:6.2.5
container_name: redis5
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis5:/data
- ./logs/redis5:/logs
ports:
- "6383:6379"
networks:
redis-cluster:
ipv4_address: 172.16.238.14
redis6:
image: redis:6.2.5
container_name: redis6
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis6:/data
- ./logs/redis6:/logs
ports:
- "6384:6379"
networks:
redis-cluster:
ipv4_address: 172.16.238.15
networks:
redis-cluster:
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
```
5. 启动Redis集群。
```shell
docker-compose up -d
```
这将启动6个Redis容器,其中3个是主节点,3个是从节点,并且外部可以通过6379端口访问Redis集群。
阅读全文