Redis Cluster搭建、节点扩容及节点删减

一、Redis Cluster搭建

本文讲述搭建一主一从的redis集群,严格来说 redis cluster要求至少三个节点(三个主节点):《为什么Redis Cluster中至少应该有三个结点?》
本文搭建的伪集群用同一台服务器,一个端口为4131作为主节点,一个端口4132作为从节点。

  1. 编辑配置文件
[redis@VM-0-13-centos ~]$ mkdir redis4131
[redis@VM-0-13-centos ~]$ touch redis4131.conf
[redis@VM-0-13-centos ~]$ cat redis4131.conf
port 4131
daemonize yes
dir '/home/redis/redis-4131'
logfile '/home/redis/redis_4131.log'
dbfilename 'redis_4131.data'
cluster-enabled yes
cluster-config-file nodes-4131.conf
cluster-require-full-coverage no
requirepass 1234
masterauth 1234
[redis@VM-0-13-centos ~]$ mkdir redis4132
[redis@VM-0-13-centos ~]$ touch redis4132.conf
[redis@VM-0-13-centos ~]$ cat redis4132.conf
port 4132
daemonize yes
dir '/home/redis/redis-4132'
logfile '/home/redis/redis_4132.log'
dbfilename 'redis_4132.data'
cluster-enabled yes
cluster-config-file nodes-4132.conf
cluster-require-full-coverage no
requirepass 1234
masterauth 1234
  1. 启动redis-server
[redis@VM-0-13-centos ~] redis-server redis-4131/redis4131.conf
[redis@VM-0-13-centos ~] redis-server redis-4132/redis4132.conf
[redis@VM-0-13-centos ~] ps -ef|grep redis-server
redis     3651 12573  0 17:10 pts/2    00:00:00 grep --color=auto redis-server
redis    13577     1  0 16:38 ?        00:00:01 redis-server *:4132 [cluster]
redis    13609     1  0 16:38 ?        00:00:04 redis-server *:4131 [cluster]
  1. meet添加节点
[redis@VM-0-13-centos ~] redis-cli -p 4131 cluster meet 127.0.0.1 4132

此时redis的状态处于未分配slot的状态

  1. 添加slot
    编写添加脚本
[redis@VM-0-13-centos ~] vi add_slots.sh
#!/bin/bash
start=$1
end=$2
port=$3
auth=$4
for slot in `seq ${start} ${end}`
do
	echo "slot:${slot}"
	redis-cli -p ${port} -a ${auth} cluster addslots ${slot} 
done

[redis@VM-0-13-centos ~] bash add_slots.sh 0 16383 4131 1234
slot:0
OK
slot:1
OK
.
.
.
OK
slot:16383
OK
[redis@VM-0-13-centos ~] redis-cli -a 1234 -p 4131 cluster nodes
e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132@14132 master - 0 1615625361433 0 connected
c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131@14131 myself,master - 0 0 1 connected 0-16383

此时0-16383总共16384个slot已经全部分配到了此节点上

  1. 添加主从关系
[redis@VM-0-13-centos ~] redis-cli -p 4132 -a 1234 cluseter replicate c611941d03a35765bb4041dab8fca5fdd272f4c4
OK
[redis@VM-0-13-centos ~]redis-cli -a 1234 -p 4131 cluster nodes
e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132@14132 slave c611941d03a35765bb4041dab8fca5fdd272f4c4 0 1615625502730 1 connected
c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131@14131 myself,master - 0 0 1 connected 0-16383
[redis@VM-0-13-centos ~]redis-cli -a 1234 -p 4131 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:2
cluster_size:1
cluster_current_epoch:1
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1883
cluster_stats_messages_pong_sent:1880
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:3764
cluster_stats_messages_ping_received:1880
cluster_stats_messages_pong_received:1884
cluster_stats_messages_received:3764

二、节点扩容

有两种方法可以实现扩容,一种是手动迁移某个slot的全部key,另一种方法是使用redis-trib.rb。使用后一种方法更简单,这里采集redis-trib.rb,redis-trib.rb的全部功能已经整合到redis-cli --cluster中。

  1. 启动两个新的节点
    同上,这里使用端口4133,4134
