redis集群:手动与自动

手动建立:

环境描述:一台机器启动六个节点,3个主节点,3个从节点。
安装:
tar -zxvf redis-3.2.10.tar.gz
mv redis-3.2.10 /usr/local/redis
yum install gcc* tcl -y
make && make test
修改配置文件:
vi /usr/local/redis/redis.confphp

**要改的地方**
daemonize yes
port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf . //这个编号最好是等于你的端口号
cluster-node-timeout 5000

mkdir /usr/local/cluster-test/ //同时复制配置文件,且修改端口及参数node

**在/usr/local/cluster-test/下建立**
mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005

启动服务:linux

./redis-server /usr/local/cluster-test/7000/redis.conf
./redis-server /usr/local/cluster-test/7001/redis.conf
./redis-server /usr/local/cluster-test/7002/redis.conf
./redis-server /usr/local/cluster-test/7003/redis.conf
./redis-server /usr/local/cluster-test/7004/redis.conf
./redis-server /usr/local/cluster-test/7005/redis.conf

netstat -anput | grep redis
[root@localhost src]# ./redis-cli -h 127.0.0.1 -p 7000web

127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7000
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7001
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7002
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7003
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7004
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7005
127.0.0.1:7000> CLUSTER NODES
127.0.0.1:7000> CLUSTER INFO . //cluter开始的时候是fail

给三个主节点分配槽点:redis

redis-cli -h 127.0.0.1 -p 7000 cluster addslots {0..5461}
redis-cli -h 127.0.0.1 -p 7001 cluster addslots {5462..10922}
redis-cli -h 127.0.0.1 -p 7002 cluster addslots {10923..16383}

设置从节点:
[root@localhost src]# ./redis-cli -p 7003
127.0.0.1:7003> CLUSTER REPLICATE
8573e44492c6ba0713e36545202459974cc18cf7
[root@localhost src]# ./redis-cli -p 7004
127.0.0.1:7004> CLUSTER REPLICATE
59d75c9a0b599265d0ac123cc99f18050df5afa3
[root@localhost src]# ./redis-cli -p 7005
127.0.0.1:7005> CLUSTER REPLICATE
607f5472bdd9f581ad16b960244ee7ba18fb5a1f
[root@localhost src]# ./redis-cli -p 7000
127.0.0.1:7000> 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:5
cluster_my_epoch:3
cluster_stats_messages_sent:7306
cluster_stats_messages_received:7306

自动建立:

环境描述:一台机器启动六个节点,3个主节点,3个从节点。
安装:
tar -zxvf redis-3.2.10.tar.gz
mv redis-3.2.10 /usr/local/redis
yum install gcc* tcl -y
make && make test
修改配置文件:
vi /usr/local/redis/redis.confruby

daemonize yes
port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf . //这个编号最好是等于你的端口号
cluster-node-timeout 5000
appendonly yes

mkdir /usr/local/cluster-test/ //同时复制配置文件,且修改端口及参数服务器

mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005

启动服务:app

redis-server /usr/local/cluster-test/7000/redis.conf
redis-server /usr/local/cluster-test/7001/redis.conf
redis-server /usr/local/cluster-test/7002/redis.conf
redis-server /usr/local/cluster-test/7003/redis.conf
redis-server /usr/local/cluster-test/7004/redis.conf
redis-server /usr/local/cluster-test/7005/redis.conf

[root@localhost ~]#netstat -anput | grep redis
将6个节点连在一块儿构成集群
须要用到的命令就是redis-trib.rb,这是官方的一个用ruby写的一个操做redis cluster的命
令,因此,你的机器上须要安装ruby。咱们先试一下这个命令:
[root@web3 7005]# yum -y install rpm-build openssl openssl-devel
[root@localhost ruby]# tar -zxvf ruby-2.3.1.tar.gz
[root@localhost ruby]#./configure --prefix=/usr/local/ruby
[root@localhost ruby]#make && make install
[root@localhost ruby]#ln -s /usr/local/ruby/bin/* /usr/local/bin/
[root@localhost ~]# ln /usr/local/redis/src/redis-trib.rb /usr/local/bin/
[root@localhost ~]#gem install redis-3.3.0.gem
[root@localhost ~]# gem list redis
由于咱们要新建集群, 因此这里使用create命令. --replicas 1 参数表示为每一个主节点建立一
个从节点. 其余参数是实例的地址集合。ide

