Redis Cluster集群至少需要三个master节点,本文将以单机多实例的方式部署3个主节点及3个从节点,6个节点实例分别使用不同的端口及工作目录
1. 创建目录
首先,在redis安装目录/usr/local/redis-3.2.0下新建目录redis-cluster,并在该目录下再新建6个子目录,8001,8002,8003,8004,8005,8006,此时目录结构如下图所示:
[ranen@VM_0_15_centos redis-cluster]# pwd
/usr/local/redis-4.0.6/redis-cluster
[ranen@VM_0_15_centos redis-cluster]# ll
drwxr-xr-x 2 ranen ranen 4096 Jul 7 11:00 8001
drwxr-xr-x 2 ranen ranen 4096 Jul 7 11:00 8002
drwxr-xr-x 2 ranen ranen 4096 Jul 7 11:00 8003
drwxr-xr-x 2 ranen ranen 4096 Jul 7 11:00 8004
drwxr-xr-x 2 ranen ranen 4096 Jul 7 11:00 8005
drwxr-xr-x 2 ranen ranen 4096 Jul 7 11:00 8006
2. 修改配置
将redis安装目录/usr/local/redis-3.2.0下的redis.conf文件拷贝至8001文件夹,并修改以下配置:
#开启守护进程模式,后台运行
daemonize yes
#工作端口
port 8001
#此处需要bind本机ip,必须设置
bind 192.168.1.101 127.0.0.1
#指定工作目录,将持久化内容放置该目录
dir /usr/local/redis-3.2.0/redis-cluster/8001/下,不同实例一定要配置不同的工作目录
#开启集群模式
cluster-enabled yes
#生成的集群配置文件名称,集群搭建成功后会自动生成,在工作目录下
cluster-config-file nodes-8001.conf
#节点宕机发现时间,master node 宕机,从节点升级为master的时间
cluster-node-timeout 5000
#开启AOF模式
appendonly yes
#pid file所在目录
pidfile /tmp/run/redis_8001.pid
3. 拷贝配置文件到其他节点
把8001文件夹下的redis.conf文件拷贝到其他5个目录,并重新修改port 、dir、cluster-config-file 三个属性
这里可以使用sed命令快速修改
[ranen@VM_0_15_centos redis-cluster]# sed -i 's/8001/8002/g' 8002/redis.conf
[ranen@VM_0_15_centos redis-cluster]# sed -i 's/8001/8003/g' 8003/redis.conf
[ranen@VM_0_15_centos redis-cluster]# sed -i 's/8001/8004/g' 8004/redis.conf
[ranen@VM_0_15_centos redis-cluster]# sed -i 's/8001/8005/g' 8005/redis.conf
[ranen@VM_0_15_centos redis-cluster]# sed -i 's/8001/8006/g' 8006/redis.conf
4. 安装依赖
由于创建集群需要用到redis-trib这个命令,它依赖Ruby和RubyGems,因此我们要先安装一下
[ranen@VM_0_15_centos redis-cluster]# yum install ruby
[ranen@VM_0_15_centos redis-cluster]# yum install rubygems
[ranen@VM_0_15_centos redis-cluster]# gem install redis --version 3.3.3
5. 启动
[ranen@VM_0_15_centos redis-3.2.0]# ./src/redis-server redis-cluster/8001/redis.conf
[ranen@VM_0_15_centos redis-3.2.0]# ./src/redis-server redis-cluster/8002/redis.conf
[ranen@VM_0_15_centos redis-3.2.0]# ./src/redis-server redis-cluster/8003/redis.conf
[ranen@VM_0_15_centos redis-3.2.0]# ./src/redis-server redis-cluster/8004/redis.conf
[ranen@VM_0_15_centos redis-3.2.0]# ./src/redis-server redis-cluster/8005/redis.conf
[ranen@VM_0_15_centos redis-3.2.0]# ./src/redis-server redis-cluster/8006/redis.conf
6.启动检查
[ranen@VM_0_15_centos redis-3.2.0]# ps -ef|grep redis
ranen 20290 1 0 18:33 ? 00:00:02 ./src/redis-server *:8001 [cluster]
ranen 20295 1 0 18:33 ? 00:00:02 ./src/redis-server *:8002 [cluster]
ranen 20300 1 0 18:33 ? 00:00:02 ./src/redis-server *:8003 [cluster]
ranen 20305 1 0 18:33 ? 00:00:02 ./src/redis-server *:8004 [cluster]
ranen 20310 1 0 18:33 ? 00:00:02 ./src/redis-server *:8005 [cluster]
ranen 20312 1 0 18:33 ? 00:00:02 ./src/redis-server *:8006 [cluster]
ranen 22913 15679 0 19:31 pts/2 00:00:00 grep --color=auto redis
6个节点都已经正常启动。
7. 创建集群
[ranen@VM_0_15_centos redis-3.2.0]# ./src/redis-trib.rb create --replicas 1 192.168.1.101:8001 192.168.1.101:8002 192.168.1.101:8003 192.168.1.101:8004 192.168.1.101:8005 192.168.1.101:8006
由控制台可以看到,集群已经创建完毕,那么我们到8001这个节点看一下集群节点状态
192.168.1.101:8001> cluster nodes
068ac2afe1ade8b69b83226453fecc2b79cd93ae 172.16.0.15:8004@18004 slave 421ebe9e0a5ac6c811935ecd9dba83ef119dec17 0 1531008204920 4 connected
784c727c83a5952d3714ac211021f909cc4dfee4 172.16.0.15:8005@18005 slave eb5d700e2f030c02fb1f30ba4420d0b4f7170d84 0 1531008203000 5 connected
0537099e7cc7ab595c7aad5f0c96985251b85ec0 172.16.0.15:8006@18006 slave 79262341417df0a11eaf31e72bbf3e26f5f60ebf 0 1531008204419 6 connected
421ebe9e0a5ac6c811935ecd9dba83ef119dec17 172.16.0.15:8001@18001 myself,master - 0 1531008204000 1 connected 0-5460
eb5d700e2f030c02fb1f30ba4420d0b4f7170d84 172.16.0.15:8002@18002 master - 0 1531008203000 2 connected 5461-10922
79262341417df0a11eaf31e72bbf3e26f5f60ebf 172.16.0.15:8003@18003 master - 0 1531008203419 3 connected 10923-16383
控制台信息显示: 当前集群中存在3个主节点和3个从节点,说明我们的集群已经搭建成功
至此,Redis Cluster集群就搭建完成了!
redis tabase配置
#########基础配置#########
spring.redis.database=0
spring.redis.timeout=30000
spring.redis.password=taredis
spring.redis.cluster.max-redirects=3
spring.redis.cluster.nodes=10.20.30.10:8001,10.20.30.10:8002,10.20.30.10:8003,10.20.30.10:8004,10.20.30.10:8005,10.20.30.10:8006
spring.redis.lettuce.pool.max-active=100
spring.redis.lettuce.pool.max-idle=50
spring.redis.lettuce.pool.min-idle=10
spring.redis.lettuce.pool.max-wait=20000
spring.redis.lettuce.shutdown-timeout=10