重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
這篇文章給大家分享的是手動創(chuàng)建與自動創(chuàng)建redis集群的方法,相信大部分人都還沒學(xué)會這個技能,為了讓大家學(xué)會,給大家總結(jié)了以下內(nèi)容,話不多說,一起往下看吧。
公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出寧陜免費(fèi)做網(wǎng)站回饋大家。
環(huán)境描述:一臺機(jī)器啟動六個節(jié)點(diǎn),3個主節(jié)點(diǎn),3個從節(jié)點(diǎn)。
安裝: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.conf
**要改的地方**
daemonize yes
port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf . //這個編號最好是等于你的端口號
cluster-node-timeout 5000
mkdir /usr/local/cluster-test/ //同時復(fù)制配置文件,且修改端口及參數(shù)
**在/usr/local/cluster-test/下創(chuàng)建**
mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005
啟動服務(wù):
./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 7000
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
給三個主節(jié)點(diǎn)分配槽點(diǎn):
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}
設(shè)置從節(jié)點(diǎn):[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
環(huán)境描述:一臺機(jī)器啟動六個節(jié)點(diǎn),3個主節(jié)點(diǎn),3個從節(jié)點(diǎn)。
安裝: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.conf
daemonize yes
port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf . //這個編號最好是等于你的端口號
cluster-node-timeout 5000
appendonly yes
mkdir /usr/local/cluster-test/ //同時復(fù)制配置文件,且修改端口及參數(shù)
mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005
啟動服務(wù):
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個節(jié)點(diǎn)連在一起構(gòu)成集群
需要用到的命令就是redis-trib.rb,這是官方的一個用ruby寫的一個操作redis cluster的命
令,所以,你的機(jī)器上需要安裝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
因?yàn)槲覀円陆? 所以這里使用create命令. --replicas 1 參數(shù)表示為每個主節(jié)點(diǎn)創(chuàng)建一
個從節(jié)點(diǎn). 其他參數(shù)是實(shí)例的地址集合。
[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接受. 集群就被配置和加入了, 意思是, 實(shí)例
會經(jīng)過互相交流后啟動。
測試集群的狀態(tài):[root@localhost 7000]# redis-trib.rb check 127.0.0.1:7000
可以看到有3個主節(jié)點(diǎn),3個從節(jié)點(diǎn)。每個節(jié)點(diǎn)都是成功的連接狀態(tài)
3個主節(jié)點(diǎn)[M]是:
7000 (3707debcbe7be66d4a1968eaf3a5ffaf4308efa4)
7001 (cb5c04b6160c3b7e18cad5d49d8e2987b27e0d6c)
7002 (dfa0754c7854a874a6ebd2613b86140ad97701fc)
3個從節(jié)點(diǎn)[S]是:
7003 (d2237fdcfbba672de766b913d1186cebcb6e1761)->7000
7004 (4b4aef8b48c427a3c903518339d53b6447c58b93)->7001
7005 (30858dbf483b61b9838d5c1f853a60beaa4e7afd) ->7002
測試連接集群
剛才集群搭建成功了。按照redis cluster的特點(diǎn),它是去中心化,每個節(jié)點(diǎn)都是對等的,所
以,你連接哪個節(jié)點(diǎn)都可以獲取和設(shè)置數(shù)據(jù),我們來試一下。 redis-cli是redis默認(rèn)的客戶
端工具,啟動時加上-c參數(shù),就可以連接到集群。 連接任意一個節(jié)點(diǎn)端口:[root@web3 7000]# redis-cli -c -p 7000
127.0.0.1:7000>
設(shè)置一個值:
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算法,
來計(jì)算,將這個key放到哪個節(jié)點(diǎn),這里分配到了12803 slot 就分配到了7002(10923-
16383)這個節(jié)點(diǎn)上。
Redirected to slot [12803] located at 127.0.0.1:7002
redis cluster 采用的方式很直接,它直接跳轉(zhuǎn)到7002節(jié)點(diǎn)了,而不是還在自身的7000節(jié)
點(diǎn)。 現(xiàn)在我們連接7005這個從節(jié)點(diǎn):
[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的值,它同樣也是跳轉(zhuǎn)到了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.~~~~
可以看出,數(shù)據(jù)都會在7000-7002 這3個主節(jié)點(diǎn)來跳轉(zhuǎn)存儲。
測試集群中的節(jié)點(diǎn)掛掉
上面我們建立來了一個集群。3個主節(jié)點(diǎn)[7000-7002]提供數(shù)據(jù)存粗和讀取,3個從節(jié)點(diǎn)
[7003-7005]則是負(fù)責(zé)把[7000-7002]的數(shù)據(jù)同步到自己的節(jié)點(diǎn)上來,我們來看一下
[7003-7005]的appendonly.aof的內(nèi)容。看看是不是不這樣:
`[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節(jié)點(diǎn)上同步過來的數(shù)據(jù),7004,7005也是。
下面,我們先來模擬其中一臺Master主服務(wù)器掛掉的情況。
`[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主節(jié)點(diǎn)掛掉了,那么這個時候,7000的從節(jié)點(diǎn)只有7003一個,
肯定7003就會被選舉稱Master節(jié)點(diǎn)了:
`[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節(jié)點(diǎn)了,果真,7003被選取成了替代7000成為主節(jié)點(diǎn)了。那我們
來獲取原先存在7000節(jié)點(diǎn)的數(shù)據(jù):
`[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>
數(shù)據(jù)果真沒有丟失,而是從7003上面獲取了。
我們再來模擬 7000節(jié)點(diǎn)重新啟動了的情況,那么它還會自動加入到集群中嗎?那么,7000
這個節(jié)點(diǎn)上充當(dāng)什么角色呢? 我們試一下: 重新啟動 7000 節(jié)點(diǎn):
[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
啟動好了,現(xiàn)在,再來檢查一下集群:
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節(jié)點(diǎn)啟動起來了,它卻作為了 7003 的從節(jié)點(diǎn)了。
-----
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個槽點(diǎn)到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 會向你詢問重新分片的源節(jié)點(diǎn)(source node), 也即是, 要從哪個節(jié)
點(diǎn)中取出 4096 個哈希槽, 并將這些槽移動到7006節(jié)點(diǎn)上面。
如果我們不打算從特定的節(jié)點(diǎn)上取出指定數(shù)量的哈希槽, 那么可以向 redis-trib 輸入 all ,
這樣的話, 集群中的所有主節(jié)點(diǎn)都會成為源節(jié)點(diǎn), redis-trib 將從各個源節(jié)點(diǎn)中各取出一
部分哈希槽, 湊夠 4096 個, 然后移動到7006節(jié)點(diǎn)上: Source node #1:all 接下來就開
始遷移了,并且會詢問你是否確認(rèn):
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 就會正式開始執(zhí)行重新分片操作, 將指定的哈
希槽從源節(jié)點(diǎn)一個個地移動到7006節(jié)點(diǎn)上面。
遷移完畢之后,我們來檢查下:
[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) 這些原來在其他節(jié)點(diǎn)上
的slot唄遷移到了7006上。原來,它只是間隔的移動,并不是銜接的整體移動,我們看下
有數(shù)據(jù)了沒?
[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"
非常贊,已經(jīng)有數(shù)據(jù)了。
新建一個 7007從節(jié)點(diǎn),作為7006的從節(jié)點(diǎn) 我們再新建一個節(jié)點(diǎn)7007,步驟類似,就先省
略了。建好后,啟動起來,我們看如何把它加入到集群中的從節(jié)點(diǎn)中:
[root@web3 7007]# redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000
add-node的時候加上--slave表示是加入到從節(jié)點(diǎn)中,但是這樣加,是隨機(jī)的。這里的命令
行完全像我們在添加一個新主服務(wù)器時使用的一樣,所以我們沒有指定要給哪個主服務(wù)器添
加副本。這種情況下,redis-trib 會將7007作為一個具有較少副本的隨機(jī)的主服務(wù)器的副
本。
那么,它會作為誰的從節(jié)點(diǎn),應(yīng)該是7006,因?yàn)?006還沒有從節(jié)點(diǎn)。我們運(yùn)行下。
[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節(jié)點(diǎn)。并且成功了。我們檢查下:
[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的從節(jié)點(diǎn)當(dāng)中。
如果想指定一個主節(jié)點(diǎn)行不行?當(dāng)然可以。我們再建一個7008節(jié)點(diǎn)。
redis-trib.rb add-node --slave --master-id
efc3131fbdc6cf929720e0e0f7136cae85657481 127.0.0.1:7008 127.0.0.1:7000
--master-id表示指定的主節(jié)點(diǎn)node id。這里指定的是 7006 這個主節(jié)點(diǎn)。
Configure node as replica of 127.0.0.1:7006.
[OK] New node added correctly.
提示我們已經(jīng)作為7006的附屬品,也就是加入到7006的從節(jié)點(diǎn)來了,照這么說,7006就有
2個從節(jié)點(diǎn)了,我們看一下:
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
我們過濾了下看結(jié)果,果真,7007和7008是7006的從節(jié)點(diǎn)了。
我們再做一個實(shí)驗(yàn),我把7006的殺掉,看7007和7008誰會變成主節(jié)點(diǎn):
[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獲得了成為主節(jié)點(diǎn)的機(jī)會,7008就變成了7007的從節(jié)點(diǎn)。 那么這個時候,重啟7006
節(jié)點(diǎn),那么他就會變成了一個7007的從節(jié)點(diǎn)了。
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>
移除一個主節(jié)點(diǎn) 有加肯定有減,redis cluster同樣支持移除節(jié)點(diǎn)功能,同樣也是redistrib.rb的用法:
redis-trib del-node 127.0.0.1:7000 ``
和新加節(jié)點(diǎn)有點(diǎn)不同的是,移除需要節(jié)點(diǎn)的node-id。那我們嘗試將7007這個主節(jié)點(diǎn)移
除:
[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.
報(bào)錯了,它提示我們說,由于7007里面已經(jīng)有數(shù)據(jù)了,不能被移除,要先將它的數(shù)據(jù)轉(zhuǎn)移
出去。也就是說得重新分片,用上面增加新節(jié)點(diǎn)后的分片方式一樣,用我們再來一遍:
redis-trib.rb reshard 127.0.0.1:7000
由于中間太多內(nèi)容,就省略不重要的,只簡單說明下關(guān)鍵的幾步:
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)?
提示,我們要分多少個槽點(diǎn),由于7007上有4096個槽點(diǎn),所以這里填寫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:
這里就是關(guān)鍵了,它要我們從哪個節(jié)點(diǎn)去轉(zhuǎn)移數(shù)據(jù)到7001,因?yàn)槲覀兪且獎h除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
果然為空了,好。現(xiàn)在再進(jìn)行移除節(jié)點(diǎn)操作:
[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 已經(jīng)移除,連不上了。
[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集群。
移除一個從節(jié)點(diǎn) 移除一個從節(jié)點(diǎn)就簡單的多了,因?yàn)椴恍枰紤]數(shù)據(jù)的遷移,我們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
移除成功。
以上就是手動創(chuàng)建與自動創(chuàng)建redis集群方法的具體介紹,內(nèi)容較為全面,而且我也相信有相當(dāng)?shù)囊恍┕ぞ呖赡苁俏覀內(nèi)粘9ぷ骺赡軙姷交蛴玫降摹Mㄟ^這篇文章,希望你能收獲更多。