docker-compose搭建redis集群--Cluster模式

Redis有三种集群模式

* 主从模式

* Sentinel模式

* Cluster模式

参考:Redis集群详解

1.首先需要docker 、docker-compose 环境

2.下载相关配置文件

Gitee地址: https://gitee.com/lifeToSpring/redis-cluster

目录结构:

 redis.sh

redis-server  /config/nodes-${PORT}.conf

3.修改对应nodes-${PORT}.conf的配置文件(参考):

# bind 127.0.0.1 //加上注释#
protected-mode no //关闭保护模式
port 6391  //绑定自定义端口
# daemonize yes //禁止redis后台运行
pidfile /var/run/redis_6391.pid
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_6391.conf //集群的配置 配置文件首次启动自动生成
appendonly yes //开启aof
cluster-announce-ip 10.xx.xx.xx   //要宣布的IP地址。nat模式要指定宿主机IP
cluster-announce-port 6391  //要宣布的数据端口。
cluster-announce-bus-port 16391  //要宣布的集群总线端口

4.1 节点192.168.255.225:

docker-compose.yml

services:
  redis-master1:
    image: redis:5.0 # 基础镜像
    container_name: node1 # 容器名称
    working_dir: /config # 切换工作目录
    environment: # 环境变量
      - PORT=6391 # 会使用config/nodes-${PORT}.conf这个配置文件
    ports: # 映射端口,对外提供服务
      - 6391:6391 # redis的服务端口
      - 16391:16391 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    volumes:
      - /mydata/redis-cluster/config:/config #配置文件目录映射到宿主机
    entrypoint: # 设置服务默认的启动程序
      - /bin/bash
      - redis.sh
  redis-master2:
    image: redis:5.0
    working_dir: /config
    container_name: node2
    environment:
      - PORT=6392
    ports:
      - 6392:6392
      - 16392:16392
    stdin_open: true
    network_mode: host
    tty: true
    privileged: true
    volumes:
      - /mydata/redis-cluster/config:/config
    entrypoint:
      - /bin/bash
      - redis.sh
  redis-master3:
    image: redis:5.0
    container_name: node3
    working_dir: /config
    environment:
      - PORT=6393
    ports:
      - 6393:6393
      - 16393:16393
    stdin_open: true
    network_mode: host
    tty: true
    privileged: true
    volumes:
      - /mydata/redis-cluster/config:/config
    entrypoint:
      - /bin/bash
      - redis.sh

 启动redis实例

docker-compose -f docker-compose.yml up

4.2 节点192.168.255.226:

docker-compose.yml

version: "3"
services:
  redis-slave1:
    image: redis:5.0
    container_name: node4
    working_dir: /config
    environment:
      - PORT=6394
    ports:
      - 6394:6394
      - 16394:16394
    stdin_open: true
    network_mode: host
    tty: true
    privileged: true
    volumes:
      - /mydata/redis-cluster/config:/config
    entrypoint:
      - /bin/bash
      - redis.sh
  redis-slave2:
    image: redis:5.0
    working_dir: /config
    container_name: node5
    environment:
      - PORT=6395
    ports:
      - 6395:6395
      - 16395:16395
    stdin_open: true
    network_mode: host
    tty: true
    privileged: true
    volumes:
      - /mydata/redis-cluster/config:/config
    entrypoint:
      - /bin/bash
      - redis.sh
  redis-slave3:
    image: redis:5.0
    container_name: node6
    working_dir: /config
    environment:
      - PORT=6396
    ports:
      - 6396:6396
      - 16396:16396
    stdin_open: true
    network_mode: host
    tty: true
    privileged: true
    volumes:
      - /mydata/redis-cluster/config:/config
    entrypoint:
      - /bin/bash
      - redis.sh

启动redis实例

docker-compose -f docker-compose.yml up

5 使用docker镜像 zvelo/redis-trib 构建redis集群

docker run --rm -it zvelo/redis-trib create --replicas 1 192.168.255.225:6391 192.168.255.225:6392 192.168.255.225:6393 192.168.255.226:6394 192.168.255.226:6395 192.168.255.226:6396

 

原文地址:https://www.cnblogs.com/brithToSpring/p/13187206.html