[root@localhost src]# redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

Creating cluster
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
S: d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003
replicates 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4
S: 4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
S: 30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005
replicates dfa0754c7854a874a6ebd2613b86140ad97701fc
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 127.0.0.1:7000)
M: 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
M: d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003
slots: (0 slots) master
replicates 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4
M: 4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004
slots: (0 slots) master
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
M: 30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005
slots: (0 slots) master
replicates dfa0754c7854a874a6ebd2613b86140ad97701fc
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
Redis-trib会提示你作了什么配置, 输入yes接受. 集群就被配置和加入了, 意思是, 实例
会通过互相交流后启动。
测试集群的状态:
[root@localhost 7000]# redis-trib.rb check 127.0.0.1:7000
能够看到有3个主节点,3个从节点。每一个节点都是成功的链接状态
3个主节点[M]是:
7000 (3707debcbe7be66d4a1968eaf3a5ffaf4308efa4)
7001 (cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c)
7002 (dfa0754c7854a874a6ebd2613b86140ad97701fc)
3个从节点[S]是:
7003 (d2237fdcfbba672de766b913d1186cebcb6e1761)->7000
7004 (4b4aef8b48c427a3c903518339d53b6447c58b93)->7001
7005 (30858dbf483b61b9838d5c1f853a60beaa4e7afd) ->7002
测试链接集群
刚才集群搭建成功了。按照redis cluster的特色,它是去中心化,每一个节点都是对等的,所
以,你链接哪一个节点均可以获取和设置数据,咱们来试一下。 redis-cli是redis默认的客户
端工具,启动时加上-c参数,就能够链接到集群。 链接任意一个节点端口:
[root@web3 7000]# redis-cli -c -p 7000
127.0.0.1:7000>
设置一个值:
127.0.0.1:7000> set my_name linux
-> Redirected to slot [12803] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get my_name
"linux"
127.0.0.1:7002>
前面理论知识咱们知道了,分配key的时候,它会使用CRC16('my_name')%16384算法,
来计算,将这个key放到哪一个节点,这里分配到了12803 slot 就分配到了7002(10923-
16383)这个节点上。
Redirected to slot [12803] located at 127.0.0.1:7002
redis cluster 采用的方式很直接,它直接跳转到7002节点了,而不是还在自身的7000节
点。 如今咱们链接7005这个从节点:
[root@web3 7000]# redis-cli -c -p 7005

