• Redis-cluster集群搭建(redis版本5.0.4)


      一、集群特点

      1)Redis-sentinel集群只有一个主节点,并发写请求过大时不能缓解写压力;每个节点的数据都是一样的。

      2)Redis-cluster集群有多个主节点,每个主节点存放的数据不一样,主节点与从节点存放数据一样。

      二、准备工作

      知识准备:

      1)linux下安装单机版redis

      2)redis的启动与关闭以及客户端连接

      3)linux下redis的主从复制

      4)linux下redis的哨兵模式

      5)Redis-cluster集群介绍以及搭建集群示例(redis版本3.0.0)

      环境准备:

      1)准备6台服务器:192.168.43.121,192.168.43.122,192.168.43.131,192.168.43.132,192.168.43.141,192.168.43.142

      2)参考知识准备的1)在每台服务器上安装redis-5.0.4

      三、Redis-cluster搭建

      1)配置redis.conf,其余配置默认,每一台服务器配置都一样,复制即可

    daemonize yes
    protected-mode no
    #bind 127.0.0.1
    cluster-enabled yes
    cluster-config-file nodes-6379.conf

      2)启动每一台redis

    [root@localhost bin]# ./redis-server redis.conf

      启动后,在/usr/local/myroom/redis-5.0.4/bin中会自动生成nodes-6379.conf文件

      3)创建集群,121、131、141是master节点,122、132、142是slave节点,如下图

    [root@localhost bin]# ./redis-cli --cluster create 192.168.43.121:6379 192.168.43.131:6379 192.168.43.141:6379 192.168.43.122:6379 192.168.43.132:6379 192.168.43.142:6379 --cluster-replicas 1

      

      可能出现问题a):

      [ERR] Node 192.168.43.121:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

      解决方法:登录每一个redis节点,清空数据库,即执行flushall命令

      可能出现问题b):

      [ERR] Not all 16384 slots are covered by nodes.

      解决方法:

      a)安装ruby

    [root@localhost bin]# yum clean all
    [root@localhost bin]# yum install ruby

      b)执行redis-5.0.4/src下的redis-trib.rb命令修复(每一台redis都执行)

    ./redis-trib.rb fix 192.168.43.121:6379

      实在不行,干脆删除相关文件,从新配置redis.conf搭建集群

    [root@localhost bin]# rm -rf dump.rdb- nodes-6379.conf redis.conf

      4)检查集群是否成功,如下图

    [root@localhost bin]# ./redis-cli -c

      

      四、基本操作

      1)常用命令

    #登录集群
    [root@localhost bin]# ./redis-cli -c
    #查看集群状态
    127.0.0.1:6379> cluster info
    #查看集群节点
    127.0.0.1:6379> cluster nodes
    #查看集群槽数
    127.0.0.1:6379> cluster slots
    #删除从节点 .
    /redis-cli --cluster del-node ip:端口 节点id

    #删除主节点(需要先把该节点的槽移动到其它节点,以下步骤全是)
    ./redis-cli --cluster reshard ip:端口
    How many slots do you want to move (from 1 to 16384)?(输入要删除的节点的槽的个数,执行cluster nodes,结果相减)
    What is the receiving node ID?(输入要接收槽的节点的ID,即这些槽移动到哪个节点上去)
    Please enter all the source node IDs.
    Type 'all' to use all the nodes as source nodes for the hash slots.
    Type 'done' once you entered all the source nodes IDs.
    Source node #1:(要删除的节点的ID)
    Source node #2:done(输入done,表示结束)
    ...准备从要删除的节点移动槽...
    Do you want to proceed with the proposed reshard plan (yes/no)?(输入yes)
    ...从要删除的节点移动槽到接收的节点...
    ./redis-cli --cluster  del-node ip:端口 节点id(最后执行删除节点命令)

    #添加从节点(需要先删除相关文件,启动该节点,并清空数据)
    [root@localhost bin]# rm -f nodes-6379.conf dump.rdb(如果有相关配置或数据先清空)
    [root@localhost bin]# ./redis-server redis.conf
    [root@localhost bin]# ./redis-cli flushall

    redis-cli --cluster add-node ip:端口 主节点的ip:主节点的端口 --cluster-slave --cluster-master-id 主节点的id

    #添加主节点
    需要先删除相关文件,启动该节点,并清空数据,给主节点分配槽
    [root@localhost bin]# rm -f nodes-6379.conf dump.rdb(如果有相关配置或数据先清空)
    [root@localhost bin]# ./redis-server redis.conf
    [root@localhost bin]# ./redis-cli flushall
    redis-cli --cluster add-node 新主节点ip:新主节点端口 最后一个主节点ip:最后一个主节点端口 --cluster-master-id 最后一个主节点id
    redis-cli --cluster reshard host:port --cluster-from 第一个主节点id --cluster-to 新增的主节点id --cluster-slots 给它分配的槽数 --cluster-yes

    #删除无效节点(显示noaddr)
    127.0.0.1:6379> cluster forget 无效节点id

    #重新平均分配节点
    ./redis-cli --cluster rebalance 任一节点id:端口

      2)演示删除从节点

      

      

      3)演示删除主节点

      先把要删除的主节点的槽移除,放到其它节点上:

      

      

      执行删除节点命令:

      

      

      4)演示添加主节点

      先准备141节点:

      

      再添加主节点:

      

      

      最后为主节点分配槽:

      

      

      5)演示添加从节点

      先准备从节点:

      

      添加从节点

      

      

      五、测试

      1)集群启动后,宕掉一个主节点,其从节点会成为主节点,可以读到这组节点存储的数据

      2)宕掉一个主节点和它的从节点,集群状态不可用

      3)宕掉一组主从节点,只启动主节点后,集群状态可用;只启动从节点,不可用

      六、附脚本

      1)初始化集群,init.sh

    #!/bin/bash
    
    echo "重新初始化集群中..."
    
    ssh root@192.168.43.121 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-cli shutdown
    sed -i 's/cluster-enabled yes/cluster-enabled no/g' redis.conf
    rm -f dump.rdb nodes-6379.conf
    ./redis-server redis.conf
    ./redis-cli flushall
    ./redis-cli shutdown
    sed -i 's/cluster-enabled no/cluster-enabled yes/g' redis.conf
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.121节点初始化完毕!"
    
    ssh root@192.168.43.122 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-cli shutdown
    sed -i 's/cluster-enabled yes/cluster-enabled no/g' redis.conf
    rm -f dump.rdb nodes-6379.conf
    ./redis-server redis.conf
    ./redis-cli flushall
    ./redis-cli shutdown
    sed -i 's/cluster-enabled no/cluster-enabled yes/g' redis.conf
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.122节点初始化完毕!"
    
    ssh root@192.168.43.131 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-cli shutdown
    sed -i 's/cluster-enabled yes/cluster-enabled no/g' redis.conf
    rm -f dump.rdb nodes-6379.conf
    ./redis-server redis.conf
    ./redis-cli flushall
    ./redis-cli shutdown
    sed -i 's/cluster-enabled no/cluster-enabled yes/g' redis.conf
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.131节点初始化完毕!"
    
    ssh root@192.168.43.132 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-cli shutdown
    sed -i 's/cluster-enabled yes/cluster-enabled no/g' redis.conf
    rm -f dump.rdb nodes-6379.conf
    ./redis-server redis.conf
    ./redis-cli flushall
    ./redis-cli shutdown
    sed -i 's/cluster-enabled no/cluster-enabled yes/g' redis.conf
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.132节点初始化完毕!"
    
    ssh root@192.168.43.141 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-cli shutdown
    sed -i 's/cluster-enabled yes/cluster-enabled no/g' redis.conf
    rm -f dump.rdb nodes-6379.conf
    ./redis-server redis.conf
    ./redis-cli flushall
    ./redis-cli shutdown
    sed -i 's/cluster-enabled no/cluster-enabled yes/g' redis.conf
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.141节点初始化完毕!"
    
    ssh root@192.168.43.142 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-cli shutdown
    sed -i 's/cluster-enabled yes/cluster-enabled no/g' redis.conf
    rm -f dump.rdb nodes-6379.conf
    ./redis-server redis.conf
    ./redis-cli flushall
    ./redis-cli shutdown
    sed -i 's/cluster-enabled no/cluster-enabled yes/g' redis.conf
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.142节点初始化完毕!"
    
    cd /usr/local/myroom/redis-5.0.4/bin
    
    ./redis-cli --cluster create 192.168.43.121:6379 192.168.43.131:6379 192.168.43.141:6379 192.168.43.122:6379 192.168.43.132:6379 192.168.43.142:6379 --cluster-replicas 1 --cluster-yes
    echo "集群初始化完毕"
    View Code

      2)启动集群,start.sh

    #!/bin/bash
    
    ssh root@192.168.43.121 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.121 start!"
    
    ssh root@192.168.43.122 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.122 start!"
    
    ssh root@192.168.43.131 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.131 start!"
    
    ssh root@192.168.43.132 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.132 start!"
    
    ssh root@192.168.43.141 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.141 start!"
    
    ssh root@192.168.43.142 > /dev/null 2>&1 <<eof
    cd /usr/local/myroom/redis-5.0.4/bin
    ./redis-server redis.conf
    exit
    eof
    echo "192.168.43.142 start!"
    View Code

      

  • 相关阅读:
    计算机语言的学习之道
    单麦克风远场语音降噪解决方案
    ESP8266 SPI 开发之软件驱动代码分析
    ESP8266 SPI 开发之软硬基础分析
    蓝牙5.0协议及下载地址
    python中往json中添加文件的方法
    Python isinstance() 函数含义及用法解析
    从一线方案商的角度来看高通QCC3020芯片
    Ubuntu18.04声卡配置问题解决
    python 音频通道分离的源码实现
  • 原文地址:https://www.cnblogs.com/javasl/p/14704755.html
Copyright © 2020-2023  润新知