Linux下Redis集群搭建与测试
集群搭建准备条件一台已经配置好Redis服务的虚拟机,地址为:192.168.2.100:6379克隆6台,使用命令nmtui修改地址分别为:192.168.2.101192.168.2.102192.168.2.103192.168.2.104192.168.2.105192.168.2.106由于集群的容错机制是超过半数的节点认为某节点挂掉就确认挂掉,因此我们搭建的集群最好是奇数
集群搭建
准备条件
一台已经配置好Redis服务的虚拟机,地址为:
192.168.2.100:6379
克隆6台,使用命令nmtui
修改地址分别为:
192.168.2.101
192.168.2.102
192.168.2.103
192.168.2.104
192.168.2.105
192.168.2.106
由于集群的容错机制是超过半数的节点认为某节点挂掉就确认挂掉,因此我们搭建的集群最好是奇数台(>=3)。又由于为了高可用性,每个节点需要有备份节点,因此我们搭建Redis集群至少要有6台虚拟机。
然后用Xshell连上,进行已下操作:
选择全部Xshell,将会提示如下:
开始配置
主机为192.168.2.100
,修改redis.conf文件:
daemonize yes # 作为后台运行
cluster-enabled yes # 开启集群
六台虚拟机的这两项配置都一样,因此我们把刚修改好的这份配置文件复制到其它5台虚拟机上。
scp /etc/redis/6379.conf 192.168.2.101:/etc/redis/6379.conf;scp /etc/redis/6379.conf 192.168.2.102:/etc/redis/6379.conf;scp /etc/redis/6379.conf 192.168.2.103:/etc/redis/6379.conf;scp /etc/redis/6379.conf 192.168.2.104:/etc/redis/6379.conf; scp /etc/redis/6379.conf 192.168.2.105:/etc/redis/6379.conf;scp /etc/redis/6379.conf 192.168.2.106:/etc/redis/6379.conf
可能会提示,输入yes
,之后输入用户名和密码即可。
安装ruby环境
以下命令都在
中运行
安装RVM
$gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$\curl -sSL https://get.rvm.io | bash -s stable
使用RVM安装Ruby
rvm install ruby 2.4.2
安装RubyGems(ruby第三方包管理工具)
$ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
$ gem sources -l
https://gems.ruby-china.org
# 确保只有 gems.ruby-china.org
最后运行:
$gem update --system
$gem -v
安装gem redis
在主机192.168.2.100
运行:
gem install redis
然后将redis-trib.rb
复制到/usr/local/bin
目录下:
cp /usr/local/redis-4.0.2/src/redis-trib.rb /usr/local/bin
正式搭建集群
在主机192.168.2.100
运行:
redis-trib.rb create --replicas 1 192.168.2.101:6379 192.168.2.102:6379 192.168.2.102:6379 192.168.2.103:6379 192.168.2.104:6379 192.168.2.105:6379 192.168.2.106:6379 192.168.2.107:6379
如果出现错误:
[ERR] Node 192.168.2.101:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决办法,所有主机都执行以下命令:
$redis-cli
$flushdb
如果出现错误:
/usr/local/rvm/gems/ruby-2.4.2/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Slot 1151 is already busy (Redis::CommandError)
from /usr/local/rvm/gems/ruby-2.4.2/gems/redis-4.0.1/lib/redis.rb:2764:in `block in method_missing'
from /usr/local/rvm/gems/ruby-2.4.2/gems/redis-4.0.1/lib/redis.rb:45:in `block in synchronize'
from /usr/local/rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
from /usr/local/rvm/gems/ruby-2.4.2/gems/redis-4.0.1/lib/redis.rb:45:in `synchronize'
from /usr/local/rvm/gems/ruby-2.4.2/gems/redis-4.0.1/lib/redis.rb:2763:in `method_missing'
from /usr/local/bin/redis-trib.rb:212:in `flush_node_config'
from /usr/local/bin/redis-trib.rb:776:in `block in flush_nodes_config'
from /usr/local/bin/redis-trib.rb:775:in `each'
from /usr/local/bin/redis-trib.rb:775:in `flush_nodes_config'
from /usr/local/bin/redis-trib.rb:1296:in `create_cluster_cmd'
from /usr/local/bin/redis-trib.rb:1700:in `<main>'
解决办法,所有主机都执行以下命令:
$redis-cli
$flushall
$cluster reset soft
$exit
出现下图表示配置成功:
可以看到主机:
192.168.2.101
192.168.2.102
192.168.2.103
是主库
主机:
192.168.2.104
192.168.2.105
192.168.2.106
是从库,且192.168.2.104
是192.168.2.101
的从库。
192.168.2.105
是192.168.2.102
的从库。
192.168.2.106
是192.168.2.103
的从库。
且
slots:0-5460被分配到192.168.2.101
,
slots:5461-10922被分配到192.168.2.102
,
slots:10923-16383被分配到192.168.2.103
。
集群测试
运行命令:
redis-cli -h 192.168.2.104 -p 6379 -c
-c
:表示重定向
192.168.2.104:6379> set key104 hello104
-> Redirected to slot [5860] located at 192.168.2.102:6379
OK
192.168.2.102:6379> get key104
"hello104"
查看集群信息:
cluster info
192.168.2.102:6379> cluster info
cluster_state:ok # 集群状态
cluster_slots_assigned:16384 # 已分配的插槽数量
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0 # 插槽分配失败的数量
cluster_known_nodes:6 # 已知节点数
cluster_size:3 # 集群数量
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:3321
cluster_stats_messages_pong_sent:3271
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:6597
cluster_stats_messages_ping_received:3271
cluster_stats_messages_pong_received:3326
cluster_stats_messages_received:6597
查看节点信息:
cluster nodes
192.168.2.102:6379> cluster nodes
c66496e74fa73e97474fb7c5083a4343ddf82700 192.168.2.103:6379@16379 master - 0 1512714933000 3 connected 10923-16383
75cb9a7cb353edc184bb75a31478d0bf428f7f50 192.168.2.101:6379@16379 master - 0 1512714932000 1 connected 0-5460
d1be0e6e88d2dc4f4798a60a9954a3727864eb86 192.168.2.106:6379@16379 slave c66496e74fa73e97474fb7c5083a4343ddf82700 0 1512714934674 6 connected
ff74ee17d417a8e424210d4223597dcb7205be51 192.168.2.104:6379@16379 slave 75cb9a7cb353edc184bb75a31478d0bf428f7f50 0 1512714931000 4 connected
077d5522363a210b56ae9b1abe8116da78b05263 192.168.2.105:6379@16379 slave e986f608ffd3b569471d32557b208487e995ec56 0 1512714934000 5 connected
e986f608ffd3b569471d32557b208487e995ec56 192.168.2.102:6379@16379 myself,master - 0 1512714934000 2 connected 5461-10922
参考:
http://blog.csdn.net/u012453843/article/details/70268176
http://blog.csdn.net/wudalang_gd/article/details/52153571
https://stackoverflow.com/questions/34230131/err-slot-xxx-is-already-busy-rediscommanderror
更多推荐
所有评论(0)