docker下创建redis cluster集群

概述

在Redis中,集群的解决方案有三种

  1. 主从复制
  2. 哨兵机制
  3. Cluster

Redis Cluster是Redis的分布式解决方案,在 3.0 版本正式推出。

准备工作

1.确定本机IP地址

 2.配置 redis.conf 批量生成脚本

redis-cluster.tmpl

# redis端口
port ${PORT}
# 关闭保护模式
protected-mode no
# 开启集群
cluster-enabled yes
# 集群节点配置
cluster-config-file nodes.conf
# 超时
cluster-node-timeout 5000
# 集群节点IP host模式为宿主机IP
cluster-announce-ip 192.168.126.128
# 集群节点端口 7001 - 7006
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
# 开启 appendonly 备份模式
appendonly yes
# 每秒钟备份
appendfsync everysec
# 对aof文件进行压缩时,是否执行同步操作
no-appendfsync-on-rewrite no
# 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写
auto-aof-rewrite-percentage 100
# 重写前AOF文件的大小最小值 默认 64mb
auto-aof-rewrite-min-size 64mb
# 配置数据库个数
database 16

redis-cluster-config.sh

for port in `seq 7001 7006`; do \
  mkdir -p ./redis-cluster/${port}/conf \
  && PORT=${port} envsubst < ./redis-cluster.tmpl > ./redis-cluster/${port}/conf/redis.conf \
  && mkdir -p ./redis-cluster/${port}/data; \
done

执行redis-cluster-config.sh,在当前文件路径下生成 redis-cluster 文件夹及相关配置文件

redis-cluster.yml

version: '3.7'

services:
  redis7001:
    image: redis
    container_name: redis7001
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7001/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7001/data:/data
    ports:
      - 7001:7001
      - 17001:17001
    environment:
      - TZ=Asia/Shanghai

  redis7002:
    image: redis
    container_name: redis7002
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7002/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7002/data:/data
    ports:
      - 7002:7002
      - 17002:17002
    environment:
      - TZ=Asia/Shanghai

  redis7003:
    image: redis
    container_name: redis7003
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7003/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7003/data:/data
    ports:
      - 7003:7003
      - 17003:17003
    environment:
      - TZ=Asia/Shanghai

  redis7004:
    image: redis
    container_name: redis7004
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7004/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7004/data:/data
    ports:
      - 7004:7004
      - 17004:17004
    environment:
      - TZ=Asia/Shanghai

  redis7005:
    image: redis
    container_name: redis7005
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7005/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7005/data:/data
    ports:
      - 7005:7005
      - 17005:17005
    environment:
      - TZ=Asia/Shanghai

  redis7006:
    image: redis
    container_name: redis7006
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis-cluster/7006/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./redis-cluster/7006/data:/data
    ports:
      - 7006:7006
      - 17006:17006
    environment:
      - TZ=Asia/Shanghai

如图所示:

上传 redis-cluster文件夹 及 redis-cluster.yml到虚拟机指定路径下

docker 操作

1.创建 redis7001-7006 容器

在当前路径下执行

docker-compose -f redis-cluster.yml up -d

 查看容器是否启动成功

docker ps -a

 2.集群配置

 创建 cluster 集群

docker exec -it redis7001 redis-cli -p 7001 --cluster create \
192.168.126.128:7001 192.168.126.128:7002 \
192.168.126.128:7003 192.168.126.128:7004 \
192.168.126.128:7005 192.168.126.128:7006 \
--cluster-replicas 1

 这里一定要按照要求输入:yes

否则会出现创建失败,cluster node之间无法通信

 如此就成功了。

测试集群操作

连接redis

docker exec -it redis7001 redis-cli -h 192.168.126.128 -p 7003 -c

1.查看集群状态

cluster nodes

 2.查看slots分片

cluster slots

 3.查看集群信息

cluster info

 

如此一个简单的 redis cluster集群就搭建好了。

posted @ 2020-11-28 14:37  苏戏  阅读(418)  评论(0编辑  收藏  举报