1. 創(chuàng)建目錄
要讓集群正常運(yùn)作至少需要三個主節(jié)點(diǎn), 不過在剛開始試用集群功能時, 強(qiáng)烈建議使用六個節(jié)點(diǎn): 其中三個為主節(jié)點(diǎn), 而其余三個則是各個主節(jié)點(diǎn)的從節(jié)點(diǎn)
創(chuàng)建一個新目錄 redis,并在其中創(chuàng)建六個以端口號為名字的子目錄,每個子目錄都是一個 redis
mkdir redis
cd redis
mkdir 7000 7001 7002 7003 7004 7005
2. 修改配置文件
分別進(jìn)入每個子目錄,修改 redis.conf 中如下的配置項(xiàng)
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
其中端口號 port 分別為 7000-7005
3. 運(yùn)行實(shí)例
接著,從 Redis Github 頁面的 unstable 分支中下載最新代碼,然后編譯出可執(zhí)行文件放在 redis 文件夾中。(其實(shí)從我們的任一子目錄里面編譯后在 src 文件夾里拿出來也可以)通過如下命令分別創(chuàng)建出 6 個實(shí)例
cd 7000
../redis-server ./redis.conf
4. 創(chuàng)建集群
可以使用 src 目錄中的 redis-trib 程序來創(chuàng)建集群,首先需要安裝 redis gem 才能運(yùn)行 redis-trib
gem install redis
然后創(chuàng)建集群
./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
然后終端會列出集群的情況,并禮貌性的問你這樣創(chuàng)建可以嗎,當(dāng)然是輸入 yes
>>> Creating cluster
>>> 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: 968f13cefb4f895cec739794835fcd160bde22af 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 3c088766a36ed8ccd7bef5e6e2fbe75c48bc10c0 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: 433145fbee24fa60a3e7194b9b8056b02f375023 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
S: d29b5ce6e4ea9a33a9044d4211ba37aafa5198f8 127.0.0.1:7003
replicates 968f13cefb4f895cec739794835fcd160bde22af
S: ba9052a112e11e757a563ce6ed89adde463c7ec5 127.0.0.1:7004
replicates 3c088766a36ed8ccd7bef5e6e2fbe75c48bc10c0
S: 130bfe6c6e1b8d31ed1e030ae7c41902be268a1f 127.0.0.1:7005
replicates 433145fbee24fa60a3e7194b9b8056b02f375023
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: 968f13cefb4f895cec739794835fcd160bde22af 127.0.0.1:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 433145fbee24fa60a3e7194b9b8056b02f375023 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 3c088766a36ed8ccd7bef5e6e2fbe75c48bc10c0 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: ba9052a112e11e757a563ce6ed89adde463c7ec5 127.0.0.1:7004
slots: (0 slots) slave
replicates 3c088766a36ed8ccd7bef5e6e2fbe75c48bc10c0
S: d29b5ce6e4ea9a33a9044d4211ba37aafa5198f8 127.0.0.1:7003
slots: (0 slots) slave
replicates 968f13cefb4f895cec739794835fcd160bde22af
S: 130bfe6c6e1b8d31ed1e030ae7c41902be268a1f 127.0.0.1:7005
slots: (0 slots) slave
replicates 433145fbee24fa60a3e7194b9b8056b02f375023
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到此集群就創(chuàng)建成功了。
但很明顯這是個偽集群,如果條件允許的話建議使用 6 臺機(jī)器來創(chuàng)建集群,只需修改相應(yīng)的地址和端口,并在防火墻開放該端口就行。
5. 簡單測試
使用 redis-cli 進(jìn)行簡單的命令測試
$ redis-cli -c -p 7000
redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"