集羣架構
(1)全部的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬.html
(2)節點的fail是經過集羣中超過半數的節點檢測失效時才生效.
(3)客戶端與redis節點直連,不須要中間proxy層.客戶端不須要鏈接集羣全部節點,鏈接集羣中任何一個可用節點便可
(4)redis-cluster把全部的物理節點映射到[0-16383]slot(插槽)上,cluster 負責維護node<->slot<->value
準備環境node
建立一個目錄,存放集羣的配置文件
[root@master redis]# mkdir redis-cluster
[root@master redis]# cd redis-cluster/
[root@master redis-cluster]# mkdir 6380
[root@master redis-cluster]# mkdir 6381
[root@master redis-cluster]# mkdir 6382
[root@master redis-cluster]# pwd
/opt/redis/redis-cluster
拷貝安裝源碼中的redis.conf剛建立好的三個目錄中redis
[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6380/
[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6381/
[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6382/
分別進入這三個目錄,修改配置文件redis.confvim
一、將端口分別設置爲:6380、638一、6382
二、設置pidfile文件爲不一樣的路徑
三、開啓集羣,cluster-enabled yes
四、指定集羣的配置文件,cluster-config-file "nodes-xxxx.conf"
以6380爲例:
[root@master redis-master-slave]# vim 6380/redis.conf
啓動redis-server
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6380/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6381/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6382/redis.conf
[root@master redis-cluster]# ps -ef | grep redisruby
root 2948 1 0 11:11 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6380 [cluster]
root 2970 1 0 11:16 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6381 [cluster]
root 2974 1 0 11:16 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6382 [cluster]
建立redis集羣服務器
建立集羣要使用redis-trib.rb腳本,執行此腳本查看使用方法
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
del-node host:port node_id
info host:port
rebalance host:port
--timeout <arg>
--auto-weights
--pipeline <arg>
--threshold <arg>
--weight <arg>
--simulate
--use-empty-masters
check host:port
reshard host:port
--timeout <arg>
--to <arg>
--pipeline <arg>
--from <arg>
--slots <arg>
--yes
call host:port command arg arg .. arg
import host:port
--copy
--from <arg>
--replace
help (show this help)
set-timeout host:port milliseconds
create host1:port1 ... hostN:portN
--replicas <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
fix host:port
--timeout <arg>
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
使用redis-trib.rb create建立集羣,--replicas 0:指定了從數據的數量爲0網絡
注意:這裏不能使用127.0.0.1,不然在Jedis客戶端使用時沒法鏈接到!
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb create --replicas 0 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382架構
>>> Creating cluster
[ERR] Sorry, can't connect to node 192.168.56.101:6380
修改剛建立的三個redis.conf配置文件中的bindoop
bind 192.168.56.101
重啓redis-server測試
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6380/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6381/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6382/redis.conf
[root@master redis-cluster]# ps -ef | grep redis
root 3756 1 0 13:11 ? 00:00:00 /usr/local/bin/redis-server 192.168.56.101:6380 [cluster]
root 3760 1 0 13:11 ? 00:00:00 /usr/local/bin/redis-server 192.168.56.101:6381 [cluster]
root 3764 1 0 13:11 ? 00:00:00 /usr/local/bin/redis-server 192.168.56.101:6382 [cluster]
再建立集羣
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb create --replicas 0 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382
>>> Creating cluster
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
192.168.56.101:6380
192.168.56.101:6381
192.168.56.101:6382
M: 8c45a68a798aed7c1ed5e636f2899709717952c8 192.168.56.101:6380
slots:0-5460 (5461 slots) master
M: c9267b81af4bb2c186d07c5ebc777a4b8551069a 192.168.56.101:6381
slots:5461-10922 (5462 slots) master
M: 16c6db308b37be4d4dd337cd62ea1b676672e096 192.168.56.101:6382
slots:10923-16383 (5461 slots) master
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 192.168.56.101:6380)
M: 8c45a68a798aed7c1ed5e636f2899709717952c8 192.168.56.101:6380
slots:0-5460 (5461 slots) master
M: c9267b81af4bb2c186d07c5ebc777a4b8551069a 192.168.56.101:6381
slots:5461-10922 (5462 slots) master
M: 16c6db308b37be4d4dd337cd62ea1b676672e096 192.168.56.101:6382
slots:10923-16383 (5461 slots) master【集羣及槽信息】
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
測試集羣
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380
192.168.56.101:6380> keys *
(empty list or set)
192.168.56.101:6380> set k1 123
(error) MOVED 12706 192.168.56.101:6382
由於k1的hash槽信息是在6382上,如今使用redis-cli鏈接的6380,沒法完成set操做,須要客戶端跟蹤重定向,使用-c參數
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380 -c
192.168.56.101:6380> set k1 123
-> Redirected to slot [12706] located at 192.168.56.101:6382
OK
獲取數據
k1的hash槽信息在6382上,在6382上能夠直接獲取,在其餘節點仍是要重定向去獲取
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380 -c
192.168.56.101:6380> get k1
-> Redirected to slot [12706] located at 192.168.56.101:6382
"123"
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6382 -c
192.168.56.101:6382> get k1
"123"
執行redis-trib.rb腳本可能遇到的問題
一、/usr/bin/env: ruby: 沒有那個文件或目錄
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/usr/bin/env: ruby: 沒有那個文件或目錄
解決方案:因爲redis-trib.rb是用ruby語言編寫,因此須要安裝ruby環境,這裏推薦使用yum install ruby
二、redis-trib.rb:24:in `require': no such file to load -- rubygems
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/opt/redis/redis-3.2.1/src/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from /opt/redis/redis-3.2.1/src/redis-trib.rb:24
解決方案:yum install rubygems
三、/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /opt/redis/redis-3.2.1/src/redis-trib.rb:25
解決方案:缺乏redis的接口,使用gem install redis或gem install redis --version 3.2.1
[root@master redis-cluster]# gem install redis --version 3.2.1
Successfully installed redis-3.2.1
1 gem installed
Installing ri documentation for redis-3.2.1...
Installing RDoc documentation for redis-3.2.1...
備註:
若是鏈接不上gem服務器安裝,就手動下載並安裝
gem install redis --version 3.0.0
ERROR: Could not find a valid gem 'redis' (= 3.0.0) in any repository
ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)
須要手工下載並安裝:
wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
gem install -l ./redis-3.2.1.gem
書生參考於網絡整理
|