首页 » Ruby » linux上搭建redis伪集群

linux上搭建redis伪集群

原文 http://blog.csdn.net/zls_1029/article/details/78253033

2017-10-16 22:20:38阅读(491)

本文使用的linux为Centos6.4版本,redis版本为redis-3.0.0,假设您已经在Linux上安装了redis单机版,安装目录为/usr/local/redis下,redis-3.0.0.tar.gz解压的目录在/usr/local,本文只是演示怎么搭建集群
一、搭建集群前需要的环境
搭建集群需要使用到官方提供的ruby脚本。
需要安装ruby的环境。
安装ruby:
yum install ruby
yum install rubygems
安装ruby的包:(redis-3.0.0.gem需要另外下载上传到linux上):
gem install redis-3.0.0.gem
二、搭建的步骤
第一步:创建6个redis实例,端口号从7001~7006
首先要将单机版的安装好的redis安装路径上bin目录复制一份到/usr/local/redis-cluster/redis01下
在/usr/local目录下新建redis-cluster文件夹:
mkdir redis-cluster
cd到/usr/local/redis,复制bin:
cp -r bin ../redis-cluster/redis01
复制完cd到redis01文件夹下,编辑redis.conf文件:
vi redis.conf
修改原端口号6379为7001
linux上搭建redis伪集群
去掉cluster-enable前面的注释。
linux上搭建redis伪集群
修改完毕保存redis.conf
然后将redis01复制6份
linux上搭建redis伪集群
分别修改redis02-redis06下面的redis-conf文件,将端口号分别改为7002,7003,7004,7005,7006保存即可
第二步:把创建集群的ruby脚本复制到redis-cluster目录下
ruby脚本的位置在redis的解压路径下的src下
cd到redis的解压路径的src下,复制redis-trib.rb:
linux上搭建redis伪集群
第三步:启动6个redis实例
可以写一个sh脚本,在redis-cluster文件下新建startall.sh文件
linux上搭建redis伪集群
然后执行脚本:
linux上搭建redis伪集群
查看进程,可知启动成功
第四步:创建集群
在redis-cluster下执行命令:
./redis-trib.rb create –replicas 1 192.168.11.133:7001 192.168.11.133:7002 192.168.11.133:7003 192.168.11.133:7004 192.168.11.133:7005 192.168.11.133:7006
其中192.168.11.133为自己linux的host
执行显示如下:

>>> Creating cluster
Connecting to node 192.168.11.133:7001: OK
Connecting to node 192.168.11.133:7002: OK
Connecting to node 192.168.11.133:7003: OK
Connecting to node 192.168.11.133:7004: OK
Connecting to node 192.168.11.133:7005: OK
Connecting to node 192.168.11.133:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.11.133:7001
192.168.11.133:7002
192.168.11.133:7003
Adding replica 192.168.11.133:7004 to 192.168.11.133:7001
Adding replica 192.168.11.133:7005 to 192.168.11.133:7002
Adding replica 192.168.11.133:7006 to 192.168.11.133:7003
M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.11.133:7001
   slots:0-5460 (5461 slots) master
M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.11.133:7002
   slots:5461-10922 (5462 slots) master
M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.11.133:7003
   slots:10923-16383 (5461 slots) master
S: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.11.133:7004
   replicates 5a8523db7e12ca600dc82901ced06741b3010076
S: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.11.133:7005
   replicates bf6f0929044db485dea9b565bb51e0c917d20a53
S: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.11.133:7006
   replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca
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.11.133:7001)
M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.11.133:7001
   slots:0-5460 (5461 slots) master
M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.11.133:7002
   slots:5461-10922 (5462 slots) master
M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.11.133:7003
   slots:10923-16383 (5461 slots) master
M: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.11.133:7004
   slots: (0 slots) master
   replicates 5a8523db7e12ca600dc82901ced06741b3010076
M: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.11.133:7005
   slots: (0 slots) master
   replicates bf6f0929044db485dea9b565bb51e0c917d20a53
M: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.11.133:7006
   slots: (0 slots) master
   replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

到此redis伪集群搭建完毕
三、测试集群

[root@localhost redis-cluster]# redis01/redis-cli -h 192.168.11.133 -p 7002 -c
[root@localhost redis-cluster]# redis01/redis-cli -h 192.168.11.133 -p 7002 -c
192.168.11.133:7002> set a 100
-> Redirected to slot [15495] located at 192.168.11.133:7003
OK
192.168.11.133:7003> 

四、关闭redis
同样类似启动写好脚本
linux上搭建redis伪集群

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