|
集群架構
(1)所有的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬.
(2)節點的fail是通過集群中超過半數的節點檢測失效時才生效.
(3)客戶端與redis節點直連,不需要中間proxy層.客戶端不需要連接集群所有節點,連接集群中任何一個可用節點即可
(4)redis-cluster把所有的物理節點映射到[0-16383]slot(插槽)上,cluster 負責維護node<->slot<->value
准備環境
創建一個目錄,存放集群的配置文件
[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剛創建好的三個目錄中
[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.conf
1、將端口分別設置為:6380、6381、6382
2、設置pidfile文件為不同的路徑
3、開啟集群,cluster-enabled yes
4、指定集群的配置文件,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 redis
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配置文件中的bind
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腳本可能遇到的問題
1、/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
2、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
3、/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
書生參考於網絡整理
|
