工作之余學(xué)習(xí)代碼,視頻里講到單機(jī)多節(jié)點(diǎn)redis集群部署,但是有一個(gè)問(wèn)題就是如果機(jī)器掛掉了那么集群也掛掉了。于是自己就改了一下,改成了多機(jī)多集群部署。
先說(shuō)說(shuō)遇到的坑,解決辦法會(huì)在文章最后給出(因?yàn)槔锩嬗行┟钚枰渲昧酥蟛拍苡茫杏X(jué)能踩的坑都讓我踩遍了:
1、搭集群時(shí)需要使用到ruby腳本,但是使用yum -y install ruby之后,運(yùn)行g(shù)em install redis-3.2.2.gem會(huì)出現(xiàn)錯(cuò)誤:
ERROR: Error installing redis-4.0.0.gem:
redis requires Ruby version >= 2.2.2.
2、redis創(chuàng)建集群失敗,原因是redis.conf文件中的 bind 配置錯(cuò)誤:
can't connect to node 192.168.*.*
3、redis創(chuàng)建集群時(shí)顯示錯(cuò)誤:
[ERR] Node 192.168.186.91:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
4、redis創(chuàng)建集群時(shí)無(wú)錯(cuò)誤,但是一直處于Waiting for the cluster to join ……狀態(tài)非常久,使用cluster meet ip port命令無(wú)效
現(xiàn)在開(kāi)始部署步驟
1、visualbox搭建服務(wù)器集群
2、在每臺(tái)機(jī)器上安裝redis實(shí)例和ruby工具
3、把redis-trib.rb移動(dòng)到/usr/local/bin目錄中
redis-trib.rb都在redis的源碼包中的src目錄中
4、配置每個(gè)節(jié)點(diǎn)的redis.conf文件
要部署幾個(gè)redis實(shí)例就需要幾個(gè)redis.conf文件,這里需要修改每個(gè)redis的配置redis.conf文件
bind 192.168.56.103 這里的IP改成當(dāng)前redis實(shí)例所在機(jī)器的IP port 7003 這里的port改成當(dāng)前redis實(shí)例通信的port pidfile /var/run/redis_7003.pid 這個(gè)隨便什么名字都可以,不過(guò)每個(gè)配置文件里的一定不能相同,我改成了port標(biāo)識(shí)的 cluster-enabled yes 一定要改,開(kāi)啟redis的集群功能 cluster-config-file nodes-7003.conf 這個(gè)隨便什么名字都可以,不過(guò)每個(gè)配置文件里的一定不能相同,我改成了port標(biāo)識(shí)的
5、iptables和firewall配置
需要在iptables或者firewall中添加redis的通信端口和redis總線端口,iptables和firewall建議只用一個(gè),如果不想用的話關(guān)閉就行了,省去一部分麻煩
6、接下來(lái)啟動(dòng)所有的redis實(shí)例,進(jìn)行集群“會(huì)面”了。連接不同的節(jié)點(diǎn),激動(dòng)人心的時(shí)刻到了
請(qǐng)不要照抄,依次類(lèi)比你自己的IP和redis端口,修改即可
redis-trib.rb create --replicas 1 192.168.56.102:7001 192.168.56.102:7002 192.168.56.103:7003 192.168.56.103:7004 192.168.56.104:7005 192.168.56.104:7006
會(huì)出現(xiàn)以下提示,在這里Can I set the above configuration (type 'yes' to accept): yes
請(qǐng)輸入yes
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.56.102:7001 192.168.56.103:7003 192.168.56.104:7005 Adding replica 192.168.186.91:7003 to 192.168.56.102:7001 Adding replica 192.168.186.91:7004 to 192.168.56.103:7003 Adding replica 192.168.186.91:7005 to 192.168.56.104:7005 M: 319da27d8668a15d2d2d02afe433247694343459 192.168.56.102:7001 slots:0-5460 (5461 slots) master M: 3da756265e301ac0210760f13e990473f87a3017 192.168.56.103:7003 slots:5461-10922 (5462 slots) master M: 6f336da48c892d8e0c541a864765978ebfbca6d5 192.168.56.104:7005 slots:10923-16383 (5461 slots) master S: ff4cf9d8a141d85c478b9af0358c93bca342c236 192.168.56.102:7002 replicates 319da27d8668a15d2d2d02afe433247694343459 S: 43c2e0d7799e84b449803a68d557c3431e9e047e 192.168.56.103:7004 replicates 3da756265e301ac0210760f13e990473f87a3017 S: 3f174fae106cb6cf7e7f21ed844895ed7c18f793 192.168.56.104:7006 replicates 6f336da48c892d8e0c541a864765978ebfbca6d5 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.... [OK] All 16384 slots covered.
最后測(cè)試測(cè)試redis集群
redis-cli -h 192.168.56.102 -c -p 7001