• redis集群


    redis-cluster把所有的物理节点映射到[0-16383]slot上(哈希槽),cluster 负责维护Redis 集群中内置了 16384 个哈希槽,当需要在Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。(并不是每个节点数据都是相同的)
      
      
    5.x一下版本需要安装

    rubygems是gem文件的管理工具,安装gem需要用到它,如gem install xxx.gem,之所以安装这个是因为,redis集群管理工具redis-trib.rb依赖ruby环境,ruby是一种面向对象的语言。redis-trib.rb在redis-3.2.3.tar.gz源码包中。

    这里使用一台机器做测试,只是在一台机器上启动多个redis进程

    Redis集群运行最起码要3个主节点。

      一个redis集群的搭建,最少需要6个节点,构成3组服务节点;每组服务节点包括两个节点(Master-Slave)
      

    • 配置节点

    建立配置文件,首先需要创建一个配置文件的模版文件

    [root@localhost bin]# mkdir node_{8000..8005}
    
    root@localhost bin]# cp /opt/redis-6.2.2/redis.conf  node_8000/
    
    [root@localhost bin]# vim node_8000/redis.conf
    
    
    • 修改配置文件

    其他配置文件也需要修改

    [root@localhost bin]# vim node_8000/redis.conf
    bind 192.168.248.130
    protected-mode yes
    port 8000
    dir /usr/local/bin/node_8000
    daemonize yes
    pidfile /var/run/redis_8000.pid
    cluster-enabled yes
    cluster-config-file nodes-8000.conf
    cluster-node-timeout 5000
    
    [root@localhost bin]# sed 's/8000/8001/g' node_8000/redis.conf >> node_8001/redis.conf
    [root@localhost bin]# sed 's/8000/8002/g' node_8000/redis.conf >> node_8002/redis.conf
    [root@localhost bin]# sed 's/8000/8003/g' node_8000/redis.conf >> node_8003/redis.conf
    [root@localhost bin]# sed 's/8000/8004/g' node_8000/redis.conf >> node_8004/redis.conf
    [root@localhost bin]# sed 's/8000/8005/g' node_8000/redis.conf >> node_8005/redis.conf
    
    • 启动所有服务
    [root@localhost bin]#  for i  in node_800{0..5};do redis-server  $i/redis.conf;done
    
    • 创建集群

    执行以下命令会有提示,输入yes

    会由一个警告,因为是在一台机器上部署的

    [root@localhost bin]# redis-cli --cluster create 192.168.248.130:8000 192.168.248.130:8001 192.168.248.130:8002 192.168.248.130:8003 192.168.248.130:8004 192.168.248.130:8005  --cluster-replicas 1
    
    • 这里登录多了一个-c的参数,集群登录直接任意一个节点一个端口即可
    [root@localhost bin]# redis-cli  -h 192.168.248.130 -p 8000 -c
    
    
    • 查看集群信息
    192.168.248.130:8000> CLUSTER INFO
    cluster_state:ok
    cluster_slots_assigned:16384
    cluster_slots_ok:16384
    cluster_slots_pfail:0
    cluster_slots_fail:0
    cluster_known_nodes:6
    cluster_size:3
    cluster_current_epoch:6
    cluster_my_epoch:1
    cluster_stats_messages_ping_sent:238
    cluster_stats_messages_pong_sent:245
    cluster_stats_messages_sent:483
    cluster_stats_messages_ping_received:240
    cluster_stats_messages_pong_received:238
    cluster_stats_messages_meet_received:5
    cluster_stats_messages_received:483
    
    • LUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息
    192.168.248.130:8000> CLUSTER NODES
    0a68bf455b5ab28a714a38a5b8379a62d5cd390f 192.168.248.130:8001@18001 master - 0 1619330365529 2 connected 5461-10922
    571a1ce98950dfc3249c660775473d22af4eedaf 192.168.248.130:8000@18000 myself,master - 0 1619330365000 1 connected 0-5460
    127b6e4cf036681e914bfc4c7ce7718ab9545e6c 192.168.248.130:8005@18005 slave 571a1ce98950dfc3249c660775473d22af4eedaf 0 1619330364000 1 connected
    2ed5d4ca577c5f85b598cfcb4368fa5144fe125b 192.168.248.130:8004@18004 slave 0f556e93bffc39c43d49337f145ba266f0425c47 0 1619330365529 3 connected
    0f556e93bffc39c43d49337f145ba266f0425c47 192.168.248.130:8002@18002 master - 0 1619330365529 3 connected 10923-16383
    6f5425e2ce97f4214e1d5be3f4ea8a171c2ad682 192.168.248.130:8003@18003 slave 0a68bf455b5ab28a714a38a5b8379a62d5cd390f 0 1619330364816 2 connected
    
    
    • 在节点6379上新建一个值,在其它节点查看是否同步。
    192.168.248.130:8000> set new 1
    set key value
    -> Redirected to slot [12539] located at 192.168.248.130:8002
    OK
    
    
    #到其他节点查看
    192.168.248.130:8002> keys *
    1) "key"
    192.168.248.130:8002> get key
    "value"
    
    192.168.248.130:8004> keys *
    1) "key"
    
    192.168.248.130:8000> keys *
    (empty array)
    

    redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。(并不是每个节点数据都是相同的)

    • 新增节点

    和其他节点一样,创建redis.conf文件

    [root@localhost bin]# mkdir node_{8006..8007}
    [root@localhost bin]# sed 's/8000/8006/g' node_8000/redis.conf >> node_8006/redis.conf
    [root@localhost bin]# sed 's/8000/8007/g' node_8000/redis.conf >> node_8007/redis.conf
    

    启动

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

    添加主节点

    其中192.168.248.130:8006是新增节点,192.168.248.130:8000是集群中任意一个节点

    [root@localhost bin]# redis-cli --cluster add-node  192.168.248.130:8006  192.168.248.130:8000
    >>> Adding node 192.168.248.130:8006 to cluster 192.168.248.130:8000
    >>> Performing Cluster Check (using node 192.168.248.130:8000)
    M: 571a1ce98950dfc3249c660775473d22af4eedaf 192.168.248.130:8000
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: 0a68bf455b5ab28a714a38a5b8379a62d5cd390f 192.168.248.130:8001
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 127b6e4cf036681e914bfc4c7ce7718ab9545e6c 192.168.248.130:8005
       slots: (0 slots) slave
       replicates 571a1ce98950dfc3249c660775473d22af4eedaf
    S: 2ed5d4ca577c5f85b598cfcb4368fa5144fe125b 192.168.248.130:8004
       slots: (0 slots) slave
       replicates 0f556e93bffc39c43d49337f145ba266f0425c47
    M: 0f556e93bffc39c43d49337f145ba266f0425c47 192.168.248.130:8002
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: 6f5425e2ce97f4214e1d5be3f4ea8a171c2ad682 192.168.248.130:8003
       slots: (0 slots) slave
       replicates 0a68bf455b5ab28a714a38a5b8379a62d5cd390f
    [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.248.130:8006 to make it join the cluster.
    [OK] New node added correctly.
    [root@localhost bin]# 
    
    

    分配slot,新增的节点是没有slots,需要给新添加的节点分配。

    [root@localhost bin]# redis-cli --cluster reshard   192.168.248.130:8006
    ......................
    ......................
    How many slots do you want to move (from 1 to 16384)? 1000 //设置slot数1000  
    What is the receiving node ID? 03ccad2ba5dd1e062464bc759000441fafb63f2 //新节点node 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:all //表示全部节点重新洗牌  
    Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认重新分 
    ..........................
    

    检查集群情况

    可以看出,重新洗牌之后并不是按照顺序划分的slot,且现在有4个主节点。

    [root@localhost bin]# redis-cli --cluster check 192.168.248.130:8000
    192.168.248.130:8000 (571a1ce9...) -> 0 keys | 5128 slots | 1 slaves.
    192.168.248.130:8006 (2c2ff818...) -> 0 keys | 1000 slots | 0 slaves.
    192.168.248.130:8001 (0a68bf45...) -> 0 keys | 5128 slots | 1 slaves.
    192.168.248.130:8002 (0f556e93...) -> 1 keys | 5128 slots | 1 slaves.
    [OK] 1 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.248.130:8000)
    M: 571a1ce98950dfc3249c660775473d22af4eedaf 192.168.248.130:8000
       slots:[333-5460] (5128 slots) master
       1 additional replica(s)
    M: 2c2ff8188a40253c51084268d0a620d0538b9cc8 192.168.248.130:8006
       slots:[0-332],[5461-5794],[10923-11255] (1000 slots) master
    M: 0a68bf455b5ab28a714a38a5b8379a62d5cd390f 192.168.248.130:8001
       slots:[5795-10922] (5128 slots) master
       1 additional replica(s)
    S: 127b6e4cf036681e914bfc4c7ce7718ab9545e6c 192.168.248.130:8005
       slots: (0 slots) slave
       replicates 571a1ce98950dfc3249c660775473d22af4eedaf
    S: 2ed5d4ca577c5f85b598cfcb4368fa5144fe125b 192.168.248.130:8004
       slots: (0 slots) slave
       replicates 0f556e93bffc39c43d49337f145ba266f0425c47
    M: 0f556e93bffc39c43d49337f145ba266f0425c47 192.168.248.130:8002
       slots:[11256-16383] (5128 slots) master
       1 additional replica(s)
    S: 6f5425e2ce97f4214e1d5be3f4ea8a171c2ad682 192.168.248.130:8003
       slots: (0 slots) slave
       replicates 0a68bf455b5ab28a714a38a5b8379a62d5cd390f
    

    新增节点将8007作为8006的从节点

    [root@localhost bin]# redis-cli --cluster add-node --cluster-slave --cluster-master-id 2c2ff8188a40253c51084268d0a620d0538b9cc8 192.168.248.130:8007 192.168.248.130:8006
    

    删除节点

    删除从节点

    [root@localhost bin]# redis-cli --cluster del-node 192.168.248.130:8007 7324249d9aca62b0470bbaf5a9e82ce5f82898a7
    >>> Removing node 7324249d9aca62b0470bbaf5a9e82ce5f82898a7 from cluster 192.168.248.130:8007
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> Sending CLUSTER RESET SOFT to the deleted node.
    

    删除主节点

    • 如果主节点有从节点,将从节点转移到其他主节点

    • 如果主节点有slot,去掉分配的slot,然后在删除主节点

    [root@localhost bin]# redis-cli --cluster  reshard  192.168.248.130:8000
    .....................
    How many slots do you want to move (from 1 to 16384)?   //被删除master的所有slot数量
    What is the receiving node ID?  //接收slots的master
    .................
    Source node #1:  //被删除的master
    Source node #2:done  //输入done表示完成
    .................
    Do you want to proceed with the proposed reshard plan (yes/no)? yes //取消slot后,reshard
    
    
    
    # 然后删除主节点
    [root@localhost bin]# redis-cli --cluster del-node 192.168.248.130:8000  571a1ce98950dfc3249c660775473d22af4eedaf
    >>> Removing node 571a1ce98950dfc3249c660775473d22af4eedaf from cluster 192.168.248.130:8000
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> Sending CLUSTER RESET SOFT to the deleted node.
    
  • 相关阅读:
    二进制,八进制,十六进制,十进制之间的换算
    14简化路径(71)
    13字符串解码(394)
    12 反转每对括号间的子串(1190)
    11 使括号有效的最少添加(921)
    10 K 个一组翻转链表(25)
    9 从链表中删去总和值为零的连续节点(1171)
    8 链表中的下一个更大节点(1019)
    7两两交换链表中的节点(24)
    6 奇偶链表(
  • 原文地址:https://www.cnblogs.com/diqiyao/p/14701055.html
Copyright © 2020-2023  润新知