centos7 redis的安装请参考一下链接 http://blog.csdn.net/duguxingfeng/article/details/78911640
1.Reids安装包里有个集群工具,要复制到/usr/local/bin里去
[root@localhost ~]# ll
[root@localhost ~]# cd redis-3.2.9/src
[root@localhost src]# ll
[root@localhost src]# cp redis-trib.rb /usr/local/bin
[root@localhost ~]# cd /usr/local/bin;ll
2.创建节点,修改配置文件
我们现在要搞六个节点,三主三从,
端口规定分别是7001,7002,7003,7004,7005,7006
我们先在root目录下新建一个redis_cluster目录,然后该目录下再创建6个目录,
分别是7001,7002,7003,7004,7005,7006,用来存在redis配置文件;
这里我们要使用redis集群,要先修改redis的配置文件redis.conf
具体步骤如下:
[root@localhost ~]# mkdir redis_cluster
[root@localhost ~]# cd redis_cluster/
[root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006
先复制一份配置文件到7001目录下
[root@localhost redis_cluster]# cd
[root@localhost ~]# cp redis-3.2.9/redis.conf redis_cluster/7001/
[root@localhost ~]# vim redis_cluster/7001/redis.conf
修改此配置文件里面的一下几项数值
#bind 127.0.0.1 #注释掉,使其他机器也可访问
protected-mode no #关闭保护模式
port 7001 #六个节点配置文件分别是7001-7006
daemonize yes #redis后台运行
pidfile /var/run/redis_7001.pid #pidfile文件对应7001-7006
cluster-enabled yes #开启集群
cluster-config-file nodes_7001.conf #保存节点配置,自动创建,自动更新对应7001-7006
cluster-node-timeout 5000 /#集群超时时间,节点超过这个时间没反应就断定是宕机
7001下的修改完后,我们把7001下的配置分别复制到7002-7006 ,只需修改port 、pidfile、cluster-config-file 即可
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/
3.分别启动上述六个节点
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf
出现以上说明这六个节点已经启动成功,接下来需要创建集群
4.创建集群
这里需要用到第一步复制到usr/local/bin里面的redis-trib.rb工具
创建集群之前需要安装rubby,以及redis和rubby的连接
[root@localhost ~]# yum -y install ruby ruby-devel rubygems rpm-build
[root@localhost ~]# gem install redis
会提示
说明redis 需要ruby的版本在2.2.2以上。我们可以看到目前的版本为2.0.0
[root@localhost ~]# yum list ruby
[root@localhost ~]# yum remove ruby
提示是否删除依赖,这里选择y
采用 rvm来更新ruby
首先需要安装RVM 具体步骤如下:
1. [root@localhost ~]# gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3 会下载密钥,如果连接失败,多尝试几次。
2. root@localhost ~]# curl -L get.rvm.io | bash -s stable
3. [root@localhost ~]# find / -name rvm -print
4. [root@localhost ~]# source /usr/local/rvm/scripts/rvm
5. [root@localhost ~]# rvm list known
6.[root@localhost ~]# rvm install 2.3.3 安装ruby2.3.3此过程可能较慢,
7. [root@localhost ~]# ruby -v #查看当前ruby版本,为2.3.3
8. [root@localhost ~]# gem install redis #重新安装redis即可
redis安装完成之后即可创建集群
[root@localhost ~]# redis-trib.rb create --replicas 1 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 127.0.0.1:7006
这一步可能会提示
>>> Creating cluster
[ERR] Node 127.0.0.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
类似的错误。
其解决方案如下:
1. [root@localhost ~]# ps -ef | grep redis #查看目前redis的进程 杀掉节点的进程
2.到 用户目录下 删除下图文件。
3.重新启动各个节点即可。
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf
4.以上步骤可循环尝试
创建集群
[root@localhost ~]# redis-trib.rb create --replicas 1 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 127.0.0.1:7006
从运行结果看 主节点就是7001 7002 7003 从节点分别是7004 7005 7006
以上说明集群已经配置成功!
5.测试:
[root@localhost ~]# /usr/local/redis/bin/redis-cli -c -p 7002 #登录节点7002
设置age
登录其他节点获取,能够成功获取到值
6.集群宕机测试
这里我们干掉一个节点,比如7003
[root@localhost ~]# ps -ef|grep redis
root 9606 1 0 10:50 ? 00:00:06 /usr/local/redis/bin/redis-server *:7001 [cluster]
root 9614 1 0 10:50 ? 00:00:06 /usr/local/redis/bin/redis-server *:7002 [cluster]
root 9623 1 0 10:51 ? 00:00:06 /usr/local/redis/bin/redis-server *:7003 [cluster]
root 9635 1 0 10:51 ? 00:00:06 /usr/local/redis/bin/redis-server *:7004 [cluster]
root 9642 1 0 10:51 ? 00:00:06 /usr/local/redis/bin/redis-server *:7005 [cluster]
root 9649 1 0 10:51 ? 00:00:06 /usr/local/redis/bin/redis-server *:7006 [cluster]
root 10370 10209 0 11:31 pts/4 00:00:00 grep --color=auto redis
[root@localhost ~]# ps -ef|grep redis
root 9606 1 0 10:50 ? 00:00:06 /usr/local/redis/bin/redis-server *:7001 [cluster]
root 9614 1 0 10:50 ? 00:00:06 /usr/local/redis/bin/redis-server *:7002 [cluster]
root 9635 1 0 10:51 ? 00:00:06 /usr/local/redis/bin/redis-server *:7004 [cluster]
root 9642 1 0 10:51 ? 00:00:06 /usr/local/redis/bin/redis-server *:7005 [cluster]
root 9649 1 0 10:51 ? 00:00:06 /usr/local/redis/bin/redis-server *:7006 [cluster]
root 10381 10209 0 11:32 pts/4 00:00:00 grep --color=auto redis
查看目前集群的情况
[root@localhost ~]# redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: ce53acdd719273cf6af746da9760243cf70860b1 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: d155427a31c295179642c22b6936fd5acb31e6a8 127.0.0.1:7006
slots:10923-16383 (5461 slots) master
0 additional replica(s)
S: e39fb46a37e63f737b6802f13458b8a1978f7d05 127.0.0.1:7004
slots: (0 slots) slave
replicates ce53acdd719273cf6af746da9760243cf70860b1
S: 3aeb4b14c31f1c3f7858236ed1e705d62b824b11 127.0.0.1:7005
slots: (0 slots) slave
replicates dacc614b13d4f927cf80a557a74ddc7d737ccf23
M: dacc614b13d4f927cf80a557a74ddc7d737ccf23 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
我们看到 7006本来是从节点,但是7003这个主节点挂了,7006就变成了主节点
同时显示[OK] All 16384 slots covered. 表明,集群还可用。
现在我们干掉7006
[root@localhost ~]# ps -ef|grep redis
root 9606 1 0 10:50 ? 00:00:08 /usr/local/redis/bin/redis-server *:7001 [cluster]
root 9614 1 0 10:50 ? 00:00:08 /usr/local/redis/bin/redis-server *:7002 [cluster]
root 9635 1 0 10:51 ? 00:00:08 /usr/local/redis/bin/redis-server *:7004 [cluster]
root 9642 1 0 10:51 ? 00:00:08 /usr/local/redis/bin/redis-server *:7005 [cluster]
root 9649 1 0 10:51 ? 00:00:08 /usr/local/redis/bin/redis-server *:7006 [cluster]
root 10436 10209 0 11:37 pts/4 00:00:00 grep --color=auto redis
[root@localhost ~]# kill -9 9649
[root@localhost ~]# redis-trib.rb check 127.0.0.1:7001
[ERR] Sorry, can't connect to node 127.0.0.1:7006
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: ce53acdd719273cf6af746da9760243cf70860b1 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: e39fb46a37e63f737b6802f13458b8a1978f7d05 127.0.0.1:7004
slots: (0 slots) slave
replicates ce53acdd719273cf6af746da9760243cf70860b1
S: 3aeb4b14c31f1c3f7858236ed1e705d62b824b11 127.0.0.1:7005
slots: (0 slots) slave
replicates dacc614b13d4f927cf80a557a74ddc7d737ccf23
M: dacc614b13d4f927cf80a557a74ddc7d737ccf23 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
最后发现 Not all 16384 slots are covered by nodes. 没有完全覆盖,集群不能正常使用
如需重新启用,将已经挂掉的节点重新启动即可
如:命令如下:
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf
集群还可正常使用