docker-compose 部署redis-cluster (全程脚本)

目录

准备工作

1.配置文件脚本

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

3.配置docker-compose.yml

4. 启动容器

5.进入容器做集群配置

6.集群操作


准备工作

#创建工作目录
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 *

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

八方来财添好运

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值