初学redis,学习了redis的常用数据结构,然后就到了学习redis集群的时候了
那么,学习前面应该先搭建起来redis集群
以下搭建步骤是在各种找资料搭建起来的,亲测可以使用
首先安装redis
在这里安装的是redis源码包,所以需要gcc环境支持
yum install gcc-c++
将从redis官网上下载的redis安装包上传至 /usr/local
然后解压
tar -zxvf redis-3.2.5.tar.gz
进入解压后的目录进行编译
cd /usr/local/redis-3.2.5
make
编译这一步可以指定目录,也就是安装的指定目录
把上一步的make 写成 make PREFIX=/usr/local/redis install (在这里我指定的目录为 /usr/local/redis)
安装完毕之后 redis文件夹下面会生成一个bin目录
redis启动
直接运行bin/redis-server将以前端模式启动,前端模式启动的缺点是ssh命令窗口关闭则redis-server程序结束,不推荐使用此方法。如下图:
修改redis.conf配置文件, daemonize yes 以后端模式启动。
执行如下命令启动redis:
cd /usr/local/redis
./bin/redis-server ./redis.conf
至此 redis的安装完成了
下面开始搭建集群
ruby环境
redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境:
安装ruby
yum install ruby
yum install rubygems
安装ruby和redis的接口程序
gem install redis
集群结点规划
这里在同一台服务器用不同的端口表示不同的redis服务器,如下:
主节点:192.168.106.132:7001 192.168.106.132:7002 192.168.106.132:7003
从节点:192.168.106.132:7004 192.168.106.132:7005 192.168.106.132:7006
在/usr/local下创建redis-cluster目录,其下创建7001、7002。。7006目录,如下:
将redis安装目录bin下的文件拷贝到每个700X目录内,同时将redis源码目录src下的redis-trib.rb拷贝到redis-cluster目录下。
修改每个700X目录下的redis.conf配置文件:
port XXXX
cluster-enabled yes
启动每个节点redis服务
分别进入7001、7002、...7006目录,执行:
./redis-server ./redis.conf
查看进程
执行创建集群命令
在/usr/local/redis-cluster/
执行redis-trib.rb,此脚本是ruby脚本,它依赖ruby环境。
./redis-trib.rb create --replicas 1 192.168.106.132:7001 192.168.106.132:7002 192.168.106.132:7003 192.168.106.132:7004 192.168.106.132:7005 192.168.106.132:7006
说明:
redis集群至少需要3个主节点,每个主节点有一个从节点总共6个节点
replicas指定为1表示每个主节点有一个从节点
注意:
如果执行时报如下错误:
[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
解决方法是删除生成的配置文件nodes.conf,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除redis的持久化文件后再重启redis,比如:appendonly.aof、dump.rdb
创建集群输出如下
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.106.132:7001
192.168.106.132:7002
192.168.106.132:7003
Adding replica 192.168.106.132:7004 to 192.168.106.132:7001
Adding replica 192.168.106.132:7005 to 192.168.106.132:7002
Adding replica 192.168.106.132:7006 to 192.168.106.132:7003
M: 6c388e0cd04990b23e2e65b285b5f9c1bc996538 192.168.106.132:7001
slots:0-5460 (5461 slots) master
M: 01e9cd67978b1cad73a40e9a70000a236744cb17 192.168.106.132:7002
slots:5461-10922 (5462 slots) master
M: 72a649017fdd5d4045f9d58df8b231c2d69e6c32 192.168.106.132:7003
slots:10923-16383 (5461 slots) master
S: b90aa487cf48270df43e79af98807bb5ffabbe34 192.168.106.132:7004
replicates 6c388e0cd04990b23e2e65b285b5f9c1bc996538
S: f873f54c61dbb613df58fa4b9ff8bc3dd48e2388 192.168.106.132:7005
replicates 01e9cd67978b1cad73a40e9a70000a236744cb17
S: daa813e169580417f4410fac86a3d4ab9abda804 192.168.106.132:7006
replicates 72a649017fdd5d4045f9d58df8b231c2d69e6c32
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.106.132:7001)
M: 6c388e0cd04990b23e2e65b285b5f9c1bc996538 192.168.106.132:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: b90aa487cf48270df43e79af98807bb5ffabbe34 192.168.106.132:7004
slots: (0 slots) slave
replicates 6c388e0cd04990b23e2e65b285b5f9c1bc996538
M: 72a649017fdd5d4045f9d58df8b231c2d69e6c32 192.168.106.132:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 01e9cd67978b1cad73a40e9a70000a236744cb17 192.168.106.132:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: daa813e169580417f4410fac86a3d4ab9abda804 192.168.106.132:7006
slots: (0 slots) slave
replicates 72a649017fdd5d4045f9d58df8b231c2d69e6c32
S: f873f54c61dbb613df58fa4b9ff8bc3dd48e2388 192.168.106.132:7005
slots: (0 slots) slave
replicates 01e9cd67978b1cad73a40e9a70000a236744cb17
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查询集群信息
集群创建成功登陆任意redis结点查询集群中的节点情况
集群创建完毕
记录一下