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集群部署以及一些坑)

推荐阅读更多精彩内容

  • 在企业应用中,对于数据的固化一般采用数据库,但是数据库的吞吐量还是存在一定的问题的,在高并发高吞吐要求时,一般会直...
    丁小晶的晶小丁阅读 64,724评论 4 11
  • 这里创建6个redis节点,其中三个为主节点,三个为从节点。 redis和端口对应关系: 127.0.0.1:70...
    笨尛孩你谁啊阅读 1,034评论 0 0
  • 坐在前往拉萨市区的车里,连绵的山峰从窗外掠过,天边的云朵仿佛伸手就能触碰,空气中肆意弥漫着神秘的气息,脑海中立...
    努力吧夭桃君阅读 84评论 0 0
  • 终于踏上了回家的火车,感谢你把我送到了车站,耽误了你上班我感到非常抱歉,一直说走来着可是一直狠不下心走,...
    风花微凉阅读 117评论 0 0