linux上搭建redis伪集群

2017-10-17 08:22:39来源:CSDN作者:zls_1029人点击

分享

本文使用的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
这里写图片描述
去掉cluster-enable前面的注释。
这里写图片描述
修改完毕保存redis.conf
然后将redis01复制6份
这里写图片描述
分别修改redis02-redis06下面的redis-conf文件,将端口号分别改为7002,7003,7004,7005,7006保存即可
第二步:把创建集群的ruby脚本复制到redis-cluster目录下
ruby脚本的位置在redis的解压路径下的src下
cd到redis的解压路径的src下,复制redis-trib.rb:
这里写图片描述
第三步:启动6个redis实例
可以写一个sh脚本,在redis-cluster文件下新建startall.sh文件
这里写图片描述
然后执行脚本:
这里写图片描述
查看进程,可知启动成功
第四步:创建集群
在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 clusterConnecting to node 192.168.11.133:7001: OKConnecting to node 192.168.11.133:7002: OKConnecting to node 192.168.11.133:7003: OKConnecting to node 192.168.11.133:7004: OKConnecting to node 192.168.11.133:7005: OKConnecting to node 192.168.11.133:7006: OK>>> Performing hash slots allocation on 6 nodes...Using 3 masters:192.168.11.133:7001192.168.11.133:7002192.168.11.133:7003Adding replica 192.168.11.133:7004 to 192.168.11.133:7001Adding replica 192.168.11.133:7005 to 192.168.11.133:7002Adding replica 192.168.11.133:7006 to 192.168.11.133:7003M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.11.133:7001   slots:0-5460 (5461 slots) masterM: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.11.133:7002   slots:5461-10922 (5462 slots) masterM: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.11.133:7003   slots:10923-16383 (5461 slots) masterS: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.11.133:7004   replicates 5a8523db7e12ca600dc82901ced06741b3010076S: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.11.133:7005   replicates bf6f0929044db485dea9b565bb51e0c917d20a53S: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.11.133:7006   replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693caCan 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 clusterWaiting 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) masterM: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.11.133:7002   slots:5461-10922 (5462 slots) masterM: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.11.133:7003   slots:10923-16383 (5461 slots) masterM: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.11.133:7004   slots: (0 slots) master   replicates 5a8523db7e12ca600dc82901ced06741b3010076M: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.11.133:7005   slots: (0 slots) master   replicates bf6f0929044db485dea9b565bb51e0c917d20a53M: 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 -c192.168.11.133:7002> set a 100-> Redirected to slot [15495] located at 192.168.11.133:7003OK192.168.11.133:7003> 

四、关闭redis
同样类似启动写好脚本
这里写图片描述

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台