1. 程式人生 > >Redis的叢集建立步驟(非redis-trib.rb指令碼方式)

Redis的叢集建立步驟(非redis-trib.rb指令碼方式)

本文目的僅用於測試驗證建立叢集的相關步驟,所以只是採用最簡單的配置在同一個伺服器部署了由3個節點組成的Redis叢集

1. 建立叢集步驟,本文使用Redis 3.2.5版本

1.1 建立配置檔案目錄,使用的埠分別為5000,5001和5002


[[email protected] ~]# cd /opt/redis-3.2.5
[[email protected] redis-3.2.5]# mkdir 5000 5001 5002

1.2 分別在上述建立的目錄建立對應的配置檔案

[[email protected] redis-3.2.5]# cp redis.conf 5000/redis_5000.conf
[
[email protected]
redis-3.2.5]# cp redis.conf 5001/redis_5001.conf
[[email protected] redis-3.2.5]# cp redis.conf 5002/redis_5002.conf

1.3 分別修改各個配置檔案使用對應的埠,日誌檔案,持久化檔案等等,例如redis_5000.conf


port 5000
pidfile /opt/redis-3.2.5/5000/redis_5000.pid
logfile "redis_5000.log"
dbfilename dump_5000.rdb
appendonly yes
appendfilename "appendonly_5000.aof"
cluster-enabled yes
cluster-config-file nodes_5000.conf

1.4 分別啟動各個Redis instance


[[email protected] redis-3.2.5]# cd 5000
[[email protected] 5000]# ../src/redis-server ./redis_5000.conf &

[[email protected] redis-3.2.5]# cd 5001
[[email protected] 5001]# ../src/redis-server ./redis_5001.conf &

[[email protected] redis-3.2.5]# cd 5002
[[email protected] 5002]# ../src/redis-server ./redis_5002.conf &

1.5 檢視自動生成的叢集配置檔案,日誌檔案和程序,如果有類似以下資訊則代表各個Redis instance啟動成功(下面省略部分無關資訊)


[[email protected] 5000]# cat nodes_5000.conf 
2c192963bfbd4a42127b99dbeeb09f207b868413 :0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0

[[email protected] 5000]# cat redis_5000.log 
16857:M 21 Nov 11:39:05.328 * No cluster configuration found, I'm 2c192963bfbd4a42127b99dbeeb09f207b868413
16857:M 21 Nov 11:39:05.343 # Server started, Redis version 3.2.5
16857:M 21 Nov 11:39:05.345 * The server is now ready to accept connections on port 5000

[[email protected] 5000]# ps -ef|grep redis
root     16857 13864  0 11:39 pts/0    00:00:21 ../src/redis-server 127.0.0.1:5000 [cluster]
root     24860 13864  0 14:26 pts/0    00:00:00 ../src/redis-server 127.0.0.1:5001 [cluster]
root     24935 13864  0 14:27 pts/0    00:00:00 ../src/redis-server 127.0.0.1:5002 [cluster]

1.6 分別在各個Redis instance新增hash slots(這裡是平均分配)


[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5000 CLUSTER ADDSLOTS {0..5460}
[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5001 CLUSTER ADDSLOTS {5461..10922}
[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5002 CLUSTER ADDSLOTS {10923..16383}

1.7 分別在各個Redis instance執行SET-CONFIG-EPOCH

[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5000 CLUSTER SET-CONFIG-EPOCH 1
[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5001 CLUSTER SET-CONFIG-EPOCH 2
[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5002 CLUSTER SET-CONFIG-EPOCH 3

1.8 連線叢集,在一個Redis instance執行就OK了

[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5000 CLUSTER MEET 127.0.0.1 5001
[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5000 CLUSTER MEET 127.0.0.1 5002

1.9 檢視叢集狀態資訊

[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5000 CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:32
cluster_stats_messages_received:32

[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5000 CLUSTER NODES
2c192963bfbd4a42127b99dbeeb09f207b868413 127.0.0.1:5000 myself,master - 0 0 1 connected 0-5460
be67d8e4ca455d4b28af83f1e9646e94d8897cad 127.0.0.1:5002 master - 0 1479714113782 3 connected 10923-16383
cae1ea01c446406ef231e85529283f3751ded23a 127.0.0.1:5001 master - 0 1479714114788 2 connected 5461-10922

1.10 驗證讀寫資料

[[email protected] redis-3.2.5]# ./src/redis-cli -c -p 5000
127.0.0.1:5000> dbsize
(integer) 0
127.0.0.1:5000> set key1 value1
-> Redirected to slot [9189] located at 127.0.0.1:5001
OK
127.0.0.1:5001> get key1
"value1"

以上是使用命令列搭建Redis叢集的步驟,或者你可以使用redis-trib.rb指令碼簡化上述步驟