docker-compose 两台虚拟服务器:部署三主三从Redis集群

因为环境限制,所以考虑用两台虚拟机搭建简单的redis分布式集群

一、服务器规划

服务器IP实例端口
10.211.55.7redis_6391(master),redis_6392(slave)6391,6392
10.211.55.8

redis_6391(master),redis_6392(slave),redis_6393(master),

redis_6394(slave)

6391,6392,6393,6394

二、准备redis集群的docker-compose文件

2.1、10.211.55.7服务器docker-compose文件

docker-compose-rediscluster01.yml

version: "3.7"
services:
  redis-6391:
    image: redis:7.0.10 # 基础镜像
    container_name: redis-6391 # 容器名称
    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: [   
        "/home/workspaces/redis-cluster/redis-6391/data:/data",
        "/home/workspaces/redis-cluster/redis-6391/conf/redis.conf:/etc/redis/redis.conf",
        "/home/workspaces/redis-cluster/etc/rc.local:/etc/rc.local",
        "/etc/localtime:/etc/localtime" #将Redis服务的时间设置为与宿主机相同(/etc/localtime:/etc/localtime:ro)如果加:ro,表示只读
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  redis-m6392:
    image: redis:7.0.10 # 基础镜像
    container_name: redis-6392 # 容器名称
    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: [
        "/home/workspaces/redis-cluster/redis-6392/data:/data",
        "/home/workspaces/redis-cluster/redis-6392/conf/redis.conf:/etc/redis/redis.conf",
        "/home/workspaces/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'

2.2、10.211.55.8服务器docker-compose文件

docker-compose-rediscluster02.yml

version: "3.7"
services:
  redis-6391:
    image: redis:7.0.10 # 基础镜像
    container_name: redis-6391 # 容器名称
    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: [   
        "/home/workspaces/redis-cluster/redis-6391/data:/data",
        "/home/workspaces/redis-cluster/redis-6391/conf/redis.conf:/etc/redis/redis.conf",
        "/home/workspaces/redis-cluster/etc/rc.local:/etc/rc.local",
        "/etc/localtime:/etc/localtime" #将Redis服务的时间设置为与宿主机相同(/etc/localtime:/etc/localtime:ro)如果加:ro,表示只读
    ]
    command: [
        "redis-server",
        "/etc/redis/redis.conf"
    ]
    logging:
      options:
        max-size: '100m'
        max-file: '10'
  redis-6392:
    image: redis:7.0.10 # 基础镜像
    container_name: redis-6392 # 容器名称
    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: [
        "/home/workspaces/redis-cluster/redis-6392/data:/data",
        "/home/workspaces/redis-cluster/redis-6392/conf/redis.conf:/etc/redis/redis.conf",
        "/home/workspaces/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-6393:
    image: redis:7.0.10 # 基础镜像
    container_name: redis-6393 # 容器名称
    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: [
        "/home/workspaces/redis-cluster/redis-6393/data:/data",
        "/home/workspaces/redis-cluster/redis-6393/conf/redis.conf:/etc/redis/redis.conf",
        "/home/workspaces/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-6394:
    image: redis:7.0.10 # 基础镜像
    container_name: redis-6394 # 容器名称
    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: [
        "/home/workspaces/redis-cluster/redis-6394/data:/data",
        "/home/workspaces/redis-cluster/redis-6394/conf/redis.conf:/etc/redis/redis.conf",
        "/home/workspaces/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-cluster.tmpl文件

进入/home/workspaces/redis-cluster目录,新增文件redis-cluster.tmpl

两个服务器相同操作。(根据服务器配置cluster-announce-ip)

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 yourpassword
#redis 访问Master节点密码
masterauth yourpassword
# 关闭保护模式
protected-mode no
# 开启集群
cluster-enabled yes
# 集群节点配置
cluster-config-file nodes_${PORT}.conf
# 超时
cluster-node-timeout 5000
# 集群节点IP host模式为宿主机IP(根据服务器配置)
cluster-announce-ip 10.211.55.7
# 集群节点端口 6391 - 6392
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

复制redis-cluster-config.sh至/home/workspaces/redis-cluster目录下,执行 ./redis-cluster-config.sh。(10.211.55.7只生成6391、6392文件夹;10.211.55.8生成6391、6392、6394、6395文件夹;)

五、编排docker-compose

10.211.55.7 服务器编排 docker-compose

  1. 创建redis 集群:

#docker编排Redis集群
docker-compose -f ./docker-compose-rediscluster01.yml up -d
#集群初始化
# 进入Redis容器
docker exec -it redis-6391 /bin/bash
# 初始化Redis集群命令,如果有密码,需要带上 --a <your_password>
redis-cli -a <password> --cluster create 10.211.55.7:6391 10.211.55.7:6391 10.211.55.8:6391 10.211.55.8:6392 10.211.55.8:6393 10.211.55.8:6394 --cluster-replicas 1
#--cluster-replicas 1 参数指定了每个主节点的副本数量。
#查看集群通信是否正常
docker docker exec -it redis-6391 redis-cli -h 10.211.55.7 -p 6391 -a yourpassword ping
#查看集群状态
cluster nodes
#查看slots分片
cluster slots
#查看集群信息
cluster info

2.为每个主节点配置从新的节点:

redis-cli --cluster add-node <新节点IP>:<新节点端口>  <已存在节点IP>:<端口>

redis-cli -a <password> --cluster add-node 10.211.55.7:6393 10.211.55.7:6391 --cluster-slave --cluster-master-id <你的主节点id>
 
redis-cli -a <password> --cluster add-node 10.211.55.8:6395 10.211.55.8:6391 --cluster-slave --cluster-master-id <你的主节点id>
 
redis-cli -a <password> --cluster add-node 10.211.55.8:6396 10.211.55.8:6393 --cluster-slave --cluster-master-id <你的主节点id>
  • slave:该节点为备份节点
  • master:该节点为主节点
  • myself:该节点为当前连接的节点
  • --cluster-replicas 0 : 表示都为主节点

六、查看集群状态

redis-cli -a <password> -c -h 10.211.55.7 -p 6391

然后,执行指令cluster info查看集群状况,显示cluster_state:ok则表示集群已经正常创建

七.在项目里配置

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值