登录
  • 欢迎访问悠扬的技术博客,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站😉

docker-compose 部署redis-cluster

Redis 悠扬 1120次浏览 已收录 0个评论

准备工作

#创建工作目录
mkdir -p /home/redis-cluster/{log,conf} 
#赋权,免得一会容器出现挂载没有权限
chmod -R 777 /home/redis-cluster/*

1.配置文件脚本

vim /home/redis-cluster/conf/redis-cluster.tmpl
# redis端口
port ${PORT}
#redis 访问密码
requirepass 123456
#redis 访问Master节点密码
masterauth 123456
# 关闭保护模式
protected-mode no
# 开启集群
cluster-enabled yes
# 集群节点配置
cluster-config-file nodes.conf
# 超时
cluster-node-timeout 5000
# 集群节点IP host模式为宿主机IP
cluster-announce-ip 172.168.9.133
# 集群节点端口 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

# 日志配置
# debug:会打印生成大量信息,适用于开发/测试阶段
# verbose:包含很多不太有用的信息,但是不像debug级别那么混乱
# notice:适度冗长,适用于生产环境
# warning:仅记录非常重要、关键的警告消息
loglevel notice
# 日志文件路径
logfile "/home/redis-cluster/log/redis${PORT}.log"

2.准备配置文件生成脚本

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

3.配置docker-compose.yml

vim /home/redis-cluster/conf/docker-compose.yml
version: '3.7'

services:
  redis7001:
    image: redis:6
    container_name: redis7001
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./7001/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./7001/data:/data
      - /home/redis-cluster/log/redis7001.log:/home/redis-cluster/log/redis7001.log
    ports:
      - "7001:7001"
      - "17001:17001"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai
      - TAKE_FILE_OWNERSHIP=true
    logging:
      options:
        max-size: '100m'
        max-file: '10'

  redis7002:
    image: redis:6
    container_name: redis7002
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./7002/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./7002/data:/data
      - /home/redis-cluster/log/redis7002.log:/home/redis-cluster/log/redis7002.log
    ports:
      - "7002:7002"
      - "17002:17002"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai
      - TAKE_FILE_OWNERSHIP=true
    logging:
      options:
        max-size: '100m'
        max-file: '10'

  redis7003:
    image: redis:6
    container_name: redis7003
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./7003/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./7003/data:/data
      - /home/redis-cluster/log/redis7003.log:/home/redis-cluster/log/redis7003.log
    ports:
      - "7003:7003"
      - "17003:17003"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai
      - TAKE_FILE_OWNERSHIP=true
    logging:
      options:
        max-size: '100m'
        max-file: '10'

  redis7004:
    image: redis:6
    container_name: redis7004
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./7004/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./7004/data:/data
      - /home/redis-cluster/log/redis7004.log:/home/redis-cluster/log/redis7004.log
    ports:
      - "7004:7004"
      - "17004:17004"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai
      - TAKE_FILE_OWNERSHIP=true
    logging:
      options:
        max-size: '100m'
        max-file: '10'

  redis7005:
    image: redis:6
    container_name: redis7005
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./7005/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./7005/data:/data
      - /home/redis-cluster/log/redis7005.log:/home/redis-cluster/log/redis7005.log
    ports:
      - "7005:7005"
      - "17005:17005"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai
      - TAKE_FILE_OWNERSHIP=true
    logging:
      options:
        max-size: '100m'
        max-file: '10'

  redis7006:
    image: redis:6
    container_name: redis7006
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./7006/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - ./7006/data:/data
      - /home/redis-cluster/log/redis7006.log:/home/redis-cluster/log/redis7006.log
    ports:
      - "7006:7006"
      - "17006:17006"
    environment:
      # 设置时区为上海,否则时间会有问题
      - TZ=Asia/Shanghai
      - TAKE_FILE_OWNERSHIP=true
    logging:
      options:
        max-size: '100m'
        max-file: '10'
        
networks:
  app_net:
    external: true

4. 启动容器

cd /home/redis-cluster/conf
docker-compose up -d

5.进入容器做集群配置

docker exec -it redis7001 redis-cli -p 7001 -a 123456 –cluster create 172.168.9.133:7001 172.168.9.133:7002 172.168.9.133:7003 172.168.9.133:7004 172.168.9.133:7005 172.168.9.133:7006 –cluster-replicas 1

6.集群操作

#进入容器操作集群
docker exec -it redis7001 redis-cli -p 7001 -h 172.168.9.133 -a 123456 -c
#查看集群节点
cluster nodes
#查看slots分片
cluster slots
#查看集群信息
cluster info
#查询数据 正常访问操作就行
keys *

版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明docker-compose 部署redis-cluster
喜欢 (0)
支付宝[]
分享 (0)
悠扬
关于作者:
10年以上工作经验,从事2年微服务架构搭建工作,有大数据处理相关工作经验,使用spring全家桶包括:Spring,SpringBoot,SpringCloud 数据层组件服务使用SpringDataJpa,Mybatis以及其他第三方组件Sharding-JDBC,Sharding-Proxy分库分表。熟悉微服务,服务降级,限流,分流,做过项目源码修改,有cat,apollo,nacos使用经验,有Lostash,Elasticsearch,kibana,mysqlMHA生产实践经验,使用开源代码Apache Sarding项目,修改源码支持mysql分库分表使用年月日小时分库分表,docker做集群服务,Jekins做项目发布,GitLab做项目管理,使用docker容器部署,熟悉消息队列RabbitMQ,Kafka,ActiveMQ。RuoYi-Vue-Atomikos项目开源加入生态圈组件,项目支持分布式事务,界面添加多数据源,数据源动态配置,切面切换,多数据源事务支持,支持区域数据源配置,用于区域数据切分,数据层次分库。项目地址:https://gitee.com/zsiyang/ruoyi-vue-atomikos
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址