1. 程式人生 > >Redis叢集:Redis3.X環境搭建、查詢叢集資訊

Redis叢集:Redis3.X環境搭建、查詢叢集資訊

redis叢集

 

ruby環境

redis叢集管理工具redis-trib.rb依賴ruby環境,首先需要安裝ruby環境:

安裝ruby

yum install ruby

yum install rubygems

安裝ruby和redis的介面程式

拷貝redis-3.0.0.gem至/usr/local下

執行:

gem install /usr/local/redis-3.0.0.gem

 

 

建立叢集:

叢集結點規劃

這裡在同一臺伺服器用不同的埠表示不同的redis伺服器,如下:

主節點:192.168.101.3:7001 192.168.101.3:7002 192.168.101.3:7003

從節點:192.168.101.3:7004 192.168.101.3:7005 192.168.101.3:7006

在/usr/local下建立redis-cluster目錄,其下建立7001、7002...7006目錄,如下:

 

 

將redis安裝目錄bin下的檔案拷貝到每個700X目錄內,同時將redis原始碼目錄src下的redis-trib.rb拷貝到redis-cluster目錄下。

修改每個700X目錄下的redis.conf配置檔案:

port XXXX

bind 192.168.101.3

cluster-enabled yes 

啟動每個結點redis服務

分別進入7001、7002、...7006目錄,執行:

./redis-server ./redis.conf

檢視redis程序:

執行建立叢集命令

執行redis-trib.rb,此指令碼是ruby指令碼,它依賴ruby環境。

./redis-trib.rb create --replicas 1 192.168.101.3:7001 192.168.101.3:7002 192.168.101.3:7003 192.168.101.3:7004 192.168.101.3:7005  192.168.101.3:7006

說明:

redis叢集至少需要3個主節點,每個主節點有一個從節點總共6個節點

replicas指定為1表示每個主節點有一個從節點

注意:

如果執行時報如下錯誤:

[ERR] Node XXXXXX is not empty. 

Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

解決方法是刪除生成的配置檔案nodes.conf,如果不行則說明現在建立的結點包括了舊叢集的結點資訊,需要刪除redis的持久化檔案後再重啟redis,比如:appendonly.aof、dump.rdb

 

建立叢集輸出如下:

>>> Creating cluster

Connecting to node 192.168.101.3:7001: OK

Connecting to node 192.168.101.3:7002: OK

Connecting to node 192.168.101.3:7003: OK

Connecting to node 192.168.101.3:7004: OK

Connecting to node 192.168.101.3:7005: OK

Connecting to node 192.168.101.3:7006: OK

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.101.3:7001

192.168.101.3:7002

192.168.101.3:7003

Adding replica 192.168.101.3:7004 to 192.168.101.3:7001

Adding replica 192.168.101.3:7005 to 192.168.101.3:7002

Adding replica 192.168.101.3:7006 to 192.168.101.3:7003

M: cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 192.168.101.3:7001

   slots:0-5460 (5461 slots) master

M: 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 192.168.101.3:7002

   slots:5461-10922 (5462 slots) master

M: 1a8420896c3ff60b70c716e8480de8e50749ee65 192.168.101.3:7003

   slots:10923-16383 (5461 slots) master

S: 69d94b4963fd94f315fba2b9f12fae1278184fe8 192.168.101.3:7004

   replicates cad9f7413ec6842c971dbcc2c48b4ca959eb5db4

S: d2421a820cc23e17a01b597866fd0f750b698ac5 192.168.101.3:7005

   replicates 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841

S: 444e7bedbdfa40714ee55cd3086b8f0d5511fe54 192.168.101.3:7006

   replicates 1a8420896c3ff60b70c716e8480de8e50749ee65

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.101.3:7001)

M: cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 192.168.101.3:7001

   slots:0-5460 (5461 slots) master

M: 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 192.168.101.3:7002

   slots:5461-10922 (5462 slots) master

M: 1a8420896c3ff60b70c716e8480de8e50749ee65 192.168.101.3:7003

   slots:10923-16383 (5461 slots) master

M: 69d94b4963fd94f315fba2b9f12fae1278184fe8 192.168.101.3:7004

   slots: (0 slots) master

   replicates cad9f7413ec6842c971dbcc2c48b4ca959eb5db4

M: d2421a820cc23e17a01b597866fd0f750b698ac5 192.168.101.3:7005

   slots: (0 slots) master

   replicates 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841

M: 444e7bedbdfa40714ee55cd3086b8f0d5511fe54 192.168.101.3:7006

   slots: (0 slots) master

   replicates 1a8420896c3ff60b70c716e8480de8e50749ee65

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

查詢叢集資訊

叢集建立成功登陸任意redis節點查詢叢集中的節點情況。

客戶端以叢集方式登陸:

說明:

./redis-cli -c -h 192.168.101.3 -p 7001 

其中-c表示以叢集方式連線redis,-h指定ip地址,-p指定埠號

cluster nodes 查詢叢集結點資訊

cluster info 查詢叢集狀態資訊