码迷,mamicode.com
首页 > 其他好文 > 详细

Redis3.0.5安装集群实例分享

时间:2015-11-18 21:08:07      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

基于redhat linux来安装

前置:安装了yum(因为需要装ruby等软件,使用yum install很容易就安装了,安装yum见http://eip.teamshub.com/t/3111540


1、将【redis-3.0.5.tar.gz】放到home/redis目录;

2、解压
tar -zxvf redis-3.0.5.tar.gz

3、进到目录去编译
cd redis-3.0.5
make

4、创建集群需要的目录
mkdir -p /home/redis/redis-3.0.5/cluster-test
cd /home/redis/redis-3.0.5/cluster-test
mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005

5、修改配置文件redis.conf
cd /home/redis/redis-3.0.5/cluster-test/7000
vi redis.conf

##编辑文件中的内容为下面内容:
port 7000
daemonize yes  #这个可有可无,得看看是什么配置项
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

6、将7000的redis.conf配置文件,分别拷贝到7001/7002/7003/7004/7005目录下面
cp /home/redis/redis-3.0.5/cluster-test/7000/redis.conf /home/redis/redis-3.0.5/cluster-test/7001
cp /home/redis/redis-3.0.5/cluster-test/7000/redis.conf /home/redis/redis-3.0.5/cluster-test/7002
cp /home/redis/redis-3.0.5/cluster-test/7000/redis.conf /home/redis/redis-3.0.5/cluster-test/7003
cp /home/redis/redis-3.0.5/cluster-test/7000/redis.conf /home/redis/redis-3.0.5/cluster-test/7004
cp /home/redis/redis-3.0.5/cluster-test/7000/redis.conf /home/redis/redis-3.0.5/cluster-test/7005

7、对上一步拷贝完成之后要修改7001/7002/7003/7004/7005目录下面redis.conf文件中的port参数,分别改为对应的文件夹的名称

8、编写redis的启动脚本,如7000
cd /home/redis/redis-3.0.5/cluster-test/7000
vi start7000.sh

##在start7000.sh中编辑如下内容并保存:
cd /home/redis/redis-3.0.5/cluster-test/7000
nohup /home/redis/redis-3.0.5/src/redis-server redis.conf > redis7000.log 2>&1 &

#编辑保存好后,修改权限为可执行
chmod 777 start7000.sh

9、将start7000.sh的脚本,分别拷贝到7001/7002/7003/7004/7005目录下面
cp /home/redis/redis-3.0.5/cluster-test/7000/start7000.sh /home/redis/redis-3.0.5/cluster-test/7001/start7001.sh
cp /home/redis/redis-3.0.5/cluster-test/7000/start7000.sh /home/redis/redis-3.0.5/cluster-test/7002/start7002.sh
cp /home/redis/redis-3.0.5/cluster-test/7000/start7000.sh /home/redis/redis-3.0.5/cluster-test/7003/start7003.sh
cp /home/redis/redis-3.0.5/cluster-test/7000/start7000.sh /home/redis/redis-3.0.5/cluster-test/7004/start7004.sh
cp /home/redis/redis-3.0.5/cluster-test/7000/start7000.sh /home/redis/redis-3.0.5/cluster-test/7005/start7005.sh

10、对上一步拷贝完成之后要修改7001/7002/7003/7004/7005目录下面start700X.sh文件中的port参数,分别改为对应的文件夹的名称

11、分布启动各自路径下的start700X.sh,启动后,可以查看进程
[redis@localhost 7005]$ ps -ef|grep 700
redis     6063     1  0 22:56 ?        00:00:06 /home/redis/redis-3.0.5/src/redis-server *:7000 [cluster]
redis    28766     1  0 23:50 pts/2    00:00:00 /home/redis/redis-3.0.5/src/redis-server *:7001 [cluster]
redis    28795     1  0 23:52 pts/2    00:00:00 /home/redis/redis-3.0.5/src/redis-server *:7002 [cluster]
redis    28800     1  2 23:53 pts/2    00:00:00 /home/redis/redis-3.0.5/src/redis-server *:7003 [cluster]
redis    28817     1  0 23:53 pts/2    00:00:00 /home/redis/redis-3.0.5/src/redis-server *:7004 [cluster]
redis    28823     1  0 23:53 pts/2    00:00:00 /home/redis/redis-3.0.5/src/redis-server *:7005 [cluster]
redis    28831 28500  0 23:53 pts/2    00:00:00 grep 700

如在启动时遇到因为ulimit中nofile设置过小而启动不了,可以通过root用户去修改ulimit,给redis用户设置一下nofile
vim /etc/security/limits.conf
加入以下两行
redis soft nofile 10240
redis hard nofile 20480

12、升级ruby,安装gem 
安装gem需要ruby的版本在 1.8.7 以上,如果ruby版本低或没有装,则首先你的升级你的ruby
[root@localhost ~]# ruby -v
bash: ruby: command not found
[root@localhost ~]# yum install ruby

检查 ruby 版本: 
[root@localhost ~]# ruby -v
ruby 1.8.7 (2013-06-27 patchlevel 374) [i386-linux]

检查是否安装了rubygems:
[root@localhost ~]# rpm -qa|grep ruby
ruby-1.8.7.374-4.el6_6.i686
ruby-libs-1.8.7.374-4.el6_6.i686
如果没有rubygems,就安装
[root@localhost ~]# yum install rubygems

13、通过gem 安装redis ruby 接口
[root@localhost ~]# gem install redis

14、执行redis的创建集群命令创建集群
使用redis-trib.rb创建集群的用法说明:
#redis-trib.rb的create子命令构建
#--replicas 则指定了为Redis Cluster中的每个Master节点配备几个Slave节点
#节点角色由顺序决定,先master之后是slave

创建命令: 
/home/redis/redis-3.0.5/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

[redis@localhost src]$ /home/redis/redis-3.0.5/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
>>> Creating cluster
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: 240ded0272e5d6827a9d99f83304a38ba656ddad 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: d8640ddab8d45f26d34082490cce6a567e612192 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: f1a4a0983c595dcab49e8b8258538929525632e8 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: feb6af7fbdd9a68712c8017b447222aac596a5ee 127.0.0.1:7003
   replicates 240ded0272e5d6827a9d99f83304a38ba656ddad
S: 441f2fd0fb4b5fbe66929af699a20e3e989449fb 127.0.0.1:7004
   replicates d8640ddab8d45f26d34082490cce6a567e612192
S: cc16895d326c29bc159ddf6ca10dcfa04762d984 127.0.0.1:7005
   replicates f1a4a0983c595dcab49e8b8258538929525632e8
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 127.0.0.1:7000)
M: 240ded0272e5d6827a9d99f83304a38ba656ddad 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: d8640ddab8d45f26d34082490cce6a567e612192 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: f1a4a0983c595dcab49e8b8258538929525632e8 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
M: feb6af7fbdd9a68712c8017b447222aac596a5ee 127.0.0.1:7003
   slots: (0 slots) master
   replicates 240ded0272e5d6827a9d99f83304a38ba656ddad
