redis集群部署以及一些坑

开始部署之前,务必先看最后一条

  • 从官网下载和编译redis安装包
 $ wget http://download.redis.io/releases/redis-3.2.5.tar.gz 
 $ tar xzf redis-3.2.5.tar.gz
 $ cd redis-3.2.5
 $ make
  • 配置redis
    创建redis.conf, 内容如下
 port 7000  
 cluster-enabled yes
 cluster-config-file nodes.conf
 cluster-node-timeout 5000
 appendonly yes

如果你不想做持久化的话,appendonly yes 改为 appendonly no,假设我们部署3个redis nodes, 那么需要创 建6个redis实例,其中3个node为master另外3个nodes的slave;

 mkdir cluster-test
 cd cluster-test
 mkdir 7000 7001 7002 7003 7004 7005

将redis.conf分别拷贝一份到这6个文件夹下,保证这6个目录下的redis.conf的port 分别为7000~7005

  • 启动所有redis实例
    分别进入六个目录,启动相应的redis-server
cd 7000
 ../redis-server ./redis.conf
 cd ../7001
 ../redis-server ./redis.conf
 cd ../7002
 ../redis-server ./redis.conf
 cd ../7003
 ../redis-server ./redis.conf
 cd ../7004
 ../redis-server ./redis.conf
 cd ../7005
 ../redis-server ./redis.conf
  • 安装redis-trib.rb运行环境
    redis-trib.rb是ruby代码,所以先要安装ruby
 yum install ruby  
 gem install redis -- 安装redis依赖

因为墙的问题,gem install redis 可能会失败,改用国内的源

 gem sources --remove https://rubygems.org/ 删掉原来的源
 gem sources -a https://gems.ruby-china.com/   改用国内的源 
 gem sources -l 查看现有的源 
 gem install redis  安装redis依赖
  • 创建集群
./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
  • 注意的地方
    • 使用redis-trib.rb构建集群,完成前不要配置密码

    • 集群构建完再通过config set + config rewrite命令逐个实例设置密码

    • 对集群设置密码,requirepass和masterauth都需要设置

    • 各个节点密码都必须一致,否则Redirected就会失败

        config set masterauth   yourpasswd 
        config set requirepass   yourpasswd
        auth yourpasswd
        config rewrite
      
    • 如果提示一下错误,那么是redis依赖版本错误,移除当前redis依赖,安装指定版本即可

        /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:111:in `rescue in _write_to_socket': Connection timed out (Redis::TimeoutError)  
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:104:in `_write_to_socket' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:131:in `block in write' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:130:in `loop' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:130:in `write' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:374:in `write' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:271:in `block in write' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:250:in `io' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:269:in `write' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:228:in `block (3 levels) in process' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:222:in `each' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:222:in `block (2 levels) in process' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:367:in `ensure_connected' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:221:in `block in process' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:306:in `logging' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:220:in `process' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:120:in `call' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis.rb:2705:in `block in method_missing' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis.rb:58:in `block in synchronize' 
       from /usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis.rb:58:in `synchronize' 
       from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis.rb:2704:in `method_missing' 
       from ./redis-trib.rb:212:in `flush_node_config' 
       from ./redis-trib.rb:776:in `block in flush_nodes_config' 
       from ./redis-trib.rb:775:in `each' 
       from ./redis-trib.rb:775:in `flush_nodes_config' 
       from ./redis-trib.rb:1296:in `create_cluster_cmd' 
       from ./redis-trib.rb:1701:in ` 
      

      那就gem删除 redis-3.3.2 插件版本,改用3.0.0

         gem list
         gem uninstall redis --version 3.3.2 
         gem install redis --version 3.0.0
         gem list
      

(转载至我另外一个博客 redis集群部署以及一些坑)

    原文作者:yandaren
    原文地址: https://www.jianshu.com/p/6e7c627703a5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