之前我说要写一下Redis Cluster的搭建步骤,后来忙忘了。
现在补上。
https://redis.io/
当前最新的稳定版本是4.0.11。压缩包只有1.7M。
2,规划只有一台机器模拟搭建
规划7001 - 7006 部署6个端口
先建好7001-7006 6个目录,用来放redis配置文件。在每个目录下建好data目录用来存放数据
将安装包解压好
tar -zxvf redis-4.0.11.tar.gz
下载的包只有源码,需要make一下
(注意gcc -v 看下版本 需要4.2以上)
将其中的redis.conf copy一份到7001下
3,修改配置文件修改点:
bind 10.138.46.25 :配一下ip
port 7001
daemonize yes : 后台启动
pidfile /home/rocketmq410/7001/redis_7001.pid :pidfele的路径,改成你自己的
logfile /home/rocketmq410/7001/redis.log : 可以打一下日志,默认是达到dev/null
dir /home/rocketmq410/7001/data/ : 存放数据的路径,必须是个目录
cluster-enabled yes
cluster-config-file nodes-7001.conf : redis节点的配置文件,起个名就好,这个文件是自动生成的
appendonly yes :开启追加保存
# appendfsync always :这两个是追加的模式,生成上根据你的需要来选择。有改变就保存还是每秒保存一次
appendfsync everysec :这里就用默认的了
改好一个后,每个对应的目录下复制一份
然后修改redis.conf 这里直接全局替换端口就好了,很快
如: :%s/7001/7003/g
4,安装rubyredis管理cluster的脚本是用ruby写的,所以需要使用ruby。这个脚本就是src下的redis-trib.rb
单纯使用redis命令管理集群也可以,但是会非常麻烦和危险。
yum install ruby
现在的版本下,只要安装Ruby,Rubygems就会自动安装
所以这个只需要install ruby
5,安装redis的ruby接口gem install redis
但是我遇到了报错:
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
解决办法:
[rocketmq410@rocketmq4 ~]$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
[rocketmq410@rocketmq4 ~]$ curl -sSL https://get.rvm.io | bash -s stable
[rocketmq410@rocketmq4 ~]$ find / -name rvm -print
/home/rocketmq410/.rvm/src/rvm
/home/rocketmq410/.rvm/src/rvm/bin/rvm
/home/rocketmq410/.rvm/src/rvm/lib/rvm
/home/rocketmq410/.rvm/src/rvm/scripts/rvm
/home/rocketmq410/.rvm/bin/rvm
/home/rocketmq410/.rvm/lib/rvm
/home/rocketmq410/.rvm/scripts/rvm
source /home/rocketmq410/.rvm/scripts/rvm
查看可用版本
[rocketmq410@rocketmq4 ~]$ 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[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
安装一个新版本
[rocketmq410@rocketmq4 ~]$ rvm install 2.4.1
中间会要输一次密码
[rocketmq410@rocketmq4 ~]$ rvm use 2.4.1
[rocketmq410@rocketmq4 ~]$ rvm use 2.4.1 --default
[rocketmq410@rocketmq4 ~]$ ruby -version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
OK了
继续
gem install redis
6,准备就绪,启动实例[rocketmq410@rocketmq4 src]$ ./redis-server ~/7001/redis.conf
[rocketmq410@rocketmq4 src]$ ./redis-server ~/7002/redis.conf
[rocketmq410@rocketmq4 src]$ ./redis-server ~/7003/redis.conf
[rocketmq410@rocketmq4 src]$ ./redis-server ~/7004/redis.conf
[rocketmq410@rocketmq4 src]$ ./redis-server ~/7005/redis.conf
[rocketmq410@rocketmq4 src]$ ./redis-server ~/7006/redis.conf
[rocketmq410@rocketmq4 src]$ ps -ef | grep redis
7,配置集群rocketm 26002 1 0 11:26 ? 00:00:01 ./redis-server 10.138.46.25:7001 [cluster]
rocketm 26445 1 0 11:30 ? 00:00:00 ./redis-server 10.138.46.25:7002 [cluster]
rocketm 26450 1 0 11:30 ? 00:00:00 ./redis-server 10.138.46.25:7003 [cluster]
rocketm 26464 1 0 11:30 ? 00:00:00 ./redis-server 10.138.46.25:7004 [cluster]
rocketm 26469 1 0 11:30 ? 00:00:00 ./redis-server 10.138.46.25:7005 [cluster]
rocketm 26482 1 0 11:30 ? 00:00:00 ./redis-server 10.138.46.25:7006 [cluster]
rocketm 26487 5176 0 11:30 pts/1 00:00:00 grep --color=auto redis
./redis-trib.rb create --replicas 1 10.138.46.25:7001 10.138.46.25:7002 10.138.46.25:7003 10.138.46.25:7004 10.138.46.25:7005 10.138.46.25:7006
--replicas 1 表示 为每个主节点配置1个备节点
执行,[WARNING] Some slaves are in the same host as their master 有个警告,因为主备在一台机器上是有问题的,生产上要分开部署。
[OK] All 16384 slots covered
这就搭建完成了
8,查看信息-c 是以集群模式登录
[rocketmq410@rocketmq4 src]$ ./redis-cli -c -h 10.138.46.25 -p 7001
10.138.46.25:7001> 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:7
cluster_my_epoch:7
cluster_stats_messages_ping_sent:542
cluster_stats_messages_pong_sent:50
cluster_stats_messages_sent:592
cluster_stats_messages_ping_received:50
cluster_stats_messages_pong_received:50
cluster_stats_messages_update_received:1
cluster_stats_messages_received:101
10.138.46.25:7001> CLUSTER NODES
a0189fb279204ab2bee767587567dbd1445cc3d8 10.138.46.25:7003@17003 master - 0 1535687220000 3 connected 10923-16383
1c8af9390e14a3aa486a12df02bd2fdb9a0107c1 10.138.46.25:7004@17004 slave a0189fb279204ab2bee767587567dbd1445cc3d8 0 1535687220249 4 connected
67ea8a2a279e293416b7e14355cabf73238c3000 10.138.46.25:7005@17005 master - 0 1535687220000 7 connected 0-5460
5bd1384233a584d99fb9fdbab9a2a30c9a62ac21 10.138.46.25:7001@17001 myself,slave 67ea8a2a279e293416b7e14355cabf73238c3000 0 1535687221000 1 connected
66b16c3c134ff7f828e4b2fdd88536c04d884c83 10.138.46.25:7006@17006 slave 1b2a62758e7bd46e4be95fc8dcda12a1bd99723e 0 1535687222256 6 connected
1b2a62758e7bd46e4be95fc8dcda12a1bd99723e 10.138.46.25:7002@17002 master - 0 1535687222000 2 connected 5461-10922