redis叢集部署及常用的操作命令(上)

豬頭強發表於2015-07-20

簡單說下自己測試搭建簡單的redis叢集的大體步驟:

1.首先你的有6個redis(官方說最少6個,3master,3slave),可以先在一臺機器上搭建,搭建到多臺上應該只需要改變啟動命令即可(可能需要一些ssh無金鑰什麼的,只是猜測)

在網上隨便可以找到的配置多個redis,(總體有兩種方式,一種是虛擬的,貌似走的都是一個redis,一種是將配置好的redis複製成六份,配置相應的埠等,我選擇的後者顯得更真實一些)

 

然後把他們全部開啟,可以寫一個指令碼,或者一個一個開啟,作為一個新世紀的年輕人,我當然選擇一個一個開啟它們......

[root@localhost ~]# ps -ef | grep redis
root      3427     1  0 7月17 ?       00:02:59 src/redis-server *:6379
root      3454     1  0 7月17 ?       00:06:09 src/redis-server *:7001 [cluster]
root      3460     1  0 7月17 ?       00:06:19 src/redis-server *:7002 [cluster]
root      3467     1  0 7月17 ?       00:05:59 src/redis-server *:7003 [cluster]
root      3473     1  0 7月17 ?       00:05:59 src/redis-server *:7004 [cluster]
root      3477     1  0 7月17 ?       00:05:57 src/redis-server *:7005 [cluster]
root      5867     1  0 12:02 ?       00:00:06 src/redis-server *:7000 [cluster]
root      5938  5913  0 13:17 pts/1    00:00:00 grep --color=auto redis

2.將6個獨立redis設定為叢集,so easy 一句話搞定。其中 --replicas 引數是將6臺redis分別分配了主從關係(master掛掉slave可以頂替,但是還沒有具體深入研究)

./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

在開啟過程中可能會遇到很多問題,各種奇怪的報錯,那就要安裝ruby和gem的一些東東,搞不明白是什麼,反正就是依賴吧~ ,只能說按照這個來會繞開很多坑。

# yum install ruby-devel.x86_64

# wget http://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
# gem install -l ./redis-3.2.1.gem

執行這些程式碼之後再去執行上面建立叢集的命令,如果成功的話,會看到下面的樣子:

>>> 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: ad52e4f7c14da4b8b1e8e48603c9e5515a4cec7a 127.0.0.1:7000  
   slots:0-5460 (5461 slots) master  
M: d3058a19483d7be5c30c042779e56130f6ebf074 127.0.0.1:7001  
   slots:5461-10922 (5462 slots) master  
M: 5814305e71552ab45c44b9b8233681a63c3a57b1 127.0.0.1:7002  
   slots:10923-16383 (5461 slots) master  
S: e50676757acbe7c6a21e8abf4eda26ababb08285 127.0.0.1:7003  
   replicates ad52e4f7c14da4b8b1e8e48603c9e5515a4cec7a  
S: ab307ba26d6dabe8edb2f2a7287be6f01aa46d88 127.0.0.1:7004  
   replicates d3058a19483d7be5c30c042779e56130f6ebf074  
S: 11174332eb6ad40c0327750536fa776d706caf85 127.0.0.1:7005  
   replicates 5814305e71552ab45c44b9b8233681a63c3a57b1  
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: ad52e4f7c14da4b8b1e8e48603c9e5515a4cec7a 127.0.0.1:7000  
   slots:0-5460 (5461 slots) master  
M: d3058a19483d7be5c30c042779e56130f6ebf074 127.0.0.1:7001  
   slots:5461-10922 (5462 slots) master  
M: 5814305e71552ab45c44b9b8233681a63c3a57b1 127.0.0.1:7002  
   slots:10923-16383 (5461 slots) master  
M: e50676757acbe7c6a21e8abf4eda26ababb08285 127.0.0.1:7003  
   slots: (0 slots) master  
   replicates ad52e4f7c14da4b8b1e8e48603c9e5515a4cec7a  
M: ab307ba26d6dabe8edb2f2a7287be6f01aa46d88 127.0.0.1:7004  
   slots: (0 slots) master  
   replicates d3058a19483d7be5c30c042779e56130f6ebf074  
M: 11174332eb6ad40c0327750536fa776d706caf85 127.0.0.1:7005  
   slots: (0 slots) master  
   replicates 5814305e71552ab45c44b9b8233681a63c3a57b1  
<span style="color:#009900;">[OK] All nodes agree about slots configuration.</span>  
>>> Check for open slots...  
>>> Check slots coverage...  
<span style="color:#009900;">[OK] All 16384 slots covered.</span></span>  

3.測試叢集。

可以用下面的命令檢視叢集的狀態,從下面可以看出有4個master,2個slave~~~,這個是由於我太年輕kill了7000的redis又加入了進來,原先他的slave 7003 也變成了master頂替了他。

[root@localhost src]# ./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:7005: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7004: OK
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 4eb95ab689288dbb3ebf7ed380a0593ec9daa878 127.0.0.1:7000
   slots: (0 slots) master
   0 additional replica(s)
S: 11174332eb6ad40c0327750536fa776d706caf85 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 5814305e71552ab45c44b9b8233681a63c3a57b1
M: e50676757acbe7c6a21e8abf4eda26ababb08285 127.0.0.1:7003
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
M: 5814305e71552ab45c44b9b8233681a63c3a57b1 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: d3058a19483d7be5c30c042779e56130f6ebf074 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: ab307ba26d6dabe8edb2f2a7287be6f01aa46d88 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d3058a19483d7be5c30c042779e56130f6ebf074
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可以進入redis中玩一玩:查了一下使用redis-cli命令的時候應該加上 -c ,意思是在叢集模式下,否則你會以一種獨立的redis進入,然後各種moved報錯。

看著儲存位置跳來跳去,貌似簡單的叢集也就搭建完成了。

[root@localhost src]# ./redis-cli -c -p 7000
127.0.0.1:7000> set qiang weikang
-> Redirected to slot [1357] located at 127.0.0.1:7003
OK
127.0.0.1:7003> set qiang qiang 
OK
127.0.0.1:7003> set weikang qiang
-> Redirected to slot [12142] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get qiang
-> Redirected to slot [1357] located at 127.0.0.1:7003
"qiang"
127.0.0.1:7003> get weikang
-> Redirected to slot [12142] located at 127.0.0.1:7002
"qiang"

4.遺留的一些問題:

(1)搭建的叢集連線上客戶端後,所有的redis只有db0可用,不知道是什麼原因。(難道是互相之間只連線了db0嗎?)

(2)增刪改節點還有待繼續研究,不能只是單純的寫命令就可以了,要理解在什麼場景下應該做如何的處理。

(3)叢集管理工具在redis 3.0之後自己出了一個叫做redis-sentinel的,這個需要著重研究。

 

只有在深入理解的叢集的意義和所配置的每個引數的含義之後,才能使用到生產環境中,以上只是個入門,學習redis叢集才只是剛剛開始~~

相關文章