docker-compose部署redis集群(3主3从)

  • docker-compose(ports和host二选一):
version: "3"
services:
  redis-m6391:
    image: redis:5.0.5 # 基础镜像
    container_name: redis-m6391 # 容器名称
    restart: always
    environment: # 环境变量
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    ports: # 映射端口,对外提供服务
      - 6391:6391 # redis的服务端口
      - 16391:16391 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    # 映射数据卷,配置目录
    volumes: [
        "/project/docker/redis-cluster/redis-m6391/data:/data",
        "/project/docker/redis-cluster/redis-m6391/conf/redis.conf:/etc/redis/redis.conf",
        "/project/docker/redis-cluster/etc/rc.local:/etc/rc.local",
        "/etc/localtime:/etc/localtime"
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  redis-m6392:
    image: redis:5.0.5 # 基础镜像
    container_name: redis-m6392 # 容器名称
    restart: always
    environment: # 环境变量
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    ports: # 映射端口,对外提供服务
      - 6392:6392 # redis的服务端口
      - 16392:16392 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    # 映射数据卷,配置目录
    volumes: [
        "/project/docker/redis-cluster/redis-m6392/data:/data",
        "/project/docker/redis-cluster/redis-m6392/conf/redis.conf:/etc/redis/redis.conf",
        "/project/docker/redis-cluster/etc/rc.local:/etc/rc.local",
        "/etc/localtime:/etc/localtime"
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  redis-m6393:
    image: redis:5.0.5 # 基础镜像
    container_name: redis-m6393 # 容器名称
    restart: always
    environment: # 环境变量
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    ports: # 映射端口,对外提供服务
      - 6393:6393 # redis的服务端口
      - 16393:16393 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    # 映射数据卷,配置目录
    volumes: [
        "/project/docker/redis-cluster/redis-m6393/data:/data",
        "/project/docker/redis-cluster/redis-m6393/conf/redis.conf:/etc/redis/redis.conf",
        "/project/docker/redis-cluster/etc/rc.local:/etc/rc.local",
        "/etc/localtime:/etc/localtime"
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  redis-s6394:
    image: redis:5.0.5 # 基础镜像
    container_name: redis-s6394 # 容器名称
    restart: always
    environment: # 环境变量
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    ports: # 映射端口,对外提供服务
      - 6394:6394 # redis的服务端口
      - 16394:16394 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    # 映射数据卷,配置目录
    volumes: [
        "/project/docker/redis-cluster/redis-s6394/data:/data",
        "/project/docker/redis-cluster/redis-s6394/conf/redis.conf:/etc/redis/redis.conf",
        "/project/docker/redis-cluster/etc/rc.local:/etc/rc.local",
        "/etc/localtime:/etc/localtime"
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  redis-s6395:
    image: redis:5.0.5 # 基础镜像
    container_name: redis-s6395 # 容器名称
    restart: always
    environment: # 环境变量
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    ports: # 映射端口,对外提供服务
      - 6395:6395 # redis的服务端口
      - 16395:16395 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    # 映射数据卷,配置目录
    volumes: [
        "/project/docker/redis-cluster/redis-s6395/data:/data",
        "/project/docker/redis-cluster/redis-s6395/conf/redis.conf:/etc/redis/redis.conf",
        "/project/docker/redis-cluster/etc/rc.local:/etc/rc.local",
        "/etc/localtime:/etc/localtime"
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  redis-s6396:
    image: redis:5.0.5 # 基础镜像
    container_name: redis-s6396 # 容器名称
    restart: always
    environment: # 环境变量
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    ports: # 映射端口,对外提供服务
      - 6396:6396 # redis的服务端口
      - 16396:16396 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    # 映射数据卷,配置目录
    volumes: [
        "/project/docker/redis-cluster/redis-s6396/data:/data",
        "/project/docker/redis-cluster/redis-s6396/conf/redis.conf:/etc/redis/redis.conf",
        "/project/docker/redis-cluster/etc/rc.local:/etc/rc.local",
        "/etc/localtime:/etc/localtime"
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  1. 脚本文件:
  • redis-cluster.tmpl:
bind 0.0.0.0
# redis端口
port ${PORT}
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
pidfile /var/run/redis_${PORT}.pid
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
dir /data/
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 60000
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
#redis 访问密码
requirepass 123456
#redis 访问Master节点密码
masterauth 123456
# 关闭保护模式
protected-mode no
# 开启集群
cluster-enabled yes
# 集群节点配置
cluster-config-file nodes_${PORT}.conf
# 超时
cluster-node-timeout 5000
# 集群节点IP host模式为宿主机IP
cluster-announce-ip 127.0.0.1
# 集群节点端口 6391 - 6396
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
# 开启 appendonly 备份模式
appendonly yes
appendfilename "appendonly.aof"
# 每秒钟备份
appendfsync everysec
# 对aof文件进行压缩时,是否执行同步操作
no-appendfsync-on-rewrite no
# 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写
auto-aof-rewrite-percentage 100
# 重写前AOF文件的大小最小值 默认 64mb
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# 日志配置
# debug:会打印生成大量信息,适用于开发/测试阶段
# verbose:包含很多不太有用的信息,但是不像debug级别那么混乱
# notice:适度冗长,适用于生产环境
# warning:仅记录非常重要、关键的警告消息
loglevel notice
# 日志文件路径
logfile "/data/redis.log"
  • redis-cluster-config.sh
for port in `seq 6391 6396`; do \
mkdir -p ./redis-${port}/conf && PORT=${port} envsubst < ./redis-cluster.tmpl > ./redis-${port}/conf/redis.conf && mkdir -p ./redis-${port}/data; \
done
  1. 复制redis-cluster.tmpl和redis-cluster-config.sh至/project/docker/redis-cluster,执行 ./redis-cluster-config.sh,


    目录如下

    修改文件夹名称为:


    修改为

    3.执行命令:
#docker编排Redis集群
docker-compose -f ./docker-compose.yml up -d
#集群初始化
# 进入Redis容器
docker exec -it redis-m6391 /bin/bash
# 初始化Redis集群命令
redis-cli --cluster create 127.0.0.1:6391 127.0.0.1:6392 127.0.0.1:6393 127.0.0.1:6394 127.0.0.1:6395 127.0.0.1:6396 --cluster-replicas 1 -a 123456
#查看集群通信是否正常
docker docker exec -it redis-m6391 redis-cli -h 127.0.0.1 -p 6391 -a 123456 ping
#查看集群状态
cluster nodes
#查看slots分片
cluster slots
#查看集群信息
cluster info
  • slave:该节点为备份节点
  • master:该节点为主节点
  • myself:该节点为当前连接的节点

4.在项目里配置:

spring:
  redis:
    password:123456 # Redis服务器连接密码(默认为空)
    timeout: 3000ms # 连接超时时间
    lettuce:
      pool:
        max-active: 8 # 连接池最大连接数
        max-idle: 8 # 连接池最大空闲连接数
        min-idle: 0 # 连接池最小空闲连接数
        max-wait: -1ms # 连接池最大阻塞等待时间,负值表示没有限制
    cluster:
      nodes:
        - 127.0.0.1:6391
        - 127.0.0.1:6392
        - 127.0.0.1:6393
        - 127.0.0.1:6394
        - 127.0.0.1:6395
        - 127.0.0.1:6396

注:若有三台服务器,自定义主从关系:

version: "3"
services:
  redis-m6391:
    image: redis:5.0.5 # 基础镜像
    container_name: redis-m6391 # 容器名称
    restart: always
    environment: # 环境变量
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    ports: # 映射端口,对外提供服务
      - 6391:6391 # redis的服务端口
      - 16391:16391 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    # 映射数据卷,配置目录
    volumes: [
        "/project/docker/redis-cluster/redis-m6391/data:/data",
        "/project/docker/redis-cluster/redis-m6391/conf/redis.conf:/etc/redis/redis.conf",
        "/etc/localtime:/etc/localtime"
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  redis-s6392:
    image: redis:5.0.5 # 基础镜像
    container_name: redis-s6392 # 容器名称
    restart: always
    environment: # 环境变量
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
    ports: # 映射端口,对外提供服务
      - 6392:6392 # redis的服务端口
      - 16392:16392 # redis集群监控端口
    stdin_open: true # 标准输入打开
    tty: true # 后台运行不退出
    network_mode: host # 使用host模式
    privileged: true # 拥有容器内命令执行的权限
    # 映射数据卷,配置目录
    volumes: [
        "/project/docker/redis-cluster/redis-s6392/data:/data",
        "/project/docker/redis-cluster/redis-s6392/conf/redis.conf:/etc/redis/redis.conf",
        "/etc/localtime:/etc/localtime"
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  1. 每个宿主机上,都创建对应的 redis-m6391和redis--s6392目录,用于存放,redis的配置文件(config用上面的就可)。然后将 redis docker 挂载到该目录下,并使用自定义的配置文件,启动。


    宿主机目录/project/docker/redis-cluster/
  1. 将docker-compose.yml放入宿主机/project/docker/redis-cluster/下,然后分别在其他两台服务器复制同样目录、文件(cluster-announce-ip记得改)

  2. 每个服务器都执行编排文件,镜像都已成功,共6个服务,但未创建集群

  3. 在任意一台服务器上,随便进入一个redis容器里:
    docker exec -it redis-m6391 bash

  4. 先创建3主的redis 集群:

redis-cli -a 123456 --cluster create 192.168.1.1:6391 192.168.1.2:6391 192.168.1.3:6391 --cluster-replicas 0
  • --cluster-replicas 0 : 表示都为主节点
  1. 为每个主节点配置从节点:
redis-cli -a 123456 --cluster add-node 192.168.1.4:6392 192.168.1.1:6391 --cluster-slave --cluster-master-id 你的主节点d
 
redis-cli -a 123456 --cluster add-node 192.168.1.5:6392 192.168.1.2:6391 --cluster-slave --cluster-master-id 你的主节点d
 
redis-cli -a 123456 --cluster add-node 192.168.1.6:6392 192.168.1.3:6391 --cluster-slave --cluster-master-id 你的主节点id
  • 注: 前一个ip 为从redis的公网Ip,后一个为 主redis 的公网Ip。 cluster-master-id为 创建 3主集群时,分别出现的 id(若出现连接超时的问题请检查config是否配的公网ip)
  1. 查看集群状态

你可能感兴趣的:(docker-compose部署redis集群(3主3从))