redis集群cluster简单设置
环境:
这里参考官方使用一台服务器:centos 7 redis-5.0.4 192.168.10.10
redis集群cluster最少要3个主节点,所以本次需要创建6个实例:3个主节点,3个从节点。
1、创建cluster工作目录
[root@localhost ~]# mkdir -p /opt/redis-5.0.4/cluster-test/{7000,7001,7002,7003,7004,7005}
2、创建cluster的配置文件
[root@localhost ~]# cd /opt/redis-5.0.4/cluster-test/ [root@localhost cluster-test]# vim 7000/redis.conf port 7000 // 端口号 daemonize yes // 开启守护进程 dir "/opt/redis-5.0.4/cluster-test/data" // 集群的工作目录 logfile "/opt/redis-5.0.4/cluster-test/log/cluster-7000.log" // 日志文件 dbfilename "dump-7000.rdb" cluster-enabled yes // 启用集群功能 cluster-config-file nodes-7000.conf // 集群配置文件的名字 cluster-require-full-coverage no // #redis cluster需要16384个slot都正常的时候才能对外提供服务,换句话说,只要任何一个slot异常那么整个cluster不对外提供服务。 因此生产环境一般为no cluster-node-timeout 5000 //请求超时 默认15秒,可自行设置 appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
由于这6个实例的配置文件除了端口以外基本相同。所以我们将redis.conf分别复制一份到剩下的7001、7002、7003、7004、7005的目录下然后修改对应的端口。
最终的配置文件看起来向下面这样
[root@localhost cluster-test]# vim 7000/redis.conf port 7000 daemonize yes dir "/opt/redis-5.0.4/cluster-test/data" logfile "/opt/redis-5.0.4/cluster-test/log/cluster-7000.log" dbfilename "dump-7000.rdb" cluster-enabled yes cluster-config-file nodes-7000.conf cluster-require-full-coverage no cluster-node-timeout 5000 appendonly yes --------------------------------------------------------------------- [root@localhost cluster-test]# vim 7001/redis.conf port 7001 daemonize yes dir "/opt/redis-5.0.4/cluster-test/data" logfile "/opt/redis-5.0.4/cluster-test/log/cluster-7001.log" dbfilename "dump-7001.rdb" cluster-enabled yes cluster-config-file nodes-7001.conf cluster-require-full-coverage no cluster-node-timeout 5000 appendonly yes ------------------------------------------------------------------------ [root@localhost cluster-test]# vim 7002/redis.conf port 7002 daemonize yes dir "/opt/redis-5.0.4/cluster-test/data" logfile "/opt/redis-5.0.4/cluster-test/log/cluster-7002.log" dbfilename "dump-7002.rdb" cluster-enabled yes cluster-config-file nodes-7002.conf cluster-require-full-coverage no cluster-node-timeout 5000 appendonly yes ------------------------------------------------------------------------ [root@localhost cluster-test]# vim 7003/redis.conf port 7003 daemonize yes dir "/opt/redis-5.0.4/cluster-test/data" logfile "/opt/redis-5.0.4/cluster-test/log/cluster-7003.log" dbfilename "dump-7003.rdb" cluster-enabled yes cluster-config-file nodes-7003.conf cluster-require-full-coverage no cluster-node-timeout 5000 appendonly yes ------------------------------------------------------------------------ [root@localhost cluster-test]# vim 7004/redis.conf port 7004 daemonize yes dir "/opt/redis-5.0.4/cluster-test/data" logfile "/opt/redis-5.0.4/cluster-test/log/cluster-7004.log" dbfilename "dump-7004.rdb" cluster-enabled yes cluster-config-file nodes-7004.conf cluster-require-full-coverage no cluster-node-timeout 5000 appendonly yes ------------------------------------------------------------------------ [root@localhost cluster-test]# vim 7005/redis.conf port 7005 daemonize yes dir "/opt/redis-5.0.4/cluster-test/data" logfile "/opt/redis-5.0.4/cluster-test/log/cluster-7005.log" dbfilename "dump-7005.rdb" cluster-enabled yes cluster-config-file nodes-7005.conf cluster-require-full-coverage no cluster-node-timeout 5000 appendonly yes
创建好配置文件,在创建对应的cluster工作目录和日志文件目录
[root@localhost ~]# mkdir -p /opt/redis-5.0.4/cluster-test/data [root@localhost ~]# mkdir -p /opt/redis-5.0.4/cluster-test/log
3、运行集群
这里需要注意:
如果你使用的是redis 5,我们可以使用redis-cli中的redis集群命令行实用程序来创建新的集群、检查或重新分割现有集群,等等。
对于redis版本3或4,需要使用redis-trib。可以在redis源代码发行版的src目录中找到它。需要安装redis gem才能运行redis-trib,而gem需要reby环境,所以需要安装reby
如果是redis版本5以下的请按照下面安装reby环境和redis.gem。
3.1安装reby环境
[root@localhost cluster-test]# wget -p /opt/source https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
[root@localhost cluster-test]# tar -zxvf /opt/source/ruby-2.3.1.tar.gz -c /opt/
[root@localhost cluster-test]# cd /opt/ruby-2.3.1/
[root@localhost ruby-2.3.1]# ./configure --prefix=/opt/ruby231
[root@localhost ruby-2.3.1]# make && make install
[root@localhost bin]# vim /etc/profile // 添加环境变量
path="/opt/python362/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/tngx230/sbin:/opt/node-v8.6.0-linux-x64/bin:/opt/ruby231/bin"
[root@localhost src]# cp /opt/ruby231/bin/ruby /usr/local/ // redis-trib.rb会到这里去找reby
[root@localhost src]# cp /opt/ruby231/bin/gem /usr/local/
[root@localhost bin]# source /etc/profile // 使配置立即生效
3.2安装redis.gem
[root@localhost ~]# gem install redis
3.3开启redis实例
[root@localhost ~]# redis-server /opt/redis-5.0.4/cluster-test/7000/redis.conf [root@localhost ~]# redis-server /opt/redis-5.0.4/cluster-test/7001/redis.conf [root@localhost ~]# redis-server /opt/redis-5.0.4/cluster-test/7002/redis.conf [root@localhost ~]# redis-server /opt/redis-5.0.4/cluster-test/7003/redis.conf [root@localhost ~]# redis-server /opt/redis-5.0.4/cluster-test/7004/redis.conf [root@localhost ~]# redis-server /opt/redis-5.0.4/cluster-test/7005/redis.conf
开启成功后,看起来像下面这样:
3.3运行集群
[root@localhost src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 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
如果是redis5.0以上版本会有如下提示信息:
对于3或4版本的盆友,我也就只能走到这里了,下面我将以5.0版本进行操作。
4、运行集群
[root@localhost src]# redis-cli --cluster create 127.0.0.1:7000 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 --cluster-replicas 1
命令执行成功后,会看到如下界面:
输入yes根据上面的配置进行设置开启集群,集群开启成功后如下所示:
5、查看集群状态
redis-cli -p 7000 cluster info // 查看节点详细信息 redis-cli -p 7000 cluster nodes // 查看所有节点 redis-cli -p 7000 cluster nodes | grep master // 过滤出主节点 redis-cli -p 7000 cluster nodes | grep slave // 过滤出从节点
6、验证集群
开两个shell登陆redis实例
[root@localhost ~]# redis-cli -c -p 7000 // 登陆7000的实例 [root@localhost ~]# redis-cli -c -p 7003 // 登陆7003的实例
--------------------------------------------------------------------------------到此redis集群就算简单的实现了-----------------------------------------------------------------
参考文档:
上一篇: redis的主从复制,哨兵值守