127.0.0.1:7005> get my_name
-> Redirected to slot [12803] located at 127.0.0.1:7002
"linux"
127.0.0.1:7002>
咱们一样是获取my_name的值,它一样也是跳转到了7002上。 咱们再尝试一些其余的可
以:
127.0.0.1:7002> set age 123
-> Redirected to slot [741] located at 127.0.0.1:7000
OK
127.0.0.1:7000> set height 565
-> Redirected to slot [8223] located at 127.0.0.1:7001
OK
127.0.0.1:7001> set sex 1
-> Redirected to slot [2584] located at 127.0.0.1:7000
OK
127.0.0.1:7000> set home china
-> Redirected to slot [10814] located at 127.0.0.1:7001
OK
127.0.0.1:7001> set city shanghai
-> Redirected to slot [11479] located at 127.0.0.1:7002
OK
127.0.0.1:7002> set citylocate shanghaipudong
OK
127.0.0.1:7001> set wsdwxzx hhh
-> Redirected to slot [15487] located at 127.0.0.1:7002
OK
127.0.0.1:7002> zadd erew 333 rrr
-> Redirected to slot [10576] located at 127.0.0.1:7001
(integer) 1
127.0.0.1:7000> zrange erew 0 -1
-> Redirected to slot [10576] located at 127.0.0.1:7001
1) "rrr"
127.0.0.1:7001>
128.~~~~
能够看出,数据都会在7000-7002 这3个主节点来跳转存储。
测试集群中的节点挂掉
上面咱们创建来了一个集群。3个主节点[7000-7002]提供数据存粗和读取,3个从节点
[7003-7005]则是负责把[7000-7002]的数据同步到本身的节点上来,咱们来看一下
[7003-7005]的appendonly.aof的内容。看看是否是不这样:
`[root@web3 7005]# cd /usr/local/cluster-test/7003`
`[root@web3 7003]# vi appendonly.aof`
*2
$6
SELECT
$1
0
*3
$3
set
$3
age
$3
123
*3
$3
set
$3
sex
$1
1
*3
$3
set
$3
job
$3
php
咱们看下,的确是从7000节点上同步过来的数据,7004,7005也是。
下面,咱们先来模拟其中一台Master主服务器挂掉的状况。
`[root@web3 7003]# ps -ef|grep redis`
root 11380 1 0 07:37 ? 00:00:03 redis-server *:7000 [cluster]
root 11384 1 0 07:37 ? 00:00:03 redis-server *:7001 [cluster]
root 11388 1 0 07:37 ? 00:00:03 redis-server *:7002 [cluster]
root 11392 1 0 07:37 ? 00:00:03 redis-server *:7003 [cluster]
root 11396 1 0 07:37 ? 00:00:04 redis-server *:7004 [cluster]
root 11400 1 0 07:37 ? 00:00:03 redis-server *:7005 [cluster]
好,安装前面的理论,7000主节点挂掉了,那么这个时候,7000的从节点只有7003一个,
确定7003就会被选举称Master节点了:
`[root@web3 7003]# kill 11380`
`[root@web3 7003]# redis-trib.rb check 127.0.0.1:7000`
Connecting to node 127.0.0.1:7000: [ERR] Sorry, can't connect to node
127.0.0.1:7000
`[root@web3 7003]# redis-trib.rb check 127.0.0.1:7001`
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7002: OK
 Performing Cluster Check (using node 127.0.0.1:7001)
M: cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004
slots: (0 slots) slave
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
S: 30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005
slots: (0 slots) slave
replicates dfa0754c7854a874a6ebd2613b86140ad97701fc
M: d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003
slots:0-5460 (5461 slots) master
0 additional replica(s)
M: dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002
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.
看了下,上面有了三个M节点了,果然,7003被选取成了替代7000成为主节点了。那咱们
来获取原先存在7000节点的数据:
`[root@web3 7003]# redis-cli -c -p 7001`
127.0.0.1:7001> get sex
 Redirected to slot [2584] located at 127.0.0.1:7003
"1"
127.0.0.1:7003>
数据果然没有丢失,而是从7003上面获取了。
咱们再来模拟 7000节点从新启动了的状况,那么它还会自动加入到集群中吗?那么,7000
这个节点上充当什么角色呢? 咱们试一下: 从新启动 7000 节点:
[root@web3 7003]# cd ../7000
[root@web3 7000]# ll
total 56
-rw-r--r-- 1 root root 114 Oct 17 08:16 appendonly.aof
-rw-r--r-- 1 root root 43 Oct 17 08:37 dump.rdb
-rw-r--r-- 1 root root 745 Oct 17 08:00 nodes.conf
-rw-r--r-- 1 root root 41550 Oct 17 07:37 redis.conf
[root@web3 7000]# redis-server redis.conf
启动好了,如今,再来检查一下集群:
redis-trib.rb check 127.0.0.1:7001
`[root@web3 7000]# redis-trib.rb check 127.0.0.1:7001`
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7002: OK
 Performing Cluster Check (using node 127.0.0.1:7001)