[redis@VM-0-13-centos ~]$ ps -ef|grep redis-server
redis    12146     1  0 10:37 ?        00:00:00 redis-server *:4133 [cluster]
redis    12173     1  0 10:37 ?        00:00:00 redis-server *:4134 [cluster]
redis    12196 11303  0 10:37 pts/1    00:00:00 grep --color=auto redis-server
redis    13577     1  0 Mar13 ?        00:00:49 redis-server *:4132 [cluster]
redis    13609     1  0 Mar13 ?        00:00:57 redis-server *:4131 [cluster]
root     28311     1  0 Mar11 ?        00:03:48 redis-server *:26379
  1. 新增节点加入到集群中
    这里将4133作为一个主节点
[redis@VM-0-13-centos ~]$ redis-cli --cluster add-node 127.0.0.1:4133 127.0.0.1:4131 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 127.0.0.1:4133 to cluster 127.0.0.1:4131
>>> Performing Cluster Check (using node 127.0.0.1:4131)
M: c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131
   slots:[0-16383] (16384 slots) master
   1 additional replica(s)
S: e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132
   slots: (0 slots) slave
   replicates c611941d03a35765bb4041dab8fca5fdd272f4c4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:4133 to make it join the cluster.
[OK] New node added correctly.
  1. 给新增节点分配slots
    把主节点4131的500个slots分配到新增节点4133中
[redis@VM-0-13-centos ~]$ redis-cli --cluster reshard 127.0.0.1:4131 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 127.0.0.1:4131)
M: c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131
   slots:[0-16383] (16384 slots) master
   1 additional replica(s)
M: c2f0a9f5830a2b70013f54f97921870729f94cde 127.0.0.1:4133
   slots: (0 slots) master
S: e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132
   slots: (0 slots) slave
   replicates c611941d03a35765bb4041dab8fca5fdd272f4c4
[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)? 500
What is the receiving node ID? c2f0a9f5830a2b70013f54f97921870729f94cde
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

Ready to move 500 slots.
  Source nodes:
    M: c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131
       slots:[0-16383] (16384 slots) master
       1 additional replica(s)
  Destination node:
    M: c2f0a9f5830a2b70013f54f97921870729f94cde 127.0.0.1:4133
       slots: (0 slots) master
  Resharding plan:
    Moving slot 0 from c611941d03a35765bb4041dab8fca5fdd272f4c4
    ...
	Moving slot 499 from c611941d03a35765bb4041dab8fca5fdd272f4c4
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 127.0.0.1:4131 to 127.0.0.1:4133: 
Moving slot 1 from 127.0.0.1:4131 to 127.0.0.1:4133: 
Moving slot 2 from 127.0.0.1:4131 to 127.0.0.1:4133: 
[redis@VM-0-13-centos ~]$ redis-cli -a 1234 -p 4131 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
c2f0a9f5830a2b70013f54f97921870729f94cde 127.0.0.1:4133@14133 master - 0 1615690724440 2 connected 0-499
e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132@14132 slave c611941d03a35765bb4041dab8fca5fdd272f4c4 0 1615690723439 1 connected
c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131@14131 myself,master - 0 1615690723000 1 connected 500-16383

可以看到此时slots 0-499已经转移到了4133节点上
4. 添加从节点
4134端口的服务作为4133服务的从节点

[redis@VM-0-13-centos ~]$ redis-cli --cluster add-node 127.0.0.1:4134 127.0.0.1:4131 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 127.0.0.1:4134 to cluster 127.0.0.1:4131
>>> Performing Cluster Check (using node 127.0.0.1:4131)
M: c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131
   slots:[500-16383] (15884 slots) master
   1 additional replica(s)
M: c2f0a9f5830a2b70013f54f97921870729f94cde 127.0.0.1:4133
   slots:[0-499] (500 slots) master