M: 441f2fd0fb4b5fbe66929af699a20e3e989449fb 127.0.0.1:7004
   slots: (0 slots) master
   replicates d8640ddab8d45f26d34082490cce6a567e612192
M: cc16895d326c29bc159ddf6ca10dcfa04762d984 127.0.0.1:7005
   slots: (0 slots) master
   replicates f1a4a0983c595dcab49e8b8258538929525632e8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

默认是前三个节点 7000  7001  7002 是主, 后3个节点 7003  7004   7005 是从

如果需要重新分配,则将所有节点停止后,将以下几个文件删除:
rm appendonly.aof  dump.rdb  nodes.conf  redis7005.log
删除后再重新运行redis-trib.rb create命令添加组成集群。

15、监控集群状态命令
/home/redis/redis-3.0.5/src/redis-trib.rb check 127.0.0.1:7000

[redis@localhost 7000]$ /home/redis/redis-3.0.5/src/redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7004: OK
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 240ded0272e5d6827a9d99f83304a38ba656ddad 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: feb6af7fbdd9a68712c8017b447222aac596a5ee 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 240ded0272e5d6827a9d99f83304a38ba656ddad
M: f1a4a0983c595dcab49e8b8258538929525632e8 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: cc16895d326c29bc159ddf6ca10dcfa04762d984 127.0.0.1:7005
   slots: (0 slots) slave
   replicates f1a4a0983c595dcab49e8b8258538929525632e8
M: d8640ddab8d45f26d34082490cce6a567e612192 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 441f2fd0fb4b5fbe66929af699a20e3e989449fb 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d8640ddab8d45f26d34082490cce6a567e612192
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

16、测试
使用redis-cli命令进入集群环境
/home/redis/redis-3.0.5/src/redis-cli -c -p 7000

另外,当7000被杀掉时,7003会变为主节点;
 

Redis3.0.5安装集群实例分享

标签:

原文地址:http://www.cnblogs.com/dgcx/p/4975702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!