Redis集群搭建(3主3从)

来源:互联网 发布:java无参返回值 编辑:程序博客网 时间:2024/05/14 06:11

Redis3.0开始支持集群,在每个Master上存放的数据可各不相同,即分布式存储的思想。集群中的每个节点都需要知道集群中自己之外的其它节点,这些需要在每个Redis节点的nodes-6379.conf中体现,该文件是集群建立后自动设置的,不需要手动修改。

本文中节点个数为6个,3个Master,每个Master存在一个Slave从节点。这6个节点的IP分别为:

192.168.0.203、192.168.0.204、192.168.0.205、192.168.0.206、192.168.0.207、192.168.0.208

需要修改集群中各个节点的redis.conf文件,下面以192.168.0.203上的redis配置文件为例进行说明:

daemonize yes   设置为后台启动


port  6379   端口号,可以根据需要进行修改

bind 192.168.0.203    绑定当前机器的ip


dir  /usr/local/redis/etc/   指定数据文件存放的位置

cluster-enabled  yes   开启集群模式


cluster-config-file nodes-6379.conf     每一个集群节点都需要一个不同的集群配置文件


appendonly yes   开启aof

appendfilename “appendonly.aof”    指定aof文件名


yum install ruby  安装ruby,Redis集群需要使用ruby指令


yum install rubygems   


gem install redis  

由于centos支持的ruby默认版本到2.0.0,因此需要安装RVM即Ruby的版本管理器


curl是一个文件传输工具,支持文件的上传和下载。

下载RVM

curl -L get.rvm.io | bash -s stable 

curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -


GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.

curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -

执行rvm脚本

source /usr/local/rvm/scripts/rvm

查看已知的ruby版本

rvm list known


安装ruby2.4.1版本

rvm install 2.4.1


查看rvm指令的帮助信息


切换到指定的ruby版本

rvm use 2.4.1

移除之前安装的1.8.7版本

rvm remove 1.8.7


分别启动6个节点中的Redis服务

在redis的安装目录src中,执行redis-trib.rb命令,redis-trib.rb是redis集群操作的脚本。首先查看它的帮助信息。

./redis-trib.rb help


./redis-trib.rb create --replicas 1 192.168.0.203:6379 192.168.0.204:6379 192.168.0.205:6379 192.168.0.206:6379 192.168.0.207:6379 192.168.0.208:6379

 

错误信息:[ERR] Node 192.168.0.203:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

在启动各个节点的redis之前,需要在各个节点使用redis-cli进入指定的redis服务并执行flushall,把各个节点中的数据清空。删除appendonly.aof、dump.rdb,同时将每个节点中的集群配置文件nodes-6379.conf删除。如下图:


分别重新启动6个节点中的Redis服务再次尝试执行下面指令

./redis-trib.rb create --replicas 1 192.168.0.203:6379 192.168.0.204:6379 192.168.0.205:6379 192.168.0.206:6379 192.168.0.207:6379 192.168.0.208:6379



--replicas 1  这个1=主节点个数/从节点个数      上述6个节点中,前3个是主节点 , 后3个是从节点。

主:192.168.0.203     从:192.168.0.206

主:192.168.0.204     从:192.168.0.207

主:192.168.0.205     从:192.168.0.208


若想重新创建集群,可以将6个节点中的nodes-6379.conf删掉,然后重新执行创建集群的指令即可。