S: e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132
   slots: (0 slots) slave
   replicates c611941d03a35765bb4041dab8fca5fdd272f4c4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:4134 to make it join the cluster.
[OK] New node added correctly.

[redis@VM-0-13-centos ~]$ redis-cli -a 1234 -p 4134 cluster replicate c2f0a9f5830a2b70013f54f97921870729f94cde
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

[redis@VM-0-13-centos ~]$ redis-cli -a 1234 -p 4131 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
c2f0a9f5830a2b70013f54f97921870729f94cde 127.0.0.1:4133@14133 master - 0 1615690939854 2 connected 0-499
e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132@14132 slave c611941d03a35765bb4041dab8fca5fdd272f4c4 0 1615690941858 1 connected
c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131@14131 myself,master - 0 1615690941000 1 connected 500-16383
eba176a6ccc9e6b83cf9a2571d0ad2f524a44db1 127.0.0.1:4134@14134 slave c2f0a9f5830a2b70013f54f97921870729f94cde 0 1615690942860 2 connected

[redis@VM-0-13-centos ~]$ redis-cli -a 1234 -p 4131 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:4
cluster_size:2
cluster_current_epoch:2
cluster_my_epoch:1
cluster_stats_messages_ping_sent:63957
cluster_stats_messages_pong_sent:63948
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:127906
cluster_stats_messages_ping_received:63946
cluster_stats_messages_pong_received:63958
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:127906

三、节点删除

  1. 删除从节点
[redis@VM-0-13-centos ~]$ redis-cli --cluster del-node 127.0.0.1:4134 eba176a6ccc9e6b83cf9a2571d0ad2f524a44db1 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node eba176a6ccc9e6b83cf9a2571d0ad2f524a44db1 from cluster 127.0.0.1:4134
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

[redis@VM-0-13-centos ~]$ redis-cli -a 1234 -p 4131 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
c2f0a9f5830a2b70013f54f97921870729f94cde 127.0.0.1:4133@14133 master - 0 1615691570015 2 connected 0-499
e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132@14132 slave c611941d03a35765bb4041dab8fca5fdd272f4c4 0 1615691571017 1 connected
c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131@14131 myself,master - 0 1615691569000 1 connected 500-16383
  1. 删除主节点
    步骤仍然同于主节点的添加,需要先迁移slot,后删除节点
[redis@VM-0-13-centos ~]$ redis-cli --cluster reshard 127.0.0.1:4133 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 127.0.0.1:4133)
M: c2f0a9f5830a2b70013f54f97921870729f94cde 127.0.0.1:4133
   slots:[0-499] (500 slots) master
M: c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131
   slots:[500-16383] (15884 slots) master
   1 additional replica(s)
S: e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132
   slots: (0 slots) slave
   replicates c611941d03a35765bb4041dab8fca5fdd272f4c4
[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)? 500
What is the receiving node ID? c611941d03a35765bb4041dab8fca5fdd272f4c4
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

Ready to move 500 slots.
...
[redis@VM-0-13-centos ~]$ redis-cli --cluster del-node 127.0.0.1:4133 c2f0a9f5830a2b70013f54f97921870729f94cde -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node c2f0a9f5830a2b70013f54f97921870729f94cde from cluster 127.0.0.1:4133
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

[redis@VM-0-13-centos ~]$ redis-cli -a 1234 -p 4131 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
e23ca15d294844ec3c17beff0153194d63848345 127.0.0.1:4132@14132 slave c611941d03a35765bb4041dab8fca5fdd272f4c4 0 1615691845549 3 connected
c611941d03a35765bb4041dab8fca5fdd272f4c4 127.0.0.1:4131@14131 myself,master - 0 1615691825000 3 connected 0-16383

删除节点之后,4133服务的全部节点归还给了4131。集群再次回到了单节点的状态。

博文参考:
《高可用Redis:Redis Cluster》
《高可用Redis(十):Redis原生命令搭建集群》
《Redis进阶实践之十二 Redis的Cluster集群动态扩容》
《关于redis cluster 集群模式,你必须知道的几件事情》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值