1、RDB和AOF的优缺点

  1. RDB模式的优点:
    • RDB模式是直接以快照形式把内存中的数据保存到一个文件中,定时定点,非常适合用来备份,可以备份多个时间点的数据,比如,可以在最近的24小时内,每小时备份一次RDB文件,并且在每个月的每一天,也备份一个ROB文件。这样的话,即使遇上问题,也可以随时将数据集还原到不同的版本。
    • RDB在有大量数据的情况下,比如说几个G的数据,恢复的速度比AOF的快
    • RDB可以最大化redis的性能,就是在保存RDB文件的时候,父进程会fork出一个子进程,在父进程进行备份,保存文件的时候,这个子进程会接手父进程之前的工作,父进程能专心的保存RDB文件
  2. RDB模式的缺点:
    • RDB保存文件会有一个时间间隔,也就是说不能够实时保存数据,当服务器突然宕机,那么就会丢失从宕机的那一秒到上次保存数据的时间段的数据
    • RDB文件数据过大可能会造成服务器的阻塞,停止客户端请求
  3. AOF模式的优点
    • 实时保存,每一秒或者每一次操作都保存一次
    • AOF文件有序的保存了对数据库的所有写入操作,这些写入操作以redis协议的格式保存,所以AOF文件的内容非常容易让人读懂,对文件进行分析(parse)也很轻松。
    • AOF时增量操作,每一次操作都会追加到AOF文件中
  4. AOF模式的缺点:
    • 在同样的数据集下,AOF文件通常都会大于RDB文件
    • 根据fsync策略不同,AOF速度可能会慢于RDB
    • bug出现的可能性更多
    • AOF恢复大量数据时比RDB的速度慢

      2、master和slave同步过程

      至少需要两台主机来实现redis的主从复制
  • 10.0.1.17 redis master
  • 10.0.1.27 redis slave
  1. 默认redis 状态为master,需要转换为slave角色并指向master服务器的IP+PORT+Password,所以直接在slave上指向master

    1. [root@slave ~]#redis-cli
    2. 127.0.0.1:6379> info
    3. NOAUTH Authentication required.
    4. 127.0.0.1:6379> AUTH 123456
    5. OK
    6. 127.0.0.1:6379>replicaof 10.0.1.17 6379
    7. OK
    8. 127.0.0.1:6379>CONFIG SET masterauth 123456
    9. OK
    10. 127.0.0.1:6379>INFO REPLICATION
    11. # Replication
    12. role:slave
    13. master_host:10.0.1.17
    14. master_port:6379
    15. master_link_status:up
    16. master_last_io_seconds_ago:1
    17. master_sync_in_progress:0
    18. slave_repl_offset:64278
    19. slave_priority:100
    20. slave_read_only:1
    21. connected_slaves:0
    22. master_replid:f5b26fc0180f43413f872d587bd23440c247c6b3
    23. master_replid2:0000000000000000000000000000000000000000
    24. master_repl_offset:64278
    25. second_repl_offset:-1
    26. repl_backlog_active:1
    27. repl_backlog_size:1048576
    28. repl_backlog_first_byte_offset:548
    29. repl_backlog_histlen:63731
    30. 127.0.0.1:6379>
  2. 在master查看并验证是否同步

    1. [root@master ~]# redis-cli
    2. 127.0.0.1:6379> auth 123456
    3. OK
    4. 127.0.0.1:6379> info replication
    5. # Replication
    6. role:master
    7. connected_slaves:1
    8. slave0:ip=10.0.1.27,port=6379,state=online,offset=64446,lag=1
    9. master_replid:f5b26fc0180f43413f872d587bd23440c247c6b3
    10. master_replid2:fbe1e515ffb59090ef311b2b182c28594a617aae
    11. master_repl_offset:64446
    12. second_repl_offset:253
    13. repl_backlog_active:1
    14. repl_backlog_size:1048576
    15. repl_backlog_first_byte_offset:1
    16. repl_backlog_histlen:64446
    17. 127.0.0.1:6379> set name yanlei
    18. OK
  3. 在slave上查看是否同步

    1. [root@slave ~]# redis-cli
    2. 127.0.0.1:6379> auth 123456
    3. OK
    4. 127.0.0.1:6379> get name
    5. "yanlei"
    6. 127.0.0.1:6379>
  4. 从节点的配置只能在当前生效,重启后就失效了,所以要将配置写入slave的配置文件中即可永久生效

    1. [root@slave ~]#vim /etc/redis.conf
    2. # replicaof <masterip> <masterport>
    3. replicaof 10.0.1.17 6379
    4. # If the master is password protected (using the "requirepass" configuration
    5. # directive below) it is possible to tell the replica to authenticate before
    6. # starting the replication synchronization process, otherwise the master will
    7. # refuse the replica request.
    8. # masterauth <master-password>
    9. masterauth 123456
    10. [root@slave ~]#systemctl restart redis
  5. 从节点无法写入数据,只能读

    1. 127.0.0.1:6379> set name2 haha
    2. (error) READONLY You can't write against a read only replica.

    3、哨兵的使用和实现机制

    redis的哨兵节点个数应该等于或大于3个,且为奇数。

  • 10.0.1.17 redis master
  • 10.0.1.27 redis slave1
  • 10.0.1.37 redis slave2
  1. 第一步先实现redis的主从复制

    • 在所有节点的redis.conf配置文件中修改以下选项(主从都包括)

      1. bind 0.0.0.0
      2. masterauth "123456"
      3. requirepass "123456"
    • 在两个从节点配置

      • slave1

        1. [root@redis-slave1 ~]#redis-cli -a 123456
        2. Warning: Using a password with '-a' or '-u' option on the command line interface
        3. may not be safe.
        4. 127.0.0.1:6379> REPLICAOF 10.0.1.17 6379
        5. OK
        6. 127.0.0.1:6379> CONFIG SET masterauth "123456"
        7. OK
        8. 127.0.0.1:6379> info replication
        9. # Replication
        10. role:slave
        11. master_host:10.0.1.17
        12. master_port:6379
        13. master_link_status:up
        14. master_last_io_seconds_ago:10
        15. master_sync_in_progress:0
        16. slave_repl_offset:96392
        17. slave_priority:100
        18. slave_read_only:1
        19. connected_slaves:1
        20. master_replid:f5b26fc0180f43413f872d587bd23440c247c6b3
        21. master_replid2:0000000000000000000000000000000000000000
        22. master_repl_offset:96392
        23. second_repl_offset:-1
        24. repl_backlog_active:1
        25. repl_backlog_size:1048576
        26. repl_backlog_first_byte_offset:65117
        27. repl_backlog_histlen:31276
      • slave2

        1. [root@slave2 ~]# redis-cli -a 123456
        2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
        3. 127.0.0.1:6379> REPLICAOF 10.0.1.17 6379
        4. OK
        5. 127.0.0.1:6379> CONFIG set masterauth 123456
        6. OK
        7. 127.0.0.1:6379> info replication
        8. # Replication
        9. role:slave
        10. master_host:10.0.1.17
        11. master_port:6379
        12. master_link_status:up
        13. master_last_io_seconds_ago:5
        14. master_sync_in_progress:0
        15. slave_repl_offset:96770
        16. slave_priority:100
        17. slave_read_only:1
        18. connected_slaves:0
        19. master_replid:f5b26fc0180f43413f872d587bd23440c247c6b3
        20. master_replid2:0000000000000000000000000000000000000000
        21. master_repl_offset:96770
        22. second_repl_offset:-1
        23. repl_backlog_active:1
        24. repl_backlog_size:1048576
        25. repl_backlog_first_byte_offset:65117
        26. repl_backlog_histlen:31654

        配置完成后能够实现主从复制就行了

  2. 配置3个哨兵服务器,3个的配置都是一样的,以下面的配置文件为模板,以此类推 ```bash [root@master ~]# grep -vE “^#|^$” /apps/redis/etc/sentinel.conf port 26379 daemonize yes pidfile redis-sentinel.pid logfile “sentinel_26379.log” dir /apps/redis/data sentinel monitor mymaster 10.0.1.17 6379 2 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 3000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes

[root@master ~]# redis-sentinel /apps/redis/etc/sentinel.conf [root@master ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 511 :26379 :
LISTEN 0 511
:6379 :
LISTEN 0 128 :22 :
LISTEN 0 100 [::1]:25 [::]:

LISTEN 0 80 [::]:3306 [::]:
LISTEN 0 511 [::]:26379 [::]:

LISTEN 0 128 [::]:22 [::]:*

[root@master ~]# scp /apps/redis/etc/sentinel.conf 10.0.1.27:/apps/redis/etc/ [root@master ~]# scp /apps/redis/etc/sentinel.conf 10.0.1.37:/apps/redis/etc/

  1. 3. **查看哨兵主机myid**
  2. ```bash
  3. [root@master ~]# grep "^sentinel myid" /apps/redis/etc/sentinel.conf
  4. sentinel myid 3eb09ca25cc7ac6686745f977b2bbaf627e75fef
  5. [root@slave1 redis]# grep "^sentinel myid" /apps/redis/etc/sentinel.conf
  6. sentinel myid ced9b422687516c2c459e728f9650bbba55e7df2
  7. [root@slave2 redis-5.0.7]# grep "^sentinel myid" /apps/redis/etc/sentinel.conf
  8. sentinel myid 849c4623561b90f91a9575a5de9846c01767e7b2
  1. 查看状态

    1. [root@master ~]# redis-cli -p 26379
    2. 127.0.0.1:26379>
    3. 127.0.0.1:26379> info sentinel
    4. # Sentinel
    5. sentinel_masters:1
    6. sentinel_tilt:0
    7. sentinel_running_scripts:0
    8. sentinel_scripts_queue_length:0
    9. sentinel_simulate_failure_flags:0
    10. master0:name=mymaster,status=ok,address=10.0.1.17:6379,slaves=2,sentinels=3
    11. 127.0.0.1:26379>
  2. 停止Redis Master测试故障转移 ```bash [root@master ~]#killall redis-server

[root@slave1 redis]# redis-cli -p 26379 -a 123456 Warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe. 127.0.0.1:26379> ifno sentinel

Sentinel

sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=10.0.1.27:6379,slaves=2,sentinels=2

测试成功,完成

  1. - 哨兵机制的存在,是为了实现redis故障转移的自动化,自动发现,自动转移,不需要人工参与
  2. - 哨兵机制的部署也就是为了监控主从数据节点的状态,一旦出现主节点down后,哨兵就会进行各种判断,完成后最终进行主从切换
  3. - 当一个哨兵节点判断主节点down掉是主观下线,超过半数的哨兵节点认为主节点下线后,这时候才会判定为主节点客观下线
  4. - 然后就会发起投票机制,每个哨兵都会投积自己为领导者,最终被投为领导者的哨兵将会成为新的主节点,完成自动化主从切换
  5. - 当判断为主观下线时,不会进行主从切换。
  6. <a name="H3D1d"></a>
  7. # 4、redis cluster集群创建和使用
  8. 这里准备了7台主机
  9. - 10.0.39.8
  10. - 10.0.39.18
  11. - 10.0.39.28
  12. - 10.0.39.38
  13. - 10.0.39.48
  14. - 10.0.39.58
  15. **需要注意是6台服务器的redis版本必须一致,而且是全新的6台服务器**
  16. 1. **安装redis完毕后修改redis的配置文件: /etc/redis.conf 6台主机的配置完全一样)**
  17. ```bash
  18. sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require￾full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf
  1. redis cluster集群的创建 ```bash [root@8 ~]# redis-cli -a 123456 —cluster create 10.0.39.8:6379 10.0.39.18:6379 10.0.39.28:6379 10.0.39.38:6379 10.0.39.48:6379 10.0.39.58:6379 —cluster-replicas 1

[root@8 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe. e6f08fd9fa80f979e514c43ebc576b1d75f8a74f 10.0.39.48:6379@16379 slave daa77c8fe0809cb522e987831a3d3ffe11149236 0 1603533810156 5 connected 5f32815f7f8c1063ef2398d306d59b16a92480e8 10.0.39.28:6379@16379 master - 0 1603533809148 3 connected 10923-16383 daa77c8fe0809cb522e987831a3d3ffe11149236 10.0.39.18:6379@16379 master - 0 1603533808000 2 connected 5461-10922 ec2da4a7c7c57c2c67a54400f346799687a74202 10.0.39.58:6379@16379 slave 5f32815f7f8c1063ef2398d306d59b16a92480e8 0 1603533809000 6 connected f92e571ae2b0bde74a8a00d9c8879254c7aa79c0 10.0.39.38:6379@16379 slave 73d1b51a7224e7da60e4a4b3b7456de8741aae23 0 1603533807000 4 connected 73d1b51a7224e7da60e4a4b3b7456de8741aae23 10.0.39.8:6379@16379 myself,master - 0 1603533807000 1 connected 0-5460

  1. 3. **redis cluster 写入key**
  2. 注意:redis集群写入key需要经过算法计算,计算出当前key的槽位,然后指定节点才能写入
  3. ```bash
  4. [root@8 ~]# redis-cli -a 123456 -h 10.0.39.8 set name yanlei
  5. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  6. (error) MOVED 5798 10.0.39.18:6379
  7. [root@8 ~]# redis-cli -a 123456 -h 10.0.39.18 set name yanlei
  8. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  9. OK
  10. [root@8 ~]# redis-cli -a 123456 -h 10.0.39.18 get name
  11. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  12. "yanlei"
  1. 模拟master故障,对应的slave节点自动提升为新master ```bash [root@8 ~]# redis-cli -a 123456 Warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe. 127.0.0.1:6379> shutdown not connected> exit

[root@18 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe. ec2da4a7c7c57c2c67a54400f346799687a74202 10.0.39.58:6379@16379 slave 5f32815f7f8c1063ef2398d306d59b16a92480e8 0 1603535031916 6 connected daa77c8fe0809cb522e987831a3d3ffe11149236 10.0.39.18:6379@16379 myself,master - 0 1603535030000 2 connected 5461-10922 73d1b51a7224e7da60e4a4b3b7456de8741aae23 10.0.39.8:6379@16379 master,fail - 1603535002648 1603534998000 1 disconnected 5f32815f7f8c1063ef2398d306d59b16a92480e8 10.0.39.28:6379@16379 master - 0 1603535031000 3 connected 10923-16383 f92e571ae2b0bde74a8a00d9c8879254c7aa79c0 10.0.39.38:6379@16379 master - 0 1603535032926 7 connected 0-5460 e6f08fd9fa80f979e514c43ebc576b1d75f8a74f 10.0.39.48:6379@16379 slave daa77c8fe0809cb522e987831a3d3ffe11149236 0 1603535028886 5 connected

可以从上面看出10.0.39.8已经关闭掉,它的从节点10.0.39.38成为了新master

当修复好10.0.39.8后,重新启动,它将成为10.0.39.38的新slave

[root@18 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe. ec2da4a7c7c57c2c67a54400f346799687a74202 10.0.39.58:6379@16379 slave 5f32815f7f8c1063ef2398d306d59b16a92480e8 0 1603535218939 6 connected daa77c8fe0809cb522e987831a3d3ffe11149236 10.0.39.18:6379@16379 myself,master - 0 1603535216000 2 connected 5461-10922 73d1b51a7224e7da60e4a4b3b7456de8741aae23 10.0.39.8:6379@16379 slave f92e571ae2b0bde74a8a00d9c8879254c7aa79c0 0 1603535216899 7 connected 5f32815f7f8c1063ef2398d306d59b16a92480e8 10.0.39.28:6379@16379 master - 0 1603535215000 3 connected 10923-16383 f92e571ae2b0bde74a8a00d9c8879254c7aa79c0 10.0.39.38:6379@16379 master - 0 1603535215000 7 connected 0-5460 e6f08fd9fa80f979e514c43ebc576b1d75f8a74f 10.0.39.48:6379@16379 slave daa77c8fe0809cb522e987831a3d3ffe11149236 0 1603535217915 5 connected

  1. 5. **redis cluster动态扩容**
  2. **添加新的节点,其中redis的版本需要和已经在运行中的redis版本一致,配置也需要一致**
  3. ```bash
  4. #加入集群
  5. [root@8 ~]# redis-cli -a 123456 --cluster add-node 10.0.39.68:6379 10.0.39.8:6379
  6. #加入集群成功,但是没有槽位(slot)
  7. [root@8 ~]# redis-cli -a 123456 --cluster info 10.0.39.8:6379
  8. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  9. 10.0.39.8:6379 (73d1b51a...) -> 0 keys | 5461 slots | 1 slaves.
  10. 10.0.39.28:6379 (5f32815f...) -> 0 keys | 5461 slots | 1 slaves.
  11. 10.0.39.68:6379 (7acaf66d...) -> 0 keys | 0 slots | 0 slaves.
  12. 10.0.39.18:6379 (daa77c8f...) -> 1 keys | 5462 slots | 1 slaves.
  13. [OK] 1 keys in 4 masters.
  14. 0.00 keys per slot on average.
  15. #给新的master分配槽位:分配的槽位是16384除以master的个数
  16. [root@68 ~]# redis-cli -a 123456 --cluster reshard 10.0.39.8:6379
  17. .....
  18. [root@8 ~]# redis-cli -a 123456 --cluster info 10.0.39.8:6379
  19. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  20. 10.0.39.8:6379 (73d1b51a...) -> 0 keys | 4096 slots | 1 slaves.
  21. 10.0.39.28:6379 (5f32815f...) -> 0 keys | 4096 slots | 1 slaves.
  22. 10.0.39.68:6379 (7acaf66d...) -> 1 keys | 4096 slots | 0 slaves.
  23. 10.0.39.18:6379 (daa77c8f...) -> 0 keys | 4096 slots | 1 slaves.
  24. [OK] 1 keys in 4 masters.
  25. 0.00 keys per slot on average.
  26. #分配完成,每个master拥有4096个slot位
  27. #新的master还没有slave节点,给它添加新的slave接点
  28. [root@8 ~]# redis-cli -a 123456 --cluster add-node 10.0.39.78:6379 10.0.39.8:6379 --cluster-slave --cluster-master-id 7acaf66def0907a745d16dbc74041edc3be9672e
  29. #已经加入集群并成为10.0.39.68的slave节点
  30. [root@18 ~]# redis-cli -a 123456 cluster nodes
  31. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  32. ec2da4a7c7c57c2c67a54400f346799687a74202 10.0.39.58:6379@16379 slave 5f32815f7f8c1063ef2398d306d59b16a92480e8 0 1603539382745 6 connected
  33. d86163b310f0129ad9ce94aa79631b75074284a7 10.0.39.78:6379@16379 slave 7acaf66def0907a745d16dbc74041edc3be9672e 0 1603539385782 9 connected
  34. 7acaf66def0907a745d16dbc74041edc3be9672e 10.0.39.68:6379@16379 master - 0 1603539384000 9 connected 0-1364 5461-6826 10923-12287
  35. daa77c8fe0809cb522e987831a3d3ffe11149236 10.0.39.18:6379@16379 myself,master - 0 1603539385000 2 connected 6827-10922
  36. 73d1b51a7224e7da60e4a4b3b7456de8741aae23 10.0.39.8:6379@16379 master - 0 1603539383755 8 connected 1365-5460
  37. 5f32815f7f8c1063ef2398d306d59b16a92480e8 10.0.39.28:6379@16379 master - 0 1603539381000 3 connected 12288-16383
  38. f92e571ae2b0bde74a8a00d9c8879254c7aa79c0 10.0.39.38:6379@16379 slave 73d1b51a7224e7da60e4a4b3b7456de8741aae23 0 1603539382000 8 connected
  39. e6f08fd9fa80f979e514c43ebc576b1d75f8a74f 10.0.39.48:6379@16379 slave daa77c8fe0809cb522e987831a3d3ffe11149236 0 1603539384770 5 connected
  1. redis cluster 动态缩容

