Docker快速建立Redis集群(Cluster)并配置外网访问

本篇文章介绍redis集群的快速搭建,不对redis进行深入探究,方便想要了解redis集群的朋友快速进行测试。

服务器基础配置

创建挂载目录

mkdir /mnt/data/rediscluster/7001/data
mkdir /mnt/data/rediscluster/7001/conf
mkdir /mnt/data/rediscluster/7002/data
mkdir /mnt/data/rediscluster/7002/conf
mkdir /mnt/data/rediscluster/7003/data
mkdir /mnt/data/rediscluster/7003/conf
mkdir /mnt/data/rediscluster/7101/data
mkdir /mnt/data/rediscluster/7101/conf
mkdir /mnt/data/rediscluster/7102/data
mkdir /mnt/data/rediscluster/7102/conf
mkdir /mnt/data/rediscluster/7103/data
mkdir /mnt/data/rediscluster/7103/conf

分别在每个conf目录下创建文件redis.conf

#端口号,写文件夹对映的端口
Port 7001
#开启集群
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
#开启aof存储
appendonly yes
#设置登录密码
requirepass 123123
#设置节点密码,集群必设
masterauth 123123
# 关闭保护,外网可直接访问
protected-mode no

开放端口

iptables -I INPUT -p tcp --dport 7001 -j ACCEPT
iptables -I INPUT -p tcp --dport 7002 -j ACCEPT
iptables -I INPUT -p tcp --dport 7003 -j ACCEPT
iptables -I INPUT -p tcp --dport 7101 -j ACCEPT
iptables -I INPUT -p tcp --dport 7102 -j ACCEPT
iptables -I INPUT -p tcp --dport 7103 -j ACCEPT
iptables -I INPUT -p tcp --dport 17001 -j ACCEPT
iptables -I INPUT -p tcp --dport 17002 -j ACCEPT
iptables -I INPUT -p tcp --dport 17003 -j ACCEPT
iptables -I INPUT -p tcp --dport 17101 -j ACCEPT
iptables -I INPUT -p tcp --dport 17102 -j ACCEPT
iptables -I INPUT -p tcp --dport 17103 -j ACCEPT

同时在云服务器安全组开放以上端口

docker 配置

编辑docker-compose.yml文件

version: "3.4"

services:
  redis1:
    image: redis:5.0.3
    restart: "no"
    container_name: redis5-m1
    command: redis-server /etc/redis/redis.conf
    volumes:
      - /mnt/data/rediscluster/7001/data:/data
      - /mnt/data/rediscluster/7001/conf/redis.conf:/etc/redis/redis.conf
    network_mode: "host"

  redis2:
    image: redis:5.0.3
    restart: "no"
    container_name: redis5-m2
    command: redis-server /etc/redis/redis.conf
    volumes:
      - /mnt/data/rediscluster/7002/data:/data
      - /mnt/data/rediscluster/7002/conf/redis.conf:/etc/redis/redis.conf
    network_mode: "host"

  redis3:
    image: redis:5.0.3
    restart: "no"
    container_name: redis5-m3
    command: redis-server /etc/redis/redis.conf
    volumes:
      - /mnt/data/rediscluster/7003/data:/data
      - /mnt/data/rediscluster/7003/conf/redis.conf:/etc/redis/redis.conf
    network_mode: "host"

  redis4:
    image: redis:5.0.3
    restart: "no"
    container_name: redis5-s1
    command: redis-server /etc/redis/redis.conf
    volumes:
      - /mnt/data/rediscluster/7101/data:/data
      - /mnt/data/rediscluster/7101/conf/redis.conf:/etc/redis/redis.conf
    network_mode: "host"

  redis5:
    image: redis:5.0.3
    restart: "no"
    container_name: redis5-s2
    command: redis-server /etc/redis/redis.conf
    volumes:
      - /mnt/data/rediscluster/7102/data:/data
      - /mnt/data/rediscluster/7102/conf/redis.conf:/etc/redis/redis.conf
    network_mode: "host"

  redis6:
    image: redis:5.0.3
    restart: "no"
    container_name: redis5-s3
    command: redis-server /etc/redis/redis.conf
    volumes:
      - /mnt/data/rediscluster/7103/data:/data
      - /mnt/data/rediscluster/7103/conf/redis.conf:/etc/redis/redis.conf
    network_mode: "host"

执行命令

docker-compose up -d

运行命令docker ps查看redis6个实例是否都启动成功

建立集群

随意进入一个容器内

docker exec -it redis5-m1 /bin/bash

在容器内执行命令:

redis-cli --cluster create 外网ip:7001  外网ip:7002  外网ip:7003 外网ip:7101 外网ip:7102  外网ip:7103  --cluster-replicas 1

中途会提示是否启用上述配置,输入 yes

执行命令:exit 退出容器

至此集群搭建完成

查看集群当前状态

随意进入一个容器内输入命令:

redis-cli -a 123123 --cluster info 外网ip:7001

注意:

建立集群时需要保证集群每个节点中都没有数据。如果写入了数据想要重建集群,需要先把
/mnt/data/rediscluster/*/data 目录下的文件清空

find /mnt/data/rediscluster/   -name "nodes.*" |xargs rm -rf
find /mnt/data/rediscluster/   -name "*.aof" |xargs rm -rf
posted @ 2021-03-17 17:03  张三~~  阅读(1966)  评论(0编辑  收藏  举报