美文网首页REDIS
6.Redis Cluster动态增删结点

6.Redis Cluster动态增删结点

作者: Stone_説 | 来源:发表于2020-08-03 18:20 被阅读0次

    目录
    0.架构图
    1.Cluster介绍
    2.准备环境
    3.集群部署
    4.添加新结点
    5.删除节点

    0.架构图

    redis_cluster架构图.jpg

    1.Cluster介绍

    Redis分布式部署方案:
    1)客户端分区:由客户端程序决定key写分配和写入的redis node,但是需要客户端自己处理写入分配、高可用管理和故障转移等
    2)代理方案:基于三方软件实现 redis proxy,客户端先连接之代理层,由代理层实现 key 的写入分配,对客户端来说是有比较简单,但是对于集群管节点
      增减相对比较麻烦,而且代理本身也是单点和性能瓶颈。在哨兵sentinel机制中,可以解决redis高可用的问题,即当master故障后可以自动将slave提升
      为master 从而可以保证redis服务的正常使用,但是无法解决 redis 单机写入的瓶颈问题, 即单机的 redis写入性能受限于单机的内存大小、 并发数量、 
      网卡速率等因素,因此 redis 官方在 redis 3.0 版本之后推出了无中心架构的 redis cluster 机制, 在无中心的 redis 集群汇中,其每个节点保存
      当前节点数据和整个集群状态,每个节点都和其他所有节点连接, 特点如下:
    
    1.所有Redis节点使用(PING 机制)互联
    2.集群中某个节点的失效,是整个集群中超过半数的节点监测都失效才算真正的失效
    3.客户端不需要proxy即可直接连接redis,应用程序需要写全部的redis服务器 IP。
    4.redis cluster把所有的redis node映射到0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个reids node相当于redis
      并发扩展了多少倍。
    5.Redis cluster 预先分配 16384 个(slot)槽位,当需要在 redis 集群中写入一个 key -value 的时候,会使用 CRC16(key) mod16384之后的值,
      决定将 key 写入值哪一个槽位从而决定写入哪一个 Redis 节点上, 从而有效解决单机瓶颈。
    

    2.准备环境

    读写数据都在master上,slave既不能读,也不能写,只能同步数据
    3台从服务器的masterauth一定要开启,否则无法从主服务器同步数据

    # masterauth <master-password>
    
    [root@node10 etc]# vim /apps/redis/etc/redis.conf
    cluster-enabled yes
    cluster-config-file nodes-6379.conf
    这个参数必须开启
    [root@node10 ~]# redis-cli
    127.0.0.1:6379> keys *
    (error) NOAUTH Authentication required.
    127.0.0.1:6379> AUTH 123456
    OK
    127.0.0.1:6379> info
    # Cluster
    cluster_enabled:1
    127.0.0.1:6379> FLUSHALL
    OK
    127.0.0.1:6379> keys *
    (empty list or set)
    [root@node10 etc]# ss -ntlu 
    Netid  State      Recv-Q Send-Q Local Address:Port               Peer Address:Port                            
    tcp    LISTEN     0      128          *:16379                    *:*                  
    16379端口为集群通告端口
    

    打包redis文件,拷贝至其他主机*

    [root@node11 apps]# pwd
    /apps
    [root@node11 apps]# rm -rf redis/run/*
    [root@node11 apps]# rm -rf redis/data/*
    [root@node11 apps]# rm -rf redis/logs/*
    [root@node11 apps]# tar czvf redis.tar.gz redis/*
    

    配置ruby环境

    [root@node09 ~]# cd /usr/local/src/
    [root@node09 src]# pwd
    /usr/local/src
    [root@node09 src]# tar xvf ruby-2.5.5.tar.gz 
    [root@node09 src]# cd ruby-2.5.5
    [root@node09 src]# ./configure 
    [root@node09 src]# make -j 2
    [root@node09 src]# make install
    [root@node09 src]# cp /usr/local/src/redis-4.2.1/src/redis-trib.rb /usr/bin/
    [root@node09 src]# redis-trib.rb
    Traceback (most recent call last):
        2: from /usr/bin/redis-trib.rb:25:in `<main>'
        1: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- redis (LoadError)
    [root@node09 ruby-2.5.5]# ruby -v
    ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]
    [root@node09 src]# ruby -v
    [root@node09 src]# find / -name gem
    [root@node09 src]# exit  退出重新登录,使环境生效
    [root@node09 src]# gem -v
    2.7.6.2
    [root@node09 src]# gem install redis
    Fetching: redis-4.2.1.gem (100%)
    Successfully installed redis-4.2.1
    Parsing documentation for redis-4.2.1
    Installing ri documentation for redis-4.2.1
    Done installing documentation for redis after 1 seconds
    1 gem installed
    [root@node09 ~]# redis-trib.rb 
    Usage: redis-trib <command> <options> <arguments ...>
    
      create          host1:port1 ... hostN:portN
                      --replicas <arg>
      check           host:port
      info            host:port
      fix             host:port
                      --timeout <arg>
      reshard         host:port
                      --from <arg>
                      --to <arg>
                      --slots <arg>
                      --yes
                      --timeout <arg>
                      --pipeline <arg>
      rebalance       host:port
                      --weight <arg>
                      --auto-weights
                      --use-empty-masters
                      --timeout <arg>
                      --simulate
                      --pipeline <arg>
                      --threshold <arg>
      add-node        new_host:new_port existing_host:existing_port
                      --slave
                      --master-id <arg>
      del-node        host:port node_id
      set-timeout     host:port milliseconds
      call            host:port command arg arg .. arg
      import          host:port
                      --from <arg>
                      --copy
                      --replace
      help            (show this help)
    
    For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
    

    修改ruby密码

    [root@node09 src]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.2.1/lib/redis/client.rb 
    # frozen_string_literal: true
    class Redis
      class Client
        DEFAULTS = {
    ...
          password: 123456,
    ...
        }.freeze
    

    各个结点打开集群模式

    [root@node09 src]# vim /apps/redis/etc/redis.conf 
    cluster-enabled yes
    cluster-config-file nodes-6379.conf
    

    清空数据

    [root@node09 ~]# redis-cli
    127.0.0.1:6379> keys *
    1) "key1"
    2) "foo"
    127.0.0.1:6379> clear
    127.0.0.1:6379> FLUSHALL
    OK
    127.0.0.1:6379> keys *
    (empty list or set)
    

    3.集群部署

    [root@node09 ~]# redis-trib.rb create --replicas 1 192.168.177.138:6379 192.168.177.139:6379 192.168.177.140:6379 192.168.177.153:6379 192.168.177.154:6379 192.168.177.155:6379
    >>> Creating cluster
    >>> Performing hash slots allocation on 6 nodes...
    Using 3 masters:
    192.168.177.138:6379
    192.168.177.139:6379
    192.168.177.140:6379
    Adding replica 192.168.177.154:6379 to 192.168.177.138:6379
    Adding replica 192.168.177.155:6379 to 192.168.177.139:6379
    Adding replica 192.168.177.153:6379 to 192.168.177.140:6379
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:0-5460 (5461 slots) master
    M: 101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379
       slots:5461-10922 (5462 slots) master
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379
       slots:10923-16383 (5461 slots) master
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       replicates 101675a131338516959aca929291f1932a056dcb
    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.177.138:6379)
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       slots: (0 slots) slave
       replicates 101675a131338516959aca929291f1932a056dcb
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       slots: (0 slots) slave
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    M: 101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379
       slots:5461-10922 (5462 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.
    

    检测集群状态

    [root@node09 ~]# redis-trib.rb info 192.168.177.155:6379
    192.168.177.139:6379 (101675a1...) -> 0 keys | 5462 slots | 1 slaves.
    192.168.177.138:6379 (4d8bfb20...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.177.140:6379 (f8f90ba2...) -> 0 keys | 5461 slots | 1 slaves.
    [OK] 0 keys in 3 masters.
    0.00 keys per slot on average.
    [root@node09 ~]# redis-trib.rb check 192.168.177.155:6379
    >>> Performing Cluster Check (using node 192.168.177.155:6379)
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       slots: (0 slots) slave
       replicates 101675a131338516959aca929291f1932a056dcb
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       slots: (0 slots) slave
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    M: 101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140: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.
    

    从服务器未配置密码

    [root@node09 apps]# redis-cli -h 192.168.177.153 -p 6379 -a 123456
    Warning: Using a password with '-a' option on the command line interface may not be safe.
    192.168.177.154:6379> CONFIG SET masterauth 123456
    OK
    192.168.177.154:6379> exit
    [root@node09 apps]# redis-cli -h 192.168.177.154 -p 6379 -a 123456
    Warning: Using a password with '-a' option on the command line interface may not be safe.
    192.168.177.154:6379> CONFIG SET masterauth 123456
    OK
    192.168.177.154:6379> exit
    [root@node09 apps]# redis-cli -h 192.168.177.155 -p 6379 -a 123456
    Warning: Using a password with '-a' option on the command line interface may not be safe.
    192.168.177.155:6379> CONFIG SET masterauth 123456
    OK
    192.168.177.155:6379> exit
    

    验证状态

    [root@node03 apps]# redis-cli
    127.0.0.1:6379> info
    NOAUTH Authentication required.
    127.0.0.1:6379> AUTH 123456
    OK
    127.0.0.1:6379> info
    # Replication
    role:slave
    master_host:192.168.177.140
    master_port:6379
    master_link_status:up
    
    # Cluster
    cluster_enabled:1
    
    [root@node04 apps]# redis-cli
    127.0.0.1:6379> AUTH 123456
    OK
    127.0.0.1:6379> info
    # Replication
    role:slave
    master_host:192.168.177.138
    master_port:6379
    master_link_status:up
    
    # Cluster
    cluster_enabled:1
    
    [root@node05 apps]# redis-cli
    127.0.0.1:6379> AUTH 123456
    OK
    127.0.0.1:6379> info
    # Replication
    role:slave
    master_host:192.168.177.139
    master_port:6379
    master_link_status:up
    
    # Cluster
    cluster_enabled:1
    

    可监控状态

    127.0.0.1:6379> 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:2
    cluster_stats_messages_ping_sent:1156
    cluster_stats_messages_pong_sent:1131
    cluster_stats_messages_meet_sent:5
    cluster_stats_messages_sent:2292
    cluster_stats_messages_ping_received:1130
    cluster_stats_messages_pong_received:1161
    cluster_stats_messages_meet_received:1
    cluster_stats_messages_received:2292
    
    127.0.0.1:6379> CLUSTER nodes
    771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379@16379 slave f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 0 1596030885000 4 connected
    1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379@16379 slave 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 0 1596030885000 5 connected
    101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379@16379 master - 0 1596030885475 2 connected 5461-10922
    4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379@16379 master - 0 1596030886481 1 connected 0-5460
    f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379@16379 master - 0 1596030884461 3 connected 10923-16383
    4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379@16379 myself,slave 101675a131338516959aca929291f1932a056dcb 0 1596030884000 6 connected
    
    主从关系对应图.jpg

    4.添加新结点

    [root@node09 ~]# redis-trib.rb info 192.168.177.155:6379
    192.168.177.139:6379 (101675a1...) -> 0 keys | 5462 slots | 1 slaves.
    192.168.177.138:6379 (4d8bfb20...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.177.140:6379 (f8f90ba2...) -> 0 keys | 5461 slots | 1 slaves.
    [OK] 0 keys in 3 masters.
    0.00 keys per slot on average.
    [root@node09 ~]# redis-trib.rb add-node 192.168.177.158:6379 192.168.177.139:6379
    >>> Adding node 192.168.177.158:6379 to cluster 192.168.177.139:6379
    >>> Performing Cluster Check (using node 192.168.177.139:6379)
    M: 101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       slots: (0 slots) slave
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       slots: (0 slots) slave
       replicates 101675a131338516959aca929291f1932a056dcb
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:0-5460 (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.
    >>> Send CLUSTER MEET to node 192.168.177.158:6379 to make it join the cluster.
    [OK] New node added correctly.
    

    新添加结点状态检查

    >>> Send CLUSTER MEET to node 192.168.177.158:6379 to make it join the cluster.
    [OK] New node added correctly.
    [root@node09 ~]# redis-trib.rb check 192.168.177.155:6379
    >>> Performing Cluster Check (using node 192.168.177.155:6379)
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       slots: (0 slots) slave
       replicates 101675a131338516959aca929291f1932a056dcb
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       slots: (0 slots) slave
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    M: 101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    M: fdbcd12a56120119ce2cab1d208020859863a22b 192.168.177.158:6379
       slots: (0 slots) master
       0 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    [root@node09 ~]# redis-trib.rb info 192.168.177.155:6379
    192.168.177.139:6379 (101675a1...) -> 0 keys | 5462 slots | 1 slaves.
    192.168.177.138:6379 (4d8bfb20...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.177.140:6379 (f8f90ba2...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.177.158:6379 (fdbcd12a...) -> 0 keys | 0 slots | 0 slaves.
    [OK] 0 keys in 4 masters.
    0.00 keys per slot on average.
    

    新master上添加SALVE

    [root@node18 ~]# cd /apps/
    [root@node18 apps]# ll
    drwxr-xr-x  7 root root      63 Jul 29 22:31 redis
    -rw-r--r--  1 root root 7825986 Jul 29 22:31 redis.tar.gz
    [root@node18 apps]# mkdir redis-6380
    [root@node18 apps]# cp redis.tar.gz redis-6380/
    [root@node18 apps]# cd redis-6380/
    [root@node18 redis-6380]# ll
    total 7644
    -rw-r--r-- 1 root root 7825986 Jul 29 22:44 redis.tar.gz
    [root@node18 redis-6380]# tar xvf redis.tar.gz 
    [root@node18 redis-6380]# ll
    total 7644
    drwxr-xr-x 7 root root      63 Jul 29 22:44 redis
    -rw-r--r-- 1 root root 7825986 Jul 29 22:44 redis.tar.gz
    [root@node18 redis-6380]# cd redis
    [root@node18 redis]# cd etc/
    [root@node18 etc]# vim redis.conf 
    [root@node18 etc]# redis-server /apps/redis-6380/redis/etc/redis.conf 将配置文件中6379端口改为6380 
    [root@node18 etc]# ss -ntlu
    Netid  State      Recv-Q Send-Q Local Address:Port               Peer Address:Port                            
    tcp    LISTEN     0      128          *:6379                     *:*                  
    tcp    LISTEN     0      128          *:6380                     *:*                           
    tcp    LISTEN     0      128          *:16379                    *:*                  
    tcp    LISTEN     0      128          *:16380                    *:*                  
    

    添加SLAVE机器

    [root@node09 ~]# redis-trib.rb add-node 192.168.177.158:6380 192.168.177.139:6379
    >>> Adding node 192.168.177.158:6380 to cluster 192.168.177.139:6379
    >>> Performing Cluster Check (using node 192.168.177.139:6379)
    M: 101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       slots: (0 slots) slave
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    M: fdbcd12a56120119ce2cab1d208020859863a22b 192.168.177.158:6379
       slots: (0 slots) master
       0 additional replica(s)
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       slots: (0 slots) slave
       replicates 101675a131338516959aca929291f1932a056dcb
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:0-5460 (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.
    >>> Send CLUSTER MEET to node 192.168.177.158:6380 to make it join the cluster.
    [OK] New node added correctly.
    
    [root@node09 ~]# redis-trib.rb info 192.168.177.155:6379
    192.168.177.158:6380 (e82c4882...) -> 0 keys | 0 slots | 0 slaves.
    192.168.177.139:6379 (101675a1...) -> 0 keys | 5462 slots | 1 slaves.
    192.168.177.138:6379 (4d8bfb20...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.177.140:6379 (f8f90ba2...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.177.158:6379 (fdbcd12a...) -> 0 keys | 0 slots | 0 slaves.
    [OK] 0 keys in 5 masters.
    0.00 keys per slot on average.
    

    设置主从关系

    [root@node09 apps]# redis-cli
    127.0.0.1:6379> AUTH 123456
    OK
    127.0.0.1:6379> clear
    127.0.0.1:6379> CLUSTER nodes
    e82c4882c4aa2ad073a6cb7a65d52cfef13f232f 192.168.177.158:6380@16380 master - 0 1596034338058 7 connected
    fdbcd12a56120119ce2cab1d208020859863a22b 192.168.177.158:6379@16379 master - 0 1596034337555 0 connected
    127.0.0.1:6379>
    
    [root@node09 apps]# redis-cli -h 192.168.177.158 -p 6380
    192.168.177.158:6380> info
    NOAUTH Authentication required.
    192.168.177.158:6380> AUTH 123456
    OK
    192.168.177.158:6380> info
    # Replication
    role:master
    
    # Cluster
    cluster_enabled:1
    
    192.168.177.158:6380> CLUSTER REPLICATE fdbcd12a56120119ce2cab1d208020859863a22b
    OK
    192.168.177.158:6380> CLUSTER nodes
    fdbcd12a56120119ce2cab1d208020859863a22b 192.168.177.158:6379@16379 master - 0 1596034722158 0 connected
    e82c4882c4aa2ad073a6cb7a65d52cfef13f232f 192.168.177.158:6380@16380 myself,slave fdbcd12a56120119ce2cab1d208020859863a22b 0 1596034724000 7 connected
    

    验证配置,主从配置成功

    [root@node09 ~]# redis-trib.rb info 192.168.177.155:6379
    192.168.177.139:6379 (101675a1...) -> 0 keys | 5462 slots | 1 slaves.
    192.168.177.138:6379 (4d8bfb20...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.177.140:6379 (f8f90ba2...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.177.158:6379 (fdbcd12a...) -> 0 keys | 0 slots | 1 slaves.
    [OK] 0 keys in 4 masters.
    0.00 keys per slot on average
    

    重新分配槽位

    [root@node09 ~]# redis-trib.rb reshard 192.168.177.155:6379
    >>> Performing Cluster Check (using node 192.168.177.155:6379)
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       slots: (0 slots) slave
       replicates 101675a131338516959aca929291f1932a056dcb
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       slots: (0 slots) slave
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    S: e82c4882c4aa2ad073a6cb7a65d52cfef13f232f 192.168.177.158:6380
       slots: (0 slots) slave
       replicates fdbcd12a56120119ce2cab1d208020859863a22b
    M: 101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    M: fdbcd12a56120119ce2cab1d208020859863a22b 192.168.177.158:6379
       slots: (0 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.
    How many slots do you want to move (from 1 to 16384)? 4096
    What is the receiving node ID? fdbcd12a56120119ce2cab1d208020859863a22b
    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
    
    

    分配槽位时,不能有数据,如果有的话,需要用fix命令修复以下,再分配

    [root@node09 ~]# redis-trib.rb info 192.168.177.155:6379
    192.168.177.139:6379 (101675a1...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.177.138:6379 (4d8bfb20...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.177.140:6379 (f8f90ba2...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.177.158:6379 (fdbcd12a...) -> 0 keys | 4096 slots | 1 slaves.
    [OK] 0 keys in 4 masters.
    0.00 keys per slot on average.
    

    横向扩容会提前通知,保证不能有数据,这个非常麻烦,扩容之前会备份,rdb文件然后再拷贝回去

    扩容后主从关系对应图.jpg

    5.删除节点

    移除槽位

    [root@node09 ~]# redis-trib.rb reshard 192.168.177.155:6379
    >>> Performing Cluster Check (using node 192.168.177.155:6379)
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       slots: (0 slots) slave
       replicates 101675a131338516959aca929291f1932a056dcb
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       slots: (0 slots) slave
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    S: e82c4882c4aa2ad073a6cb7a65d52cfef13f232f 192.168.177.158:6380
       slots: (0 slots) slave
       replicates fdbcd12a56120119ce2cab1d208020859863a22b
    M: 101675a131338516959aca929291f1932a056dcb 192.168.177.139:6379
       slots:6827-10922 (4096 slots) master
       1 additional replica(s)
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:1365-5460 (4096 slots) master
       1 additional replica(s)
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379
       slots:12288-16383 (4096 slots) master
       1 additional replica(s)
    M: fdbcd12a56120119ce2cab1d208020859863a22b 192.168.177.158:6379
       slots:0-1364,5461-6826,10923-12287 (4096 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.
    How many slots do you want to move (from 1 to 16384)? 4096
    What is the receiving node ID? f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    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:101675a131338516959aca929291f1932a056dcb
    Source node #2:done
    
    [root@node09 ~]# redis-trib.rb info 192.168.177.138:6379
    192.168.177.138:6379 (4d8bfb20...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.177.140:6379 (f8f90ba2...) -> 0 keys | 8192 slots | 2 slaves.
    192.168.177.158:6379 (fdbcd12a...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.177.139:6379 (101675a1...) -> 0 keys | 0 slots | 0 slaves.
    [OK] 0 keys in 4 masters.
    0.00 keys per slot on average.
    
    [root@node09 ~]# redis-trib.rb del-node 192.168.177.138:6379 101675a131338516959aca929291f1932a056dcb
    >>> Removing node 101675a131338516959aca929291f1932a056dcb from cluster 192.168.177.138:6379
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> SHUTDOWN the node.
    

    主节点已被删除,最重要的就是将槽位移动到其他主服务器,而且必须没有数据

    [root@node09 ~]# redis-trib.rb check 192.168.177.138:6379
    >>> Performing Cluster Check (using node 192.168.177.138:6379)
    M: 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7 192.168.177.138:6379
       slots:1365-5460 (4096 slots) master
       1 additional replica(s)
    S: e82c4882c4aa2ad073a6cb7a65d52cfef13f232f 192.168.177.158:6380
       slots: (0 slots) slave
       replicates fdbcd12a56120119ce2cab1d208020859863a22b
    M: f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99 192.168.177.140:6379
       slots:6827-10922,12288-16383 (8192 slots) master
       2 additional replica(s)
    S: 771400140a28c361a9d4ef0a487359ea28fb4fe1 192.168.177.153:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    S: 4f50979e0a2ad72db9c50117f73cb68bed699251 192.168.177.155:6379
       slots: (0 slots) slave
       replicates f8f90ba2e4552c36fcfa3ba9bf60d3997b685d99
    S: 1bd02a1517d5ad14dd5fd056495e0b1524cc502c 192.168.177.154:6379
       slots: (0 slots) slave
       replicates 4d8bfb20aea0b4b21b0d97383ca9c7794f94a0d7
    M: fdbcd12a56120119ce2cab1d208020859863a22b 192.168.177.158:6379
       slots:0-1364,5461-6826,10923-12287 (4096 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.
    
    

    相关文章

      网友评论

        本文标题:6.Redis Cluster动态增删结点

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