Redis 集群环境的搭建
下载与解压
[root@localhost ~]# cd /usr/temp/
[root@localhost temp]# wget http://download.redis.io/releases/redis-3.2.4.tar.gz
[root@localhost temp]# tar -zxvf redis-3.2.4.tar.gz
编译与安装
[root@localhost temp]# cd redis-3.2.4
[root@localhost temp]# make && make install
以上只是演示了redis的下载安装等操作,可以忽略,注意以下用的版本是之前的redis3.0.2
将src目录下的 redis-trib.rb 复制到 /usr/local/bin 目录下
[root@localhost src]# cp redis-trib.rb /usr/local/bin/
创建 Redis 节点
首先在机器上创建一个redis_cluster 目录;
[root@localhost src]#mkdir redis_cluster
在 redis_cluster 目录下,创建名为7000、7001、7002的目录,并将 redis.conf 拷贝到这三个目录中
[root@localhost redis_cluster]# mkdir 7000 7001 7002
[root@localhost redis_cluster]# cp /usr/temp/redis-3.0.2/redis.conf /usr/redis/redis_cluster/7000/ [root@localhost redis_cluster]# cp /usr/temp/redis-3.0.2/redis.conf /usr/redis/redis_cluster/7001/
[root@localhost redis_cluster]# cp /usr/temp/redis-3.0.2/redis.conf /usr/redis/redis_cluster/7002
分别修改这三个配置文件,修改如下内容:
port 7000 //端口7000,7002,7003
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7000.conf//集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
启动各个节点
[root@localhost bin]# ./redis-server /usr/redis/redis_cluster/7000/redis.conf
[root@localhost bin]# ./redis-server /usr/redis/redis_cluster/7001/redis.conf
[root@localhost bin]# ./redis-server /usr/redis/redis_cluster/7002/redis.conf
[root@localhost bin]# ./redis-server /usr/redis/redis_cluster/7003/redis.conf(又新增了一个)
检查redis启动状况
[root@localhost bin]# ps -ef | grep redis
创建集群
Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中,第三步中已将它复制到 /usr/local/bin 目录中,可以直接在命令行中使用了。使用下面这个命令即可完成安装。
[root@localhost bin]# redis-trib.rb create --replicas 1 192.168.119.130:7000 192.168.119.130:7001 192.168.119.130:7002 192.168.119.130:7003
/usr/bin/env: ruby: No such file or directory
出错了,需要安装ruby插件
[root@localhost bin]# yum -y install ruby ruby-devel rubygems rpm-build
又出错了
[root@localhost bin]# ./redis-trib.rb create --replicas 1 192.168.119.130:7000 192.168.119.130:7001 192.168.119.130:7002 192.168.119.130:7003
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./redis-trib.rb:25
这次貌似是一个gem_original_require没有安装,当然这个一般是说ruby版本太老了,所以现在要么升级ruby版本,要么直接安装ruby的redis驱动。
安装时出现错误,也可以下载文件后进行离线安装,下载地址:http://rubygems.org/gems/redis/versions,选择合适的版本,然后安装:
[root@localhost bin]# cd /usr/temp/
[root@localhost temp]# gem install -l /usr/temp/redis-3.0.2.gem
接下来再去执行集群的命令:
[root@localhost bin]# ./redis-trib.rb create --replicas 1 192.168.119.130:7000 192.168.119.130:7001 192.168.119.130:7002 192.168.119.130:7003
>>> Creating cluster
Connecting to node 192.168.119.130:7000: OK
Connecting to node 192.168.119.130:7001: OK
又出错了,说明redis中有数据,需要清除一下。
[root@localhost bin]# redis-trib.rb create --replicas 1 192.168.119.130:7000 192.168.119.130:7001 192.168.119.130:7002 192.168.119.130:7003
>>> Creating cluster
Connecting to node 192.168.119.130:7000: OK
Connecting to node 192.168.119.130:7001: OK
Connecting to node 192.168.119.130:7002: OK
Connecting to node 192.168.119.130:7003: OK
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 4 nodes and 1 replicas per node.
*** At least 6 nodes are required.
又出错了,以上说明,至少需要6个redis节点。
重复以上方法,再增加2个redis节点服务
再次执行:
[root@localhost bin]# ./redis-trib.rb create --replicas 1 192.168.119.130:7000 192.168.119.130:7001 192.168.119.130:7002 192.168.119.130:7003 192.168.119.130:7004 192.168.119.130:7005
如果出现以下错误:
删除对应的nodes-xxx.conf文件,然后再逐一重启各redis服务
接下来再次执行集群的命令即可成功,过程上要输入yes:
测试
连接到redis集群
[root@localhost bin]# ./redis-cli -h 192.168.119.130 -p 7000
基本信息
192.168.119.130:7000> cluster info
节点信息
192.168.119.130:7000> cluster nodes
数据操作
[root@localhost bin]# ./redis-cli -h 192.168.119.130 -c -p 7000
192.168.119.130:7000> set name zhangsan
-> Redirected to slot [5798] located at 192.168.119.130:7001
OK
192.168.119.130:7001> get name
"zhangsan"
192.168.119.130:7001> keys *
1) "name"
服务器重启后重启redis集群服务
当linux服务重启启动后,redis集群就挂了,重新启动redis服务操作如下:
1、逐一启动之前的各个redis节点
2、再次执行redis集群命令
3、此时即可使用redis集群服务