企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] > [home](https://www.runoob.com/docker/docker-redis-cluster.html) ## 下载 redis.conf 下载地址:[https://github.com/antirez/redis/blob/unstable/redis.conf](https://github.com/antirez/redis/blob/unstable/redis.conf) ## 修改 redis.conf 开启集群功能: `cluster-enabled yes ` 设置节点端口: `port 6391 ` 节点超时时间,单位毫秒: `cluster-node-timeout 15000 ` 集群内部配置文件: `cluster-config-file "nodes-6379.conf"` ## 编写 docker-compose.yml ![UTOOLS1573718587878.png](http://yanxuan.nosdn.127.net/d0cb7e8f45ac6fe2f10b8379ce85a9c5.png) <details> <summary>docker-compose.yml</summary> ``` version: "3.6" services: redis-master1: image: redis:5.0 # 基础镜像 container_name: redis-master1 # 容器服务名 working_dir: /config # 工作目录 environment: # 环境变量 - PORT=6391 # 跟 config/nodes-6391.conf 里的配置一样的端口 ports: # 映射端口,对外提供服务 - "6391:6391" # redis 的服务端口 - "16391:16391" # redis 集群监控端口 stdin_open: true # 标准输入打开 networks: # docker 网络设置 redis-master: ipv4_address: 172.50.0.2 tty: true privileged: true # 拥有容器内命令执行的权限 volumes: ["/c/project/docker/redis/config:/config"] # 映射数据卷,配置目录 entrypoint: # 设置服务默认的启动程序 - /bin/bash - redis.sh redis-master2: image: redis:5.0 working_dir: /config container_name: redis-master2 environment: - PORT=6392 networks: redis-master: ipv4_address: 172.50.0.3 ports: - "6392:6392" - "16392:16392" stdin_open: true tty: true privileged: true volumes: ["/c/project/docker/redis/config:/config"] entrypoint: - /bin/bash - redis.sh redis-master3: image: redis:5.0 container_name: redis-master3 working_dir: /config environment: - PORT=6393 networks: redis-master: ipv4_address: 172.50.0.4 ports: - "6393:6393" - "16393:16393" stdin_open: true tty: true privileged: true volumes: ["/c/project/docker/redis/config:/config"] entrypoint: - /bin/bash - redis.sh redis-slave1: image: redis:5.0 container_name: redis-slave1 working_dir: /config environment: - PORT=6394 networks: redis-slave: ipv4_address: 172.30.0.2 ports: - "6394:6394" - "16394:16394" stdin_open: true tty: true privileged: true volumes: ["/c/project/docker/redis/config:/config"] entrypoint: - /bin/bash - redis.sh redis-salve2: image: redis:5.0 working_dir: /config container_name: redis-salve2 environment: - PORT=6395 ports: - "6395:6395" - "16395:16395" stdin_open: true networks: redis-slave: ipv4_address: 172.30.0.3 tty: true privileged: true volumes: ["/c/project/docker/redis/config:/config"] entrypoint: - /bin/bash - redis.sh redis-salve3: image: redis:5.0 container_name: redis-slave3 working_dir: /config environment: - PORT=6396 ports: - "6396:6396" - "16396:16396" stdin_open: true networks: redis-slave: ipv4_address: 172.30.0.4 tty: true privileged: true volumes: ["/c/project/docker/redis/config:/config"] entrypoint: - /bin/bash - redis.sh networks: redis-master: driver: bridge # 创建一个docker 的桥接网络 ipam: driver: default config: - subnet: 172.50.0.0/16 redis-slave: driver: bridge ipam: driver: default config: - subnet: 172.30.0.0/16 ``` </details> <br/> ## 编写 redis 默认的启动脚本 创建文件 `config/redis.sh` ``` redis-server /config/nodes-${PORT}.conf ``` ## 启动集群 `$ docker-compose up -d ` ## 初始化集群 (>redis5.0 ) 创建 3 主 3 从的 redis 集群: ``` $ redis-cli --cluster create 192.168.99.100:6391 \ 192.168.99.100:6392 192.168.99.100:6393 \ 192.168.99.100:6394 192.168.99.100:6395 192.168.99.100:6396 \ --cluster-replicas 1 ``` ## 连接 redis ``` redis-cli -h 192.168.99.100 -p 6391 ```