美文网首页
Redis集群Docker部署

Redis集群Docker部署

作者: hcq0514 | 来源:发表于2020-10-29 17:41 被阅读0次

    因机器限制,用两台主机部署一个三主三从的redis集群架构(记为1,2)

    1号机部署步骤

    • 新建redis目录
    mkdir /mydata/redis 
    cd /mydata/redis 
    
    • 编写 redis-cluster.tmpl 文件
    vim redis-cluster.tmpl
    
    port ${PORT} #节点端口
    requirepass xx#密码
    masterauth xx#与上面相同
    protected-mode no  #保护模式,默认值 yes,即开启。开启保护模式以后,需配置 bind ip 或者设置访问密码关闭保护模式,外部网络可以直接访问;
    daemonize no #是否以守护线程的方式启动(后台启动),默认 no;
    appendonly yes #是否开启 AOF 持久化模式,默认 no;
    cluster-enabled yes #是否开启集群模式,默认 no;
    cluster-config-file nodes.conf #集群节点信息文件;
    cluster-node-timeout 15000 #集群节点连接超时时间;
    cluster-announce-ip 47.100.97.4 #1号机器的公网ip
    cluster-announce-port ${PORT} #集群节点映射端口;
    cluster-announce-bus-port 1${PORT} #集群节点总线端口。
    
    
    • 执行命令,创建目录
    for port in `seq 6371 6373`; do \
      mkdir -p ${port}/conf \
      && PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
      && mkdir -p ${port}/data;\
    done
    
    • 应用tree查看文件结构(安装插件yum install -y tree)
    [root@iZ8vb6m7o2mthc510ymisjZ redis]# tree
    .
    ├── 6371
    │   ├── conf
    │   │   └── redis.conf
    │   └── data
    ├── 6372
    │   ├── conf
    │   │   └── redis.conf
    │   └── data
    ├── 6373
    │   ├── conf
    │   │   └── redis.conf
    │   └── data
    └── redis-cluster.tmpl
    
    • 在/mydata/redis瞎新建文件docker-compose.yml
    version: "3.8"
    
    services:
      redis-6371: # 服务名称
        image: redis # 创建容器时所需的镜像
        container_name: redis-6371 # 容器名称
        restart: always # 容器总是重新启动
        network_mode: "host" # host 网络模式
        volumes: # 数据卷,目录挂载
          - /mydata/redis/6371/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /mydata/redis/6371/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
    
      redis-6372:
        image: redis
        container_name: redis-6372
        network_mode: "host"
        volumes:
        volumes: # 数据卷,目录挂载
          - /mydata/redis/6372/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /mydata/redis/6372/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
    
      redis-6373:
        image: redis
        container_name: redis-6373
        network_mode: "host"
        volumes:
        volumes: # 数据卷,目录挂载
          - /mydata/redis/6373/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /mydata/redis/6373/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
    
    • 执行安装命令
    docker-compose up -d
    

    2号机部署步骤

    • 目录层级与1号机想同

    • redis-cluster.tmpl文件(主要是ip不同)

    port ${PORT}
    requirepass xx
    masterauth xx ##与一号机相同
    protected-mode no
    daemonize no
    appendonly yes
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 15000
    cluster-announce-ip 39.99.156.91 # 二号机的公网ip
    cluster-announce-port ${PORT}
    cluster-announce-bus-port 1${PORT}
    
    • docker-compose.yml文件(主要是端口不同)
    version: "3.8"
    
    services:
      redis-6374: # 服务名称
        image: redis # 创建容器时所需的镜像
        container_name: redis-6374 # 容器名称
        restart: always # 容器总是重新启动
        network_mode: "host" # host 网络模式
        volumes: # 数据卷,目录挂载
          - /mydata/redis/6374/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /mydata/redis/6374/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
    
      redis-6375:
        image: redis
        container_name: redis-6375
        network_mode: "host"
        volumes:
        volumes: # 数据卷,目录挂载
          - /mydata/redis/6375/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /mydata/redis/6375/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
    
      redis-6376:
        image: redis
        container_name: redis-6376
        network_mode: "host"
        volumes:
        volumes: # 数据卷,目录挂载
          - /mydata/redis/6376/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /mydata/redis/6376/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
    

    2台主机都部署完成后选择1号机进入容器

    docker exec -it redis-6371 bash
    
    redis-cli -a xxx(这边是你上面设置的密码)  --cluster create 39.99.156.91:6371 39.99.156.91:6372 39.99.156.91:6373 47.100.97.46:6374 47.100.97.46:6375 47.100.97.46:6376 --cluster-replicas 1
    
    创建成功后
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    >>> Performing hash slots allocation on 6 nodes...
    Master[0] -> Slots 0 - 5460
    Master[1] -> Slots 5461 - 10922
    Master[2] -> Slots 10923 - 16383
    Adding replica 47.100.97.46:6376 to 39.99.156.91:6371
    Adding replica 39.99.156.91:6373 to 47.100.97.46:6374
    Adding replica 47.100.97.46:6375 to 39.99.156.91:6372
    M: a0df3ef56079f79cdb0114998a7649bb94119f28 39.99.156.91:6371
       slots:[0-5460] (5461 slots) master
    M: f4200a0aa263639809a7af7bf89cc75a9c0645de 39.99.156.91:6372
       slots:[10923-16383] (5461 slots) master
    S: 920af8d06e11536a8dd22162d091837b1d9f1077 39.99.156.91:6373
       replicates fe81cc42f39a49330c47523d5b4598c02526d95e
    M: fe81cc42f39a49330c47523d5b4598c02526d95e 47.100.97.46:6374
       slots:[5461-10922] (5462 slots) master
    S: 37483fa11fc93be2c1d0b5b259537a3aec357c80 47.100.97.46:6375
       replicates f4200a0aa263639809a7af7bf89cc75a9c0645de
    S: c647cbcfc0f701b859b35978a430604decfe751d 47.100.97.46:6376
       replicates a0df3ef56079f79cdb0114998a7649bb94119f28
    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 39.99.156.91:6371)
    M: a0df3ef56079f79cdb0114998a7649bb94119f28 39.99.156.91:6371
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: 920af8d06e11536a8dd22162d091837b1d9f1077 39.99.156.91:6373
       slots: (0 slots) slave
       replicates fe81cc42f39a49330c47523d5b4598c02526d95e
    S: 37483fa11fc93be2c1d0b5b259537a3aec357c80 47.100.97.46:6375
       slots: (0 slots) slave
       replicates f4200a0aa263639809a7af7bf89cc75a9c0645de
    M: fe81cc42f39a49330c47523d5b4598c02526d95e 47.100.97.46:6374
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: f4200a0aa263639809a7af7bf89cc75a9c0645de 39.99.156.91:6372
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: c647cbcfc0f701b859b35978a430604decfe751d 47.100.97.46:6376
       slots: (0 slots) slave
       replicates a0df3ef56079f79cdb0114998a7649bb94119f28
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    • 测试
    我们连接的是39.99.156.91:6372 这台机器,当操作写入时,他就变成了6374去了
    redis-cluster(39.99.156.91:6372)>set name mrhelloworld
    "OK"
    redis-cluster(47.100.97.46:6374)>
    
    

    相关文章

      网友评论

          本文标题:Redis集群Docker部署

          本文链接:https://www.haomeiwen.com/subject/hnxlvktx.html