zoukankan      html  css  js  c++  java
  • Redis集群-docker搭建篇

    搭建环境

    • docker 镜像redis5

    搭建过程

    1. 配置文件
      Docker-compose.yml
    version: "3.6"
    services: # 代表就是一组服务 - 简单来说一组容器
      redis_200: # 这个表示服务的名称,课自定义; 注意不是容器名称
        image: redis_debin # 指定容器的镜像文件
        networks: ## 引入外部预先定义的网段
          cluster_net:
            ipv4_address: 192.168.3.200 #设置ip地址
        container_name: redis5_cluster_200 # 这是容器的名称
        ports: # 配置容器与宿主机的端口
        - "6320:6379"
        - "16320:16379"
        volumes: # 配置数据挂载
        - /Users/jihan/docker/cluster/redis5/200:/redis
        # command: top
        command: /usr/local/bin/redis-server /redis/conf/redis.conf  
      redis_201: # 这个表示服务的名称,课自定义; 注意不是容器名称
        image: redis_debin # 指定容器的镜像文件
        networks: ## 引入外部预先定义的网段
          cluster_net:
            ipv4_address: 192.168.3.201 #设置ip地址
        container_name: redis5_cluster_201 # 这是容器的名称
        ports: # 配置容器与宿主机的端口
        - "6321:6379"
        - "16321:16379"
        volumes: # 配置数据挂载
        - /Users/jihan/docker/cluster/redis5/201:/redis
        # command: top
        command: /usr/local/bin/redis-server /redis/conf/redis.conf  
      redis_202: # 这个表示服务的名称,课自定义; 注意不是容器名称
        image: redis_debin # 指定容器的镜像文件
        networks: ## 引入外部预先定义的网段
          cluster_net:
            ipv4_address: 192.168.3.202 #设置ip地址
        container_name: redis5_cluster_202 # 这是容器的名称
        ports: # 配置容器与宿主机的端口
        - "6322:6379"
        - "16322:16379"
        volumes: # 配置数据挂载
        - /Users/jihan/docker/cluster/redis5/202:/redis
        # command: top
        command: /usr/local/bin/redis-server /redis/conf/redis.conf  
      redis_203: # 这个表示服务的名称,课自定义; 注意不是容器名称
        image: redis_debin # 指定容器的镜像文件
        networks: ## 引入外部预先定义的网段
          cluster_net:
            ipv4_address: 192.168.3.203 #设置ip地址
        container_name: redis5_cluster_203 # 这是容器的名称
        ports: # 配置容器与宿主机的端口
        - "6323:6379"
        - "16323:16379"
        volumes: # 配置数据挂载
        - /Users/jihan/docker/cluster/redis5/203:/redis
        # command: top
        command: /usr/local/bin/redis-server /redis/conf/redis.conf  
      redis_204: # 这个表示服务的名称,课自定义; 注意不是容器名称
        image: redis_debin # 指定容器的镜像文件
        networks: ## 引入外部预先定义的网段
          cluster_net:
            ipv4_address: 192.168.3.204 #设置ip地址
        container_name: redis5_cluster_204 # 这是容器的名称
        ports: # 配置容器与宿主机的端口
        - "6324:6379"
        - "16324:16379"
        volumes: # 配置数据挂载
        - /Users/jihan/docker/cluster/redis5/204:/redis
        # command: top
        command: /usr/local/bin/redis-server /redis/conf/redis.conf  
      redis_205: # 这个表示服务的名称,课自定义; 注意不是容器名称
        image: redis_debin # 指定容器的镜像文件
        networks: ## 引入外部预先定义的网段
          cluster_net:
            ipv4_address: 192.168.3.205 #设置ip地址
        container_name: redis5_cluster_205 # 这是容器的名称
        ports: # 配置容器与宿主机的端口
        - "6325:6379"
        - "16325:16379"
        volumes: # 配置数据挂载
        - /Users/jihan/docker/cluster/redis5/205:/redis
        # command: top
        command: /usr/local/bin/redis-server /redis/conf/redis.conf
      
    # 网段设置
    networks:
    #引用外部预先定义好的网段
      cluster_net:
        external:
          name: cluster_net
    

    提前条件:1. 准备redis5镜像,镜像中默认创建 /redis 目录,用于挂载redis共享文件目录
    2. 构建

    $ docker-compose up -d
    Creating redis5_cluster_204 ... done
    Creating redis5_cluster_200 ... done
    Creating redis5_cluster_203 ... done
    Creating redis5_cluster_202 ... done
    Creating redis5_cluster_205 ... done
    Creating redis5_cluster_201 ... done
    
    1. 启动
    $ docker-compose start
    Starting redis_200 ... done
    Starting redis_201 ... done
    Starting redis_202 ... done
    Starting redis_203 ... done
    Starting redis_204 ... done
    Starting redis_205 ... done
    

    查看是否启动成功

    $ docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                                                             NAMES
    b0ed507d505f        redis_debin         "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds                0.0.0.0:6320->6379/tcp, 0.0.0.0:16320->16379/tcp                  redis5_cluster_200
    52a158fef97a        redis_debin         "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds                0.0.0.0:6325->6379/tcp, 0.0.0.0:16325->16379/tcp                  redis5_cluster_205
    b817794cfaa8        redis_debin         "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds                0.0.0.0:6324->6379/tcp, 0.0.0.0:16324->16379/tcp                  redis5_cluster_204
    77a37266d46a        redis_debin         "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds                0.0.0.0:6321->6379/tcp, 0.0.0.0:16321->16379/tcp                  redis5_cluster_201
    c52d44b26bf2        redis_debin         "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds                0.0.0.0:6322->6379/tcp, 0.0.0.0:16322->16379/tcp                  redis5_cluster_202
    35a442807dcc        redis_debin         "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds                0.0.0.0:6323->6379/tcp, 0.0.0.0:16323->16379/tcp                  redis5_cluster_203
    

    4 . 挑一个容器查看是否有开启集群配置

    $ docker exec -ti b0ed507d505f /bin/bash
    
    root@b0ed507d505f:/data# cat /redis/log/nodes.conf
    d40ed1db40261c4f5a37410eeb828d4958769caf :0@0 myself,master - 0 0 0 connected
    vars currentEpoch 0 lastVoteEpoch 0
    
    1. 命令启动各个节点进分配槽
    $ docker exec -ti 73a402d74494 /bin/bash
    root@73a402d74494:/data# redis-cli --cluster create 192.168.3.200:6379 192.168.3.201:6379 192.168.3.202:6379 192.168.3.203:6379 192.168.3.204:6379 192.168.3.205:6379 --cluster-replicas 1
    >>> Performing hash slots allocation on 6 nodes...
    Master[0] -> Slots 0 - 5460
    Master[1] -> Slots 5461 - 10922
    Master[2] -> Slots 10923 - 16383
    Adding replica 192.168.3.204:6379 to 192.168.3.200:6379
    Adding replica 192.168.3.205:6379 to 192.168.3.201:6379
    Adding replica 192.168.3.203:6379 to 192.168.3.202:6379
    M: f4d24c9b4ed5142884451410c5eca71416b7202b 192.168.3.200:6379
       slots:[0-5460] (5461 slots) master
    M: cc60b1a75abc777fc2712125112082780fb8f1fb 192.168.3.201:6379
       slots:[5461-10922] (5462 slots) master
    M: 9c3bb8fe85725d741ffc0cb7dfa2e92015ec4543 192.168.3.202:6379
       slots:[10923-16383] (5461 slots) master
    S: 655584d4982243a9ae025c3cecaf9e292dda18e1 192.168.3.203:6379
       replicates 9c3bb8fe85725d741ffc0cb7dfa2e92015ec4543
    S: 9db4e65ea94a8f9ed6e374b02831168a5e75831a 192.168.3.204:6379
       replicates f4d24c9b4ed5142884451410c5eca71416b7202b
    S: 011f6c7c2ecf4ac6bed088369707fd5ede9d519a 192.168.3.205:6379
       replicates cc60b1a75abc777fc2712125112082780fb8f1fb
    Can I set the above configuration? (type 'yes' to accept): yes
    >>> Nodes configuration updated
    >>> Assign a different config epoch to each node
    >>> Sending CLUSTER MEET messages to join the cluster
    Waiting for the cluster to join
    .....
    >>> Performing Cluster Check (using node 192.168.3.200:6379)
    M: f4d24c9b4ed5142884451410c5eca71416b7202b 192.168.3.200:6379
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: 9db4e65ea94a8f9ed6e374b02831168a5e75831a 192.168.3.204:6379
       slots: (0 slots) slave
       replicates f4d24c9b4ed5142884451410c5eca71416b7202b
    M: cc60b1a75abc777fc2712125112082780fb8f1fb 192.168.3.201:6379
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 655584d4982243a9ae025c3cecaf9e292dda18e1 192.168.3.203:6379
       slots: (0 slots) slave
       replicates 9c3bb8fe85725d741ffc0cb7dfa2e92015ec4543
    S: 011f6c7c2ecf4ac6bed088369707fd5ede9d519a 192.168.3.205:6379
       slots: (0 slots) slave
       replicates cc60b1a75abc777fc2712125112082780fb8f1fb
    M: 9c3bb8fe85725d741ffc0cb7dfa2e92015ec4543 192.168.3.202:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    注意:
    由于过程中出现配置分配槽命令漏了给各个节点分配一个从节点,重新配置时出现最下面第问题,所以重新构建第各个节点,因此下面容器ID与上面不一样

    问题
    root@df2aa36ec786:/data# redis-cli --cluster create 192.168.3.200:6379 192.168.3.201:6379 192.168.3.202:6379 192.168.3.203:6379 192.168.3.204:6379 192.168.3.205:6379 --cluster-replicas 1
    [ERR] Node 192.168.3.200:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

    清楚aof dump.rdb文件,进入各个节点flushall 清楚数据库

  • 相关阅读:
    简单工厂模式
    设计模式概述
    Excel中如何按单元格颜色求和,这五种牛批的方法,值得学习
    Excel数据透视表只能求和运算?快来学习求差运算小技巧
    如何在Excel中分组排名?两个公式轻松搞定!
    开始菜单之数字格式,这些基础知识还记得吗?
    2021,我来了
    2020年会必备,Excel轻松制作抽奖小游戏
    如何用Excel制作工作计划,跟踪任务进度,快来学习吧
    如何防止Excel数据录入出错,巧用数据验证,实现自动限制录入
  • 原文地址:https://www.cnblogs.com/smallyi/p/13383618.html
Copyright © 2011-2022 走看看