美文网首页
redis 三主三从 集群部署

redis 三主三从 集群部署

作者: PM徐志强 | 来源:发表于2018-06-27 09:36 被阅读0次

    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 设置为后台启动

    image

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

    bind 192.168.0.203 绑定当前机器的ip

    image

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

    cluster-enabled yes 开启集群模式

    image

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

    image

    appendonly yes 开启aof

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

    image

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

    image

    yum install rubygems

    image

    **gem install redis **

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

    image

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

    下载RVM

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

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

    image

    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 -

    image

    执行rvm脚本

    source /usr/local/rvm/scripts/rvm

    查看已知的ruby版本

    rvm list known

    image

    安装ruby2.4.1版本

    rvm install 2.4.1

    image

    查看rvm指令的帮助信息

    image

    切换到指定的ruby版本

    rvm use 2.4.1

    移除之前安装的1.8.7版本

    rvm remove 1.8.7

    image

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

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

    ./redis-trib.rb help

    image

    ./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

    image

    错误信息:[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删除。如下图:

    image

    分别重新启动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

    image image

    --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删掉,然后重新执行创建集群的指令即可。

    相关文章

      网友评论

          本文标题:redis 三主三从 集群部署

          本文链接:https://www.haomeiwen.com/subject/jazvyftx.html