删除节点过程:

  • 添加节点的时候是先添加node节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相反,是先将被删除的Redis node上的槽位迁移到集群中的其他Redis node节点上,然后再将其删除,如果一个Redis node节点上的槽位没有被完全迁移,删除该node的时候会提示有数据且无法删除。
  • 注意: 被迁移的redis master服务器必须保证没有数据,否则迁移会报错并强制中断。
  • 删除10.0.39.68节点 ```bash redis-cli -a 123456 —cluster reshard 10.0.39.8:6379

    执行3次,将10.0.39.68节点的槽位平均的非给其他3个master即可

确认10.0.39.68的所有slot都移走了,上面的slave也自动删除,成为其它master的slave

[root@8 ~]# redis-cli -a 123456 —cluster check 10.0.39.8:6379 Warning: Using a password with ‘-a’ or ‘-u’ option on the command line interface may not be safe. 10.0.39.8:6379 (73d1b51a…) -> 0 keys | 5480 slots | 2 slaves. 10.0.39.28:6379 (5f32815f…) -> 0 keys | 5452 slots | 1 slaves. 10.0.39.68:6379 (7acaf66d…) -> 0 keys | 0 slots | 0 slaves. 10.0.39.18:6379 (daa77c8f…) -> 1 keys | 5452 slots | 1 slaves. [OK] 1 keys in 4 masters. 0.00 keys per slot on average.

  1. - 虽然槽位已经迁移完成,但是服务器IP信息还在集群当中,因此还需要将IP信息从集群删除
  2. - 注意: 删除服务器前,必须清除主机上面的槽位,否则会删除主机失败
  3. ```bash
  4. [root@8 ~]# redis-cli -a 123456 --cluster del-node 10.0.39.8:6379 7acaf66def0907a745d16dbc74041edc3be9672e
  5. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  6. >>> Removing node 7acaf66def0907a745d16dbc74041edc3be9672e from cluster 10.0.39.8:6379
  7. >>> Sending CLUSTER FORGET messages to the cluster...
  8. >>> SHUTDOWN the node.
  9. #删除节点后,redis进程自动关闭
  10. #删除节点信息
  11. [root@68 ~]#rm -f /var/lib/redis/nodes-6379.conf
  12. [root@8 ~]# redis-cli -a 123456 cluster nodes
  13. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  14. 5f32815f7f8c1063ef2398d306d59b16a92480e8 10.0.39.28:6379@16379 master - 0 1603540495000 12 connected 6808-6826 10923-12259 12288-16383
  15. ec2da4a7c7c57c2c67a54400f346799687a74202 10.0.39.58:6379@16379 slave 5f32815f7f8c1063ef2398d306d59b16a92480e8 0 1603540494000 12 connected
  16. f92e571ae2b0bde74a8a00d9c8879254c7aa79c0 10.0.39.38:6379@16379 slave 73d1b51a7224e7da60e4a4b3b7456de8741aae23 0 1603540492633 13 connected
  17. e6f08fd9fa80f979e514c43ebc576b1d75f8a74f 10.0.39.48:6379@16379 slave daa77c8fe0809cb522e987831a3d3ffe11149236 0 1603540494647 11 connected
  18. 73d1b51a7224e7da60e4a4b3b7456de8741aae23 10.0.39.8:6379@16379 myself,master - 0 1603540493000 13 connected 0-1355 1365-5460 12260-12287
  19. daa77c8fe0809cb522e987831a3d3ffe11149236 10.0.39.18:6379@16379 master - 0 1603540494000 11 connected 1356-1364 5461-6807 6827-10922
  20. d86163b310f0129ad9ce94aa79631b75074284a7 10.0.39.78:6379@16379 slave 73d1b51a7224e7da60e4a4b3b7456de8741aae23 0 1603540495655 13 connected
  21. #可以看到10.0.39.68节点已经消失了,彻底删除了
  22. #删除多余的从节点以及其的集群文件
  23. [root@8 ~]# redis-cli -a 123456 --cluster del-node 10.0.39.8:6379 d86163b310f0129ad9ce94aa79631b75074284a7
  24. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  25. >>> Removing node d86163b310f0129ad9ce94aa79631b75074284a7 from cluster 10.0.39.8:6379
  26. >>> Sending CLUSTER FORGET messages to the cluster...
  27. >>> SHUTDOWN the node.
  28. [root@78 ~]# rm -f /var/lib/redis/nodes-6379.conf
  29. [root@8 ~]# redis-cli -a 123456 cluster nodes
  30. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  31. 5f32815f7f8c1063ef2398d306d59b16a92480e8 10.0.39.28:6379@16379 master - 0 1603540651000 12 connected 6808-6826 10923-12259 12288-16383
  32. ec2da4a7c7c57c2c67a54400f346799687a74202 10.0.39.58:6379@16379 slave 5f32815f7f8c1063ef2398d306d59b16a92480e8 0 1603540652114 12 conneed
  33. f92e571ae2b0bde74a8a00d9c8879254c7aa79c0 10.0.39.38:6379@16379 slave 73d1b51a7224e7da60e4a4b3b7456de8741aae23 0 1603540653123 13 conneed
  34. e6f08fd9fa80f979e514c43ebc576b1d75f8a74f 10.0.39.48:6379@16379 slave daa77c8fe0809cb522e987831a3d3ffe11149236 0 1603540651104 11 conneed
  35. 73d1b51a7224e7da60e4a4b3b7456de8741aae23 10.0.39.8:6379@16379 myself,master - 0 1603540652000 13 connected 0-1355 1365-5460 12260-1228
  36. daa77c8fe0809cb522e987831a3d3ffe11149236 10.0.39.18:6379@16379 master - 0 1603540651000 11 connected 1356-1364 5461-6807 6827-10922
  1. 导入现有的redis数据到redis cluster
  • 注意: 导入数据需要redis cluster不能与被导入的数据有重复的key名称,否则导入不成功或中断。
  • 导入数据之前需要关闭各redis 服务器的密码,包括集群中的各node和源Redis server,避免认证带来的环境不一致从而无法导入,可以加参数—cluster-replace 强制替换Redis cluster已有的key。
  • 需要关闭所有节点的redis密码认证

    • 先在10.0.39.68上面写入一些数据

      1. [root@68 ~]# redis-cli
      2. 127.0.0.1:6379> set key1 haha
      3. OK
      4. 127.0.0.1:6379> set key2 haha
      5. OK
      6. 127.0.0.1:6379> set key3 haha
      7. OK
      8. 127.0.0.1:6379> set key4 haha
      9. OK
      10. 127.0.0.1:6379> set key5 haha
      11. OK
      12. 127.0.0.1:6379> set key6 haha
      13. OK
      14. 127.0.0.1:6379> set key7 haha
      15. OK
      16. 127.0.0.1:6379> set key8 haha
      17. OK
      18. 127.0.0.1:6379> set key9 haha
      19. OK
      20. 127.0.0.1:6379> set key10 haha
      21. OK
    • 然后再进行实验操作 ```bash

      在所有节点上取消需要导入的主机的密码,执行以下命令

      [root@8 ~]# redis-cli -a 123456 —no-auth-warning config set requirepass “” OK

再导入数据到集群中去

[root@8 ~]#redis-cli —cluster import 10.0.39.8:6379 —cluster-from 10.0.39.68:6379 —cluster-copy —cluster-replace

验证是否成功

验证数据

[root@centos8 ~]#redis-cli -h 10.0.0.8 keys ‘‘ 1) “key5” 2) “key1” 3) “key9” [root@centos8 ~]#redis-cli -h 10.0.0.18 keys ‘‘ 1) “key8” 2) “key4” 3) “key3” 4) “key7” [root@centos8 ~]#redis-cli -h 10.0.0.28 keys ‘*’ 1) “key6” 2) “key10” 3) “key2”

  1. 8. **集群偏移**
  2. redis cluster多个节点运行一段时间后,可能会出现倾斜现象,某个节点数据偏多,内存消耗更大,或则接收用户访问请求更多<br />可能原因:
  3. - 节点和槽分配不均
  4. - 不同槽对应键值数量差异较大
  5. - 包含bigkey,建议少用
  6. - 内存相关配置不一致
  7. - 热点数据不均衡 : 一致性不高时,可以使用本缓存和MQ
  8. ```bash
  9. #执行自动的槽位重新平衡分布
  10. [root@8 ~]# redis-cli --cluster rebalance 10.0.39.8:6379
  11. >>> Performing Cluster Check (using node 10.0.39.8:6379)
  12. [OK] All nodes agree about slots configuration.
  13. >>> Check for open slots...
  14. >>> Check slots coverage...
  15. [OK] All 16384 slots covered.
  16. *** No rebalancing needed! All nodes are within the 2.00% threshold.