美文网首页NoSQL数据库
添加服务器到Redis集群

添加服务器到Redis集群

作者: 技术老男孩 | 来源:发表于2023-03-10 10:05 被阅读0次

    一、环境准备:

    角色 Ip地址 主机名 端口
    Redis服务器(master) Host58 192.168.88.58 6379
    Redis服务器(slave) Host59 192.168.88.59 6379

    二、添加流程思路:

    1. 准备要添加redis服务器并做好集群配置
    2. 配置管理主机mgm57
      添加Master服务器到集群
      添加slave服务器到集群

    第一步:准备要添加redis服务器并做好集群配置

    • 准备redis服务器Host58(作为master加入到集群)
    [root@host58 ~]# yum -y install gcc 
    [root@host58 ~]# tar -xf redis-4.0.8.tar.gz 
    [root@host58 ~]# cd redis-4.0.8/
    [root@host58 ~]# make && make install
    [root@host58 ~]# ./utils/install_server.sh  遇到提示就回车
    
    # 配置58启用集群功能
    [root@host58 ~]# /etc/init.d/redis_6379  stop
    [root@host58 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf 
    [root@host58 ~]# vim /etc/redis/6379.conf 
    bind 192.168.88.58 
    port 6379
    cluster-enabled yes #启用集群功能
    cluster-config-file nodes-6379.conf 保存集群信息的配置文件
    cluster-node-timeout 5000  集群中主机的连接超时时间
               
    [root@host58 ~]# /etc/init.d/redis_6379  restart
    [root@host58 ~]# netstat  -utnlp  | grep redis-server
    tcp   0      0 192.168.88.58:16379   0.0.0.0:*   LISTEN      4249/redis-server 1 
    tcp  0    0 192.168.88.58:6379      0.0.0.0:*   LISTEN      4249/redis-server 1 
    
    • 准备redis服务器Host59(作为slave加入到集群)
    [root@host59 ~]# yum -y install gcc 
    [root@host59 ~]# tar -xf redis-4.0.8.tar.gz 
    [root@host59 ~]# cd redis-4.0.8/
    [root@host59 ~]# make && make install
    [root@host59 ~]# ./utils/install_server.sh  遇到提示就回车
    
    # 在host59 主机运行redis服务 且启用了集群功能
    [root@host59 ~]# /etc/init.d/redis_6379  stop
    [root@host59 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf 
    [root@host59 ~]# vim /etc/redis/6379.conf 
    bind 192.168.88.59 
    port 6379
    cluster-enabled yes # 启用集群功能
    cluster-config-file nodes-6379.conf # 保存集群信息的配置文件
    cluster-node-timeout 5000  # 集群中主机的连接超时时间
    # 重启服务生效配置
    [root@host59 ~]# /etc/init.d/redis_6379  restart
    [root@host59 ~]# netstat  -utnlp  | grep redis-server
    tcp        0      0 192.168.88.59:16379      0.0.0.0:*               LISTEN      4249/redis-server 1 
    tcp        0      0 192.168.88.59:6379      0.0.0.0:*               LISTEN      4249/redis-server 1 
    

    第二步:配置管理主机mgm57

    • 添加58服务器到集群(作为Master加入)
    [root@mgm57 ~]# redis-trib.rb add-node 192.168.88.58:6379 192.168.88.51:6379
    .....
    .....
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    >>> Send CLUSTER MEET to node 192.168.88.58:6379 to make it join the cluster.
    [OK] New node added correctly.  添加成功的提示
    
    # 新添加的master角色主机没有hash slots
    [root@mgm57 ~]# redis-trib.rb info  192.168.88.51:6379
    192.168.88.51:6379 (0eb3b7aa...) -> 2 keys | 5461 slots | 1 slaves.
    192.168.88.53:6379 (f2c1bdb7...) -> 1 keys | 5461 slots | 1 slaves.
    192.168.88.52:6379 (a9cb8ccd...) -> 3 keys | 5462 slots | 1 slaves.
    192.168.88.58:6379 (87cc1c12...) -> 0 keys | 0 slots | 0 slaves.   零个hash slots 
    [OK] 6 keys in 4 masters.
    0.00 keys per slot on average.
    
    • 分配hast slots
    • master角色的服务器没有hast slots得不到存储数据机会
    [root@mgm57 ~]# redis-trib.rb reshard  192.168.88.56:6379
    # 第1个问题 移除多少个hast slots
    How many slots do you want to move (from 1 to 16384)? 4096
    # 第2个问题 把4096个槽给哪台主数据库服务器 (host58主机的id) 
    What is the receiving node ID?87cc1c128166e08a16cc294758611453bbc71437 
    # 第3个问题 提供4096个槽 主机的id (all 表示当前所有主服务器一起提供4096个槽给host58)
    Source node #1:all
    # 第4个问题 确认前3步的配置 yes 同意 no  退出
    Do you want to proceed with the proposed reshard plan (yes/no)? yes  
    
    • 查看集群信息 (查看到多新的主服务器且有hast slots 为成功)
    [root@mgm57 ~]# redis-trib.rb info  192.168.88.56:6379
    192.168.88.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.88.51:6379 (0eb3b7aa...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.88.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
    192.168.88.58:6379 (87cc1c12...) -> 2 keys | 4096 slots | 0 slaves.
    [OK] 6 keys in 4 masters.
    0.00 keys per slot on average.
    [root@mgm57 ~]# 
                            #具体查看host58 占用4096个槽的范围
    [root@mgm57 ~]# redis-trib.rb check  192.168.88.56:6379
    ....
    ....                        
    M: 87cc1c128166e08a16cc294758611453bbc71437 192.168.88.58:6379
       slots:0-1364,5461-6826,10923-12287 (4096 slots) master
       0 additional replica(s)  
    
    • 添加59服务器到集群(作为Slave加入)
    • 添加的从服务器会自动做从服务器个数最少的 master服务器的从服务器
    # --slave:作为从服务器被添加到集群
    [root@mgm57 ~]# redis-trib.rb add-node --slave 192.168.88.59:6379  192.168.88.56:6379
    ....
    ....
    [OK] All 16384 slots covered.
    Automatically selected master 192.168.88.58:6379
    >>> Send CLUSTER MEET to node 192.168.88.59:6379 to make it join the cluster.
    Waiting for the cluster to join.
    >>> Configure node as replica of 192.168.88.58:6379.
    [OK] New node added correctly.
    
    [root@mgm57 ~]# redis-trib.rb info  192.168.88.56:6379
    192.168.88.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.88.51:6379 (0eb3b7aa...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.88.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
    192.168.88.58:6379 (87cc1c12...) -> 2 keys | 4096 slots | 1 slaves.  host58主机的从
    [OK] 6 keys in 4 masters.
    0.00 keys per slot on average.
    
    • 查看集群详细信息
    [root@mgm57 ~]# redis-trib.rb check  192.168.88.56:6379
    ....
    .....
    S: d50aa7c1acebe69af0834f1838c8b17b2348472e 192.168.88.59:6379
       slots: (0 slots) slave
       replicates 87cc1c128166e08a16cc294758611453bbc71437    主服务器的id 
    
    # 连接host59 查看数据     
    [root@host56 ~]# redis-cli  -c  -h 192.168.88.59 -p 6379  
    192.168.88.59:6379> keys *
    1) "name"
    2) "age"
    192.168.88.59:6379> 
    

    相关文章

      网友评论

        本文标题:添加服务器到Redis集群

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