• Docker搭建Redis集群


    Docker搭建Redis集群

    注意:Redis搭建集群最少需要6个Redis节点,其中3个作为主节点,3个作为从节点;

    为了方便,这里用Docker在一台机器上启动6个容器来作为集群,生产上建议用多台服务器搭建

    1. 创建Redis配置文件

      集群的访问密码,Redis访问密码需要自己改以下,这里默认为2022

    #!/bin/bash
    if [ ! $1 ]; then
        echo "请输入Redis配置文件存放路径,例如:/home"
        exit 1
    fi
    for port in $(seq 1 6);
    do
    mkdir -p $1/redis/node-${port}/conf
    mkdir -p $1/redis/node-${port}/data
    touch $1/redis/node-${port}/conf/redis.conf
    cat << EOF > $1/redis/node-${port}/conf/redis.conf
    #port(端口号)
    port 6379
    bind 0.0.0.0
    #masterauth(设置集群节点间访问密码,跟下面一致)
    masterauth 2022
    #requirepass(设置Redis访问密码)
    requirepass 2022
    #cluster-enabled yes(启动集群模式)
    cluster-enabled yes
    #cluster-config-file nodes.conf(集群节点信息文件)
    cluster-config-file nodes.conf
    #cluster-node-timeout 5000(Redis节点宕机被发现的时间)
    cluster-node-timeout 5000
    #cluster-announce-ip(集群节点的汇报ip,防止nat,预先填写为网关ip后续需要手动修改配置文件)
    cluster-announce-ip 172.18.0.1${port}
    #cluster-announce-port(集群节点的汇报port,防止nat)
    cluster-announce-port 6379
    #cluster-announce-bus-port(集群节点的汇报bus-port,防止nat)
    cluster-announce-bus-port 16379
    #appendonly yes(开启aof)
    appendonly yes
    EOF
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    运行脚本,在home目录下可以看到创建好的6个Redis配置文件

    (base) ➜  ~ chmod +x redis_conf.sh
    (base) ➜  ~ ./redis_conf.sh /home
    
    • 1
    • 2

    image-20221119204228130

    1. 创建docker-compose脚本

    自行修改IP和端口,需要和上面的Redis配置文件保持一致

    version: '3'
    services:
      # redis-1
      redis-1:
        image: redis:latest
        container_name: redis-1
        hostname: redis-1
        ports:
          - "6371:6379"
          - "16371:16379"
        command: "redis-server /etc/redis/redis.conf"
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - ./redis/node-1/data:/data
          - ./redis/node-1/conf/redis.conf:/etc/redis/redis.conf
        networks:
          tools_net:
            ipv4_address: 172.18.0.11
      # redis-2
      redis-2:
        image: redis:latest
        container_name: redis-2
        hostname: redis-2
        ports:
          - "6372:6379"
          - "16372:16379"
        command: "redis-server /etc/redis/redis.conf"
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - ./redis/node-2/data:/data
          - ./redis/node-2/conf/redis.conf:/etc/redis/redis.conf
        networks:
          tools_net:
            ipv4_address: 172.18.0.12
      # redis-3
      redis-3:
        image: redis:latest
        container_name: redis-3
        hostname: redis-3
        ports:
          - "6373:6379"
          - "16373:16379"
        command: "redis-server /etc/redis/redis.conf"
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - ./redis/node-3/data:/data
          - ./redis/node-3/conf/redis.conf:/etc/redis/redis.conf
        networks:
          tools_net:
            ipv4_address: 172.18.0.13
      # redis-4
      redis-4:
        image: redis:latest
        container_name: redis-4
        hostname: redis-4
        ports:
          - "6374:6379"
          - "16374:16379"
        command: "redis-server /etc/redis/redis.conf"
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - ./redis/node-4/data:/data
          - ./redis/node-4/conf/redis.conf:/etc/redis/redis.conf
        networks:
          tools_net:
            ipv4_address: 172.18.0.14
      # redis-5
      redis-5:
        image: redis:latest
        container_name: redis-5
        hostname: redis-5
        ports:
          - "6375:6379"
          - "16375:16379"
        command: "redis-server /etc/redis/redis.conf"
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - ./redis/node-5/data:/data
          - ./redis/node-5/conf/redis.conf:/etc/redis/redis.conf
        networks:
          tools_net:
            ipv4_address: 172.18.0.15
      # redis-6
      redis-6:
        image: redis:latest
        container_name: redis-6
        hostname: redis-6
        ports:
          - "6376:6379"
          - "16376:16379"
        command: "redis-server /etc/redis/redis.conf"
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - ./redis/node-6/data:/data
          - ./redis/node-6/conf/redis.conf:/etc/redis/redis.conf
        networks:
          tools_net:
            ipv4_address: 172.18.0.16
    networks:
      tools_net:
        ipam:
          config:
            - subnet: 172.18.0.0/24
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109

    启动Redis

    (base) ➜  ~ docker-compose up -d
    (base) ➜  ~ docker ps
    
    • 1
    • 2

    image-20221119205048941

    1. 配置Redis集群

    进入其中一台容器,进行Redis集群配置

    redis-cli -a 2022 --cluster create 172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379 172.18.0.15:6379 172.18.0.16:6379 --cluster-replicas 1

    (base) ➜  ~ docker exec -it redis-1 bash
    # -a 为上面设置的密码
    root@redis-1:/data# redis-cli -a 2022 --cluster create 172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379 172.18.0.15:6379 172.18.0.16:6379 --cluster-replicas 1
    # 回车后,会有提示,是否同意设置上面的配置,输入yes,然后集群就算搭建成功了;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    image-20221119210030627

    1. 验证集群是否搭建成功

    新增一个key,可以看到,数据成功的存入了redis-2这台容器

    # 进入其中一台容器,执行如下命令,以集群模式进入
    root@redis-1:/data# redis-cli -c -a 2022
    127.0.0.1:6379> set name yid
    -> Redirected to slot [5798] located at 172.18.0.12:6379
    OK
    172.18.0.12:6379> get name
    "yid"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    image-20221119210414688

    # 查看节点
    172.18.0.12:6379> CLUSTER NODES
    
    • 1
    • 2

    image-20221119210812424

  • 相关阅读:
    第十六章:构建n(5,7)阶素数幻方
    34. 在排序数组中查找元素的第一个和最后一个位置
    c#设计模式-结构型模式 之 享元模式
    ICT产业关联效应的国际比较——基于投入产出的分析
    【修车案例】一波形一案例(9)
    【架构】 第7章 主从复制高可用Redis集群
    温湿度计传感器DHT11控制数码管显示verilog代码及视频
    电脑重装系统后Word文档如何横向排版
    哈夫曼树构建、编码、译码C++实现
    动态爬虫管理平台构建与实现_kaic
  • 原文地址:https://blog.csdn.net/weixin_43577131/article/details/127942067