第一步:安装Redis

前面已经安装过了 不解释,
Reids安装包里有个集群工具,要复制到/usr/local/bin里去

  1. cp redis-3.2.9/src/redis-trib.rb /usr/local/bin

第二步:修改配置,创建节点

我们现在要搞六个节点,三主三从,
端口规定分别是7001,7002,7003,7004,7005,7006
我们先在root目录下新建一个redis_cluster目录,然后该目录下再创建6个目录,
分别是7001,7002,7003,7004,7005,7006,用来存在redis配置文件;
这里我们要使用redis集群,要先修改redis的配置文件redis.conf

  1. mkdir redis_cluster //新建目录
  2. [root@localhost ~]# cd redis_cluster/
  3. [root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006
  4. [root@localhost redis_cluster]# ll
  5. 总用量 0
  6. drwxr-xr-x. 2 root root 6 7 27 17:18 7001
  7. drwxr-xr-x. 2 root root 6 7 27 17:18 7002
  8. drwxr-xr-x. 2 root root 6 7 27 17:18 7003
  9. drwxr-xr-x. 2 root root 6 7 27 17:18 7004
  10. drwxr-xr-x. 2 root root 6 7 27 17:18 7005
  11. drwxr-xr-x. 2 root root 6 7 27 17:18 7006

先复制一份配置文件到7001目录下

  1. [root@localhost ~]# cp redis-3.2.9/redis.conf redis_cluster/7001/

我们修改下这个配置文件

  1. vi redis_cluster/7001/redis.conf

修改以下几个
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 //集群超时时间,节点超过这个时间没反应就断定是宕机
appendonly yes //存储方式,aof,将写操作记录保存到日志中
7001下的修改完后,我们把7001下的配置分别复制到7002-7006 然后对应的再修改下配置即可;

  1. [root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/
  2. [root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/
  3. [root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/
  4. [root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/
  5. [root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/
  6. [root@localhost ~]# vi redis_cluster/7002/redis.conf
  7. [root@localhost ~]# vi redis_cluster/7003/redis.conf
  8. [root@localhost ~]# vi redis_cluster/7004/redis.conf
  9. [root@localhost ~]# vi redis_cluster/7005/redis.conf
  10. [root@localhost ~]# vi redis_cluster/7006/redis.conf

编辑后面5个配置文件,把 port ,pidfile,cluster-config-file 分别修改下即可;

第三步:启动六个节点的redis

  1. [root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf
  2. [root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf
  3. [root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf
  4. [root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf
  5. [root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf
  6. [root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf

启动六个节点

  1. [root@localhost ~]# ps -ef | grep redis

查找下redis进程

  1. root 9501 1 0 17:38 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]
  2. root 9512 1 0 17:45 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]
  3. root 9516 1 0 17:45 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]
  4. root 9520 1 0 17:45 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]
  5. root 9524 1 0 17:45 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]
  6. root 9528 1 0 17:45 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]

说明都启动成功了

第四步:创建集群

redis官方提供了redis-trib.rb工具,第一步里已经房到里bin下 ;
但是在使用之前 需要安装ruby,以及redis和ruby连接

  1. yum -y install ruby ruby-devel rubygems rpm-build
  2. gem install redis
  3. 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

(注意redis-5.0.0版本开始才支持“–cluster”,之前的版本会报错“Unrecognized option or bad number of args for: ‘–cluster’”)
redis-5.0.0版本创建集群

  1. [root@localhost ~]# redis-cli --cluster create 192.168.0.40:7001 192.168.0.40:7002 192.168.0.41:7003 192.168.0.41:7004 192.168.0.42:7005 192.168.0.42:7006 --cluster-replicas 1

运行结果

  1. Using 3 masters:
  2. 127.0.0.1:7001
  3. 127.0.0.1:7002
  4. 127.0.0.1:7003
  5. Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
  6. Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
  7. Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
  8. M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001
  9. slots:0-5460 (5461 slots) master
  10. M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002
  11. slots:5461-10922 (5462 slots) master
  12. M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003
  13. slots:10923-16383 (5461 slots) master
  14. S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004
  15. replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c
  16. S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005
  17. replicates d61e66e49e669b99d801f22f6461172696fdd1c9
  18. S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006
  19. replicates aa6bc3f1e1174c3a991c01882584707c2408ec18
  20. Can I set the above configuration? (type yes to accept):

从运行结果看 主节点就是7001 7002 7003 从节点分别是7004 7005 7006
7001分配到的哈希槽是 0-5460
7002分配到的哈希槽是 5461-10922
7003分配到的哈希槽是 10923-16383
最后问我们是否接受上面的设置,输入yes 就表示接受,我们输入yes