工作之余學(xué)習(xí)代碼,視頻里講到單機(jī)多節(jié)點redis集群部署,但是有一個問題就是如果機(jī)器掛掉了那么集群也掛掉了。于是自己就改了一下,改成了多機(jī)多集群部署。
先說說遇到的坑,解決辦法會在文章最后給出(因為里面有些命令需要配置了之后才能用),感覺能踩的坑都讓我踩遍了:
1、搭集群時需要使用到ruby腳本,但是使用yum -y install ruby之后,運行g(shù)em install redis-3.2.2.gem會出現(xiàn)錯誤:
ERROR: Error installing redis-4.0.0.gem:
redis requires Ruby version >= 2.2.2.
2、redis創(chuàng)建集群失敗,原因是redis.conf文件中的 bind 配置錯誤:
can't connect to node 192.168.*.*
3、redis創(chuàng)建集群時顯示錯誤:
[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)建集群時無錯誤,但是一直處于Waiting for the cluster to join ……狀態(tài)非常久,使用cluster meet ip port命令無效
現(xiàn)在開始部署步驟
1、visualbox搭建服務(wù)器集群
2、在每臺機(jī)器上安裝redis實例和ruby工具
3、把redis-trib.rb移動到/usr/local/bin目錄中
redis-trib.rb都在redis的源碼包中的src目錄中
4、配置每個節(jié)點的redis.conf文件
要部署幾個redis實例就需要幾個redis.conf文件,這里需要修改每個redis的配置redis.conf文件
bind 192.168.56.103 這里的IP改成當(dāng)前redis實例所在機(jī)器的IP port 7003 這里的port改成當(dāng)前redis實例通信的port pidfile /var/run/redis_7003.pid 這個隨便什么名字都可以,不過每個配置文件里的一定不能相同,我改成了port標(biāo)識的 cluster-enabled yes 一定要改,開啟redis的集群功能 cluster-config-file nodes-7003.conf 這個隨便什么名字都可以,不過每個配置文件里的一定不能相同,我改成了port標(biāo)識的
5、iptables和firewall配置
需要在iptables或者firewall中添加redis的通信端口和redis總線端口,iptables和firewall建議只用一個,如果不想用的話關(guān)閉就行了,省去一部分麻煩
6、接下來啟動所有的redis實例,進(jìn)行集群“會面”了。連接不同的節(jié)點,激動人心的時刻到了
請不要照抄,依次類比你自己的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
會出現(xiàn)以下提示,在這里Can I set the above configuration (type 'yes' to accept): yes
請輸入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.
最后測試測試redis集群
redis-cli -h 192.168.56.102 -c -p 7001