M: cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004
slots: (0 slots) slave
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
S: 30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005
slots: (0 slots) slave
replicates dfa0754c7854a874a6ebd2613b86140ad97701fc
M: d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000
slots: (0 slots) slave
replicates d2237fdcfbba672de766b913d1186cebcb6e1761
M: dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002
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.
你看,7000节点启动起来了,它却做为了 7003 的从节点了。

-----

de 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7003: OK
 Performing Cluster Check (using node 127.0.0.1:7006)
M: efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7006
slots: (0 slots) master
0 additional replica(s)
M: cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004
slots: (0 slots) slave
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
S: 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000
slots: (0 slots) slave
replicates d2237fdcfbba672de766b913d1186cebcb6e1761
M: dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005
slots: (0 slots) slave
replicates dfa0754c7854a874a6ebd2613b86140ad97701fc
M: d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003
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.
How many slots do you want to move (from 1 to 16384)?
它提示咱们须要迁移多少slot到7006上,咱们能够算一下:16384/4 = 4096,也就是说,
为了平衡分配起见,咱们须要移动4096个槽点到7006上。 那输入4096:
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID?
它又提示咱们,接受的node ID是多少,7006的id 咱们经过上面就能够看到是
efc3131fbdc6cf929720e0e0f7136cae85657481 :
What is the receiving node ID? efc3131fbdc6cf929720e0e0f7136cae85657481
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:
接着, redis-trib 会向你询问从新分片的源节点(source node), 也便是, 要从哪一个节
点中取出 4096 个哈希槽, 并将这些槽移动到7006节点上面。
若是咱们不打算从特定的节点上取出指定数量的哈希槽, 那么能够向 redis-trib 输入 all ,
这样的话, 集群中的全部主节点都会成为源节点, redis-trib 将从各个源节点中各取出一
部分哈希槽, 凑够 4096 个, 而后移动到7006节点上: Source node #1:all 接下来就开
始迁移了,而且会询问你是否确认:
Moving slot 1359 from d2237fdcfbba672de766b913d1186cebcb6e1761
Moving slot 1360 from d2237fdcfbba672de766b913d1186cebcb6e1761
Moving slot 1361 from d2237fdcfbba672de766b913d1186cebcb6e1761
Moving slot 1362 from d2237fdcfbba672de766b913d1186cebcb6e1761
Moving slot 1363 from d2237fdcfbba672de766b913d1186cebcb6e1761
Moving slot 1364 from d2237fdcfbba672de766b913d1186cebcb6e1761
Do you want to proceed with the proposed reshard plan (yes/no)?
输入 yes 并使用按下回车以后, redis-trib 就会正式开始执行从新分片操做, 将指定的哈
希槽从源节点一个个地移动到7006节点上面。
迁移完毕以后,咱们来检查下:
[root@web3 7006]# redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Performing Cluster Check (using node 127.0.0.1:7000)
S: 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000
slots: (0 slots) slave
replicates d2237fdcfbba672de766b913d1186cebcb6e1761
M: efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7006
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
S: 4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004
slots: (0 slots) slave
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
S: 30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005
slots: (0 slots) slave
replicates dfa0754c7854a874a6ebd2613b86140ad97701fc
M: d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002
slots:12288-16383 (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.
咱们着重看7006: 0-1364,5461-6826,10923-12287 (4096 slots) 这些原来在其余节点上
的slot呗迁移到了7006上。原来,它只是间隔的移动,并非衔接的总体移动,咱们看下
有数据了没?
[root@web3 7006]# redis-cli -c -p 7006
127.0.0.1:7006> keys *
1) "city"
2) "age"
3) "citylocate"
127.0.0.1:7006> get city
"shanghai"
很是赞,已经有数据了。
新建一个 7007从节点,做为7006的从节点 咱们再新建一个节点7007,步骤相似,就先省
略了。建好后,启动起来,咱们看如何把它加入到集群中的从节点中:
[root@web3 7007]# redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000
add-node的时候加上--slave表示是加入到从节点中,可是这样加,是随机的。这里的命令
行彻底像咱们在添加一个新主服务器时使用的同样,因此咱们没有指定要给哪一个主服务器添
加副本。这种状况下,redis-trib 会将7007做为一个具备较少副本的随机的主服务器的副
本。
那么,它会做为谁的从节点,应该是7006,由于7006尚未从节点。咱们运行下。
[root@web3 7007]# redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000
...
...
[OK] All 16384 slots covered.
Automatically selected master 127.0.0.1:7006
Connecting to node 127.0.0.1:7007: OK
Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster.
Waiting for the cluster to join.
Configure node as replica of 127.0.0.1:7006.
[OK] New node added correctly.
上面提示说,自动选择了7006做为master节点。而且成功了。咱们检查下:
[root@web3 7007]# redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7007: OK
Connecting to node 127.0.0.1:7002: OK
Performing Cluster Check (using node 127.0.0.1:7000)
S: 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000
slots: (0 slots) slave
replicates d2237fdcfbba672de766b913d1186cebcb6e1761
M: efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7006
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004
slots: (0 slots) slave
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
S: 30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005
slots: (0 slots) slave
replicates dfa0754c7854a874a6ebd2613b86140ad97701fc
M: d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: 86d05e7c2b197dc182b5e71069e791d033cf899e 127.0.0.1:7007
slots: (0 slots) slave
replicates efc3131fbdc6cf929720e0e0f7136cae85657481
M: dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002
slots:12288-16383 (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.
果真,7007加入到了7006的从节点当中。
若是想指定一个主节点行不行?固然能够。咱们再建一个7008节点。
redis-trib.rb add-node --slave --master-id
efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7008 127.0.0.1:7000
--master-id表示指定的主节点node id。这里指定的是 7006 这个主节点。
 Configure node as replica of 127.0.0.1:7006.
[OK] New node added correctly.
提示咱们已经做为7006的附属品,也就是加入到7006的从节点来了,照这么说,7006就有
2个从节点了,咱们看一下:
redis-cli -c -p 7008 cluster nodes |grep
efc3131fbdc6cf929720e0e0f7136cae85657481
86d05e7c2b197dc182b5e71069e791d033cf899e 127.0.0.1:7007 slave
efc3131fbdc6cf929720e0e0f7136cae85657481 0 1445089507786 8 connected
efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7006 master - 0 1445089508289
8 connected 0-1364 5461-6826 10923-12287
44321e7d619410dc4e0a8745366610a0d06d2395 127.0.0.1:7008 myself,slave
efc3131fbdc6cf929720e0e0f7136cae85657481 0 0 0 connected
咱们过滤了下看结果,果然,7007和7008是7006的从节点了。
咱们再作一个实验,我把7006的杀掉,看7007和7008谁会变成主节点:
[root@web3 7008]# ps -ef|grep redis
root 11384 1 0 09:56 ? 00:00:16 redis-server *:7001 [cluster]
root 11388 1 0 09:56 ? 00:00:16 redis-server *:7002 [cluster]
root 11392 1 0 09:56 ? 00:00:16 redis-server *:7003 [cluster]
root 11396 1 0 09:56 ? 00:00:15 redis-server *:7004 [cluster]
root 11400 1 0 09:56 ? 00:00:15 redis-server *:7005 [cluster]
root 12100 1 0 11:01 ? 00:00:11 redis-server *:7000 [cluster]
root 12132 1 0 11:28 ? 00:00:11 redis-server *:7006 [cluster]
root 12202 1 0 13:14 ? 00:00:02 redis-server *:7007 [cluster]
root 12219 1 0 13:39 ? 00:00:00 redis-server *:7008 [cluster]
root 12239 8259 0 13:49 pts/0 00:00:00 grep redis
[root@web3 7008]# kill 12132
[root@web3 7008]# redis-cli -c -p 7008
127.0.0.1:7008> get ss5rtr
Redirected to slot [1188] located at 127.0.0.1:7007
"66"
127.0.0.1:7007> cluster nodes
efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7006 master,fail -
1445089780668 1445089779963 8 disconnected
d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003 master - 0 1445089812195
7 connected 1365-5460
30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005 slave
dfa0754c7854a874a6ebd2613b86140ad97701fc 0 1445089813710 3 connected
86d05e7c2b197dc182b5e71069e791d033cf899e 127.0.0.1:7007 myself,master - 0 0 10
connected 0-1364 5461-6826 10923-12287
cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001 master - 0 1445089814214
2 connected 6827-10922
4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004 slave
cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 0 1445089812701 2 connected
44321e7d619410dc4e0a8745366610a0d06d2395 127.0.0.1:7008 slave
86d05e7c2b197dc182b5e71069e791d033cf899e 0 1445089814214 10 connected
3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000 slave
d2237fdcfbba672de766b913d1186cebcb6e1761 0 1445089813204 7 connected
dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002 master - 0 1445089813204
3 connected 12288-16383
127.0.0.1:7007>
7007得到了成为主节点的机会,7008就变成了7007的从节点。 那么这个时候,重启7006
节点,那么他就会变成了一个7007的从节点了。
127.0.0.1:7008> cluster nodes
30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005 slave
dfa0754c7854a874a6ebd2613b86140ad97701fc 0 1445089986148 3 connected
86d05e7c2b197dc182b5e71069e791d033cf899e 127.0.0.1:7007 master - 0
1445089986652 10 connected 0-1364 5461-6826 10923-12287
d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003 master - 0
1445089986148 7 connected 1365-5460
cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001 master - 0
1445089987155 2 connected 6827-10922
efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7006 slave
86d05e7c2b197dc182b5e71069e791d033cf899e 0 1445089985644 10 connected
44321e7d619410dc4e0a8745366610a0d06d2395 127.0.0.1:7008 myself,slave
86d05e7c2b197dc182b5e71069e791d033cf899e 0 0 0 connected
dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002 master - 0
1445089986652 3 connected 12288-16383
4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004 slave
cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 0 1445089987660 2 connected
3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000 slave
d2237fdcfbba672de766b913d1186cebcb6e1761 0 1445089985644 7 connected
127.0.0.1:7008>
移除一个主节点 有加确定有减,redis cluster一样支持移除节点功能,一样也是redistrib.rb的用法:
redis-trib del-node 127.0.0.1:7000 `<node-id>`
和新加节点有点不一样的是,移除须要节点的node-id。那咱们尝试将7007这个主节点移
除:
[root@web3 7006]# redis-trib.rb del-node 127.0.0.1:7000
86d05e7c2b197dc182b5e71069e791d033cf899e
Removing node 86d05e7c2b197dc182b5e71069e791d033cf899e from cluster
127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7007: OK
Connecting to node 127.0.0.1:7008: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7002: OK
[ERR] Node 127.0.0.1:7007 is not empty! Reshard data away and try again.
报错了,它提示咱们说,因为7007里面已经有数据了,不能被移除,要先将它的数据转移
出去。也就是说得从新分片,用上面增长新节点后的分片方式同样,用咱们再来一遍:
redis-trib.rb reshard 127.0.0.1:7000
因为中间太多内容,就省略不重要的,只简单说明下关键的几步:
M: 86d05e7c2b197dc182b5e71069e791d033cf899e 127.0.0.1:7007
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
How many slots do you want to move (from 1 to 16384)?
提示,咱们要分多少个槽点,因为7007上有4096个槽点,因此这里填写4096
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID?
提示咱们,须要移动到哪一个id上,那就填7001的吧:
What is the receiving node ID? cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
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:
这里就是关键了,它要咱们从哪一个节点去转移数据到7001,由于咱们是要删除7007的,所
以,咱们就得7007的id了
Source node #1:86d05e7c2b197dc182b5e71069e791d033cf899e
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
ok,这样就迁移好了。咱们看看7007是否为空了:
[root@web3 7006]# redis-cli -c -p 7007
127.0.0.1:7007> keys *
(empty list or set)
127.0.0.1:7007> cluster nodes
86d05e7c2b197dc182b5e71069e791d033cf899e 127.0.0.1:7007 myself,master - 0 0 10
connected
果真为空了,好。如今再进行移除节点操做:
[root@web3 7006]# redis-trib.rb del-node 127.0.0.1:7000
86d05e7c2b197dc182b5e71069e791d033cf899e
Removing node 86d05e7c2b197dc182b5e71069e791d033cf899e from cluster
127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7007: OK
Connecting to node 127.0.0.1:7008: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7002: OK
Sending CLUSTER FORGET messages to the cluster...
127.0.0.1:7006 as replica of 127.0.0.1:7001
127.0.0.1:7008 as replica of 127.0.0.1:7001
SHUTDOWN the node.
删除成功。
咱们再检查一下:7007 已经移除,连不上了。
[root@web3 7006]# redis-trib.rb check 127.0.0.1:7007
Connecting to node 127.0.0.1:7007: [ERR] Sorry, can't connect to node
127.0.0.1:7007
[root@web3 7006]# redis-trib.rb check 127.0.0.1:7008
Connecting to node 127.0.0.1:7008: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7000: OK
>>> Performing Cluster Check (using node 127.0.0.1:7008)
S: 44321e7d619410dc4e0a8745366610a0d06d2395 127.0.0.1:7008
slots: (0 slots) slave
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
S: 30858dbf483b61b9838d5c1f853a60beaa4e7afd 127.0.0.1:7005
slots: (0 slots) slave
replicates dfa0754c7854a874a6ebd2613b86140ad97701fc
M: d2237fdcfbba672de766b913d1186cebcb6e1761 127.0.0.1:7003
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c 127.0.0.1:7001
slots:0-1364,5461-12287 (8192 slots) master
3 additional replica(s)
S: efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7006
slots: (0 slots) slave
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
M: dfa0754c7854a874a6ebd2613b86140ad97701fc 127.0.0.1:7002
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: 4b4aef8b48c427a3c903518339d53b6447c58b93 127.0.0.1:7004
slots: (0 slots) slave
replicates cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c
S: 3707debcbe7be66d4a1968eaf3a5ffaf4308efa4 127.0.0.1:7000
slots: (0 slots) slave
replicates d2237fdcfbba672de766b913d1186cebcb6e1761
[OK] All nodes agree about slots configuration.
 Check for open slots...
 Check slots coverage...
[OK] All 16384 slots covered.
7006和7008果真也成功加入到了7001集群。
移除一个从节点 移除一个从节点就简单的多了,由于不须要考虑数据的迁移,咱们7008给
移除:
[root@web3 7006]# redis-trib.rb del-node 127.0.0.1:7008
44321e7d619410dc4e0a8745366610a0d06d2395
 Removing node 44321e7d619410dc4e0a8745366610a0d06d2395 from cluster
127.0.0.1:7005
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7008: OK
Connecting to node 127.0.0.1:7003: OK
 Sending CLUSTER FORGET messages to the cluster...
 SHUTDOWN the node.
[root@web3 7006]# redis-trib.rb check 127.0.0.1:7008
Connecting to node 127.0.0.1:7008: [ERR] Sorry, can't connect to node
127.0.0.1:7008
移除成功。
相关文章
相关标签/搜索