要在单台机器上搭建Redis集群,方式是通过不同的TCP端口启动多个实例,然后组成集群,同时记录在搭建过程中踩过的坑。
centos版本:7
redis版本:4.0.9
安装方式:源码安装
服务器:虚拟机1台, ip地址192.168.106.133
操作过程参照: Java微服务实战(赵计刚) 以及 百度
操作步骤
此处默认已安装好单台redis,如果不会可以参照https://blog.csdn.net/u010623954/article/details/80037078, 我安装的是4.0.9的版本,, 安装在/opt/目录下
1、启动Redis多个实例
我们在Redis安装目录下创建目录cluster_test,进入cluster_test目录, 创建7000-7005 六个目录, 并复制/opt/src/redis-4.0.9/redis.conf到六个目录下,并编写此6个配置文件,这6个配置文件用来启动6个实例,后面将使用这6个实例组成集群。
以7000.conf为例,配置文件需要填写如下几项:
port 7000 //端口7000,7001,7002,7003,7004,7005
bind 192.168.106.133 //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes.conf //集群的配置状态信息
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
分别启动6个实例
alias redis-server /opt/redis-4.0.9/src/redis-server //建立别名, 方便访问
redis-server cluster-test/7000/redis.conf
redis-server cluster-test/7001/redis.conf
redis-server cluster-test/7002/redis.conf
redis-server cluster-test/7003/redis.conf
redis-server cluster-test/7004/redis.conf
redis-server cluster-test/7005/redis.conf
启动成功后,看一下进程
[root@localhost ~]# ps -ef | grep redis | grep cluster
root 2208 1 0 01:33 ? 00:00:05 /opt/redis-4.0.9/src/redis-server *:7000 [cluster]
root 2216 1 0 01:34 ? 00:00:05 /opt/redis-4.0.9/src/redis-server *:7001 [cluster]
root 2222 1 0 01:35 ? 00:00:05 /opt/redis-4.0.9/src/redis-server *:7002 [cluster]
root 2229 1 0 01:37 ? 00:00:05 /opt/redis-4.0.9/src/redis-server *:7003 [cluster]
root 2236 1 0 01:38 ? 00:00:05 /opt/redis-4.0.9/src/redis-server *:7004 [cluster]
root 2243 1 0 01:39 ? 00:00:05 /opt/redis-4.0.9/src/redis-server *:7005 [cluster]
至此,ip=192.168.106.133机器上创建了6个实例,端口号为port=7000~7005。
2、安装ruby
1)yum安装ruby和依赖的包。yum install ruby rubygems -y (centos默认安装的是ruby2.0版的,后面后有坑)
结果
2)从https://rubygems.org/gems/redis/versions/4.0.1下载redis-4.0.1.gem, 并上传到服务器/opt目录下, 使用gem这个命令来安装redis接口
[root@localhost opt]# gem install redis-4.0.1.gem
ERROR: Error installing redis-4.0.1.gem:
redis requires Ruby version >= 2.2.2.
这一步骤中出现了bug,度娘告诉我是Ruby版本太低,需要升级版本。ruby最低得2.2.2的版本
3)升级Ruby的版本
安装rvm,我不知道这是个什么东西,但是感觉像是Ruby的一个包管理器。
这一操作得到了:
gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
然后利用该密钥下载Ruby并升级。(由于我已经执行过命令了, 所以显示结果可能不太一样)
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
接着,source环境,让rvm可用。
[root@localhost opt]# source /usr/local/rvm/scripts/rvm
查看Ruby可用版本
[root@localhost opt]# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
ruby-head
可以看到最新的版本是2.4.1,本文安装2.3.0
至此,我们升级了Ruby的版本。(已经安装过了~~~~~~, 就不演示显示结果了)
[root@localhost opt]# rvm install 2.3.0
Already installed ruby-2.3.0.
To reinstall use:
使用2.3.0版本,移除旧版本,查看ruby版本
rvm use 2.3.0
rvm remove 1.8.7
ruby --version
4)安装gem redis接口gem install redis
此处再次爆炸, 谷歌一番后, 好像是说https的slsl验证的问题
解决方法如下, 将https://rubygems.org/改为http://rubygems.org/
- gem sources -r https://rubygems.org/
- gem sources -a http://rubygems.org/
5)再次安装, 执行gem install redis
到目前为止,我们的Ruby和运行redis-trib.rb需要的环境安装完成了。
3、Redis集群搭建
有了Ruby执行环境,可以开始将之前的6个实例组建成集群了。
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.106.133:7000 192.168.106.133:7001 192.168.106.133:7002 192.168.106.133:7003 192.168.106.133:7004 192.168.106.133:7005
有三个master,有三个是slave。 后面跟上6个实例就好了,形式就是ip:port
最后, 别忘了开放端口, 重启防火墙
最后, 收藏一波别人出现的3个坑点, 地址https://my.oschina.net/gaoenwei/blog/1623214
【此处有坑】
第一坑
[root@itfirst src]# redis-trib.rb create --replicas 1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005
-bash: redis-trib.rb: command not found
[root@itfirst src]# cp redis-trib.rb /usr/local/bin
需要将redis-trib.rb复制到/usr/local/bin目录下。
第二坑
[root@itfirst bin]# redis-trib.rb create --replicas 1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005
>>> Creating cluster
[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.
踩完第一坑后,继续执行,发现了第二坑,还好有度娘,但是网上各种说法都有(主要参照了《极客on之路》的博客),发现错误的原因是redis数据库没有清除。
[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7001
192.168.186.91:7001> flushdb
OK
192.168.186.91:7001> quit
[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7002
192.168.186.91:7002> flushdb
OK
192.168.186.91:7002> quit
[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7003
192.168.186.91:7003> flushdb
OK
192.168.186.91:7003> quit
[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7004
192.168.186.91:7004> flushdb
OK
192.168.186.91:7004> quit
[root@itfirst src]# redis-cli -h 192.168.186.91 -p 7005
192.168.186.91:7005> flushdb
OK
192.168.186.91:7005> quit
[root@itfirst src]# redis-trib.rb create --replicas 1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.186.91:7000
192.168.186.91:7001
192.168.186.91:7002
Adding replica 192.168.186.91:7003 to 192.168.186.91:7000
Adding replica 192.168.186.91:7004 to 192.168.186.91:7001
Adding replica 192.168.186.91:7005 to 192.168.186.91:7002
M: 61b2b608177374fd0382c872f996a2c25f62daac 192.168.186.91:7000
slots:0-5460,9189 (5462 slots) master
M: 50e678c98c31baa4ece1cba096cc34b4545456f3 192.168.186.91:7001
slots:5461-10922 (5462 slots) master
M: b8dc855a92d1c9a6e358380286a757011c40601d 192.168.186.91:7002
slots:9189,10923-16383 (5462 slots) master
S: 42392d8b4665500b3229b5c5b9dcebed311c9cdf 192.168.186.91:7003
replicates 61b2b608177374fd0382c872f996a2c25f62daac
S: 4e8cd9bae1dc0ffa63a3b8315e3f92b0490e65f8 192.168.186.91:7004
replicates 50e678c98c31baa4ece1cba096cc34b4545456f3
S: 3344981c3290c39b0d9f427842398c17de835293 192.168.186.91:7005
replicates b8dc855a92d1c9a6e358380286a757011c40601d
Can I set the above configuration? (type 'yes' to accept): yes
/usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Slot 9189 is already busy (Redis::CommandError)
from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:2764:in `block in method_missing'
from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:45:in `block in synchronize'
from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:45:in `synchronize'
from /usr/local/rvm/gems/ruby-2.3.0/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:1696:in `<main>'
第三坑
[root@itfirst src]# redis-trib.rb create --replicas 1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.186.91:7000
192.168.186.91:7001
192.168.186.91:7002
Adding replica 192.168.186.91:7003 to 192.168.186.91:7000
Adding replica 192.168.186.91:7004 to 192.168.186.91:7001
Adding replica 192.168.186.91:7005 to 192.168.186.91:7002
M: 61b2b608177374fd0382c872f996a2c25f62daac 192.168.186.91:7000
slots:0-5460,9189 (5462 slots) master
M: 50e678c98c31baa4ece1cba096cc34b4545456f3 192.168.186.91:7001
slots:5461-10922 (5462 slots) master
M: b8dc855a92d1c9a6e358380286a757011c40601d 192.168.186.91:7002
slots:9189,10923-16383 (5462 slots) master
S: 42392d8b4665500b3229b5c5b9dcebed311c9cdf 192.168.186.91:7003
replicates 61b2b608177374fd0382c872f996a2c25f62daac
S: 4e8cd9bae1dc0ffa63a3b8315e3f92b0490e65f8 192.168.186.91:7004
replicates 50e678c98c31baa4ece1cba096cc34b4545456f3
S: 3344981c3290c39b0d9f427842398c17de835293 192.168.186.91:7005
replicates b8dc855a92d1c9a6e358380286a757011c40601d
Can I set the above configuration? (type 'yes' to accept): yes
/usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Slot 9189 is already busy (Redis::CommandError)
from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:2764:in `block in method_missing'
from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:45:in `block in synchronize'
from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis.rb:45:in `synchronize'
from /usr/local/rvm/gems/ruby-2.3.0/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:1696:in `<main>'
还是度娘靠谱,在《redis 跨机器集群启动出错》博客中找到了答案。
这是由于之间创建集群没有成功,需要将nodes.conf和dir里面的文件全部删除。
[root@itfirst 7000]# find / -name "nodes-7000.conf"
/usr/local/redis-3.2.3/src/nodes-7000.conf
[root@itfirst 7000]# cd ../../
[root@itfirst src]# ls
adlist.c bitops.o dict.h lzf_c.o notify.c redis-benchmark.c rio.c slowlog.h t_zset.o
adlist.h blocked.c dict.o lzf_d.c notify.o redis-benchmark.o rio.h slowlog.o util.c
adlist.o blocked.o dump.rdb lzf_d.o object.c redis-check-aof rio.o solarisfixes.h util.h
ae.c cluster.c endianconv.c lzf.h object.o redis-check-aof.c scripting.c sort.c util.o
ae_epoll.c cluster.h endianconv.h lzfP.h pqsort.c redis-check-aof.o scripting.o sort.o valgrind.sup
ae_evport.c cluster.o endianconv.o Makefile pqsort.h redis-check-rdb sdsalloc.h sparkline.c version.h
ae.h config.c fmacros.h Makefile.dep pqsort.o redis-check-rdb.c sds.c sparkline.h ziplist.c
ae_kqueue.c config.h geo.c memtest.c pubsub.c redis-check-rdb.o sds.h sparkline.o ziplist.h
ae.o config.o geo.h memtest.o pubsub.o redis-cli sds.o syncio.c ziplist.o
ae_select.c crc16.c geo.o mkreleasehdr.sh quicklist.c redis-cli.c sentinel.c syncio.o zipmap.c
anet.c crc16.o help.h multi.c quicklist.h redis-cli.o sentinel.o testhelp.h zipmap.h
anet.h crc64.c hyperloglog.c multi.o quicklist.o redis_cluster server.c t_hash.c zipmap.o
anet.o crc64.h hyperloglog.o networking.c rand.c redis-sentinel server.h t_hash.o zmalloc.c
aof.c crc64.o intset.c networking.o rand.h redis-server server.o t_list.c zmalloc.h
aof.o db.c intset.h nodes-7000.conf rand.o redis-trib.rb setproctitle.c t_list.o zmalloc.o
asciilogo.h db.o intset.o nodes-7001.conf rdb.c release.c setproctitle.o t_set.c
bio.c debug.c latency.c nodes-7002.conf rdb.h release.h sha1.c t_set.o
bio.h debugmacro.h latency.h nodes-7003.conf rdb.o release.o sha1.h t_string.c
bio.o debug.o latency.o nodes-7004.conf redisassert.h replication.c sha1.o t_string.o
bitops.c dict.c lzf_c.c nodes-7005.conf redis-benchmark replication.o slowlog.c t_zset.c
[root@itfirst src]# rm -rf nodes-700*
然后重启redis服务。
[root@itfirst src]# redis-trib.rb create --replicas 1 192.168.186.91:7000 192.168.186.91:7001 192.168.186.91:7002 192.168.186.91:7003 192.168.186.91:7004 192.168.186.91:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.186.91:7000
192.168.186.91:7001
192.168.186.91:7002
Adding replica 192.168.186.91:7003 to 192.168.186.91:7000
Adding replica 192.168.186.91:7004 to 192.168.186.91:7001
Adding replica 192.168.186.91:7005 to 192.168.186.91:7002
M: 319da27d8668a15d2d2d02afe433247694343459 192.168.186.91:7000
slots:0-5460 (5461 slots) master
M: 3da756265e301ac0210760f13e990473f87a3017 192.168.186.91:7001
slots:5461-10922 (5462 slots) master
M: 6f336da48c892d8e0c541a864765978ebfbca6d5 192.168.186.91:7002
slots:10923-16383 (5461 slots) master
S: ff4cf9d8a141d85c478b9af0358c93bca342c236 192.168.186.91:7003
replicates 319da27d8668a15d2d2d02afe433247694343459
S: 43c2e0d7799e84b449803a68d557c3431e9e047e 192.168.186.91:7004
replicates 3da756265e301ac0210760f13e990473f87a3017
S: 3f174fae106cb6cf7e7f21ed844895ed7c18f793 192.168.186.91:7005
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....
>>> Performing Cluster Check (using node 192.168.186.91:7000)
M: 319da27d8668a15d2d2d02afe433247694343459 192.168.186.91:7000
slots:0-5460 (5461 slots) master
M: 3da756265e301ac0210760f13e990473f87a3017 192.168.186.91:7001
slots:5461-10922 (5462 slots) master
M: 6f336da48c892d8e0c541a864765978ebfbca6d5 192.168.186.91:7002
slots:10923-16383 (5461 slots) master
M: ff4cf9d8a141d85c478b9af0358c93bca342c236 192.168.186.91:7003
slots: (0 slots) master
replicates 319da27d8668a15d2d2d02afe433247694343459
M: 43c2e0d7799e84b449803a68d557c3431e9e047e 192.168.186.91:7004
slots: (0 slots) master
replicates 3da756265e301ac0210760f13e990473f87a3017
M: 3f174fae106cb6cf7e7f21ed844895ed7c18f793 192.168.186.91:7005
slots: (0 slots) master
replicates 6f336da48c892d8e0c541a864765978ebfbca6d5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
4、验证集群状态
登录集群客户端,-c标识以集群方式登录
[root@itfirst src]# redis-cli -h 192.168.186.91 -c -p 7002
查看集群状态
192.168.186.91:7002> 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:3
cluster_stats_messages_sent:124174
cluster_stats_messages_received:124174
192.168.186.91:7002> cluster nodes
319da27d8668a15d2d2d02afe433247694343459 192.168.186.91:7000 master - 0 1519465974307 1 connected 0-5460
3f174fae106cb6cf7e7f21ed844895ed7c18f793 192.168.186.91:7005 slave 6f336da48c892d8e0c541a864765978ebfbca6d5 0 1519465971278 6 connected
6f336da48c892d8e0c541a864765978ebfbca6d5 192.168.186.91:7002 myself,master - 0 0 3 connected 10923-16383
3da756265e301ac0210760f13e990473f87a3017 192.168.186.91:7001 master - 0 1519465972288 2 connected 5461-10922
43c2e0d7799e84b449803a68d557c3431e9e047e 192.168.186.91:7004 slave 3da756265e301ac0210760f13e990473f87a3017 0 1519465973298 5 connected
ff4cf9d8a141d85c478b9af0358c93bca342c236 192.168.186.91:7003 slave 319da27d8668a15d2d2d02afe433247694343459 0 1519465969258 4 connected