28 November 2015

之前写过一个redis的入门教程, 这次来用redis进行集群的安装配置。

Redis集群介绍

  • Redis 集群是一个提供在多个Redis间节点间共享数据的程序集.
  • Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误.
  • Redis 集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下继续处理命令.
  • Redis 集群的优势: -自动分割数据到不同的节点上.
    • 整个集群的部分节点失败或者不可达的情况下能够继续处理命令.

准备工作

  • 我的环境:centos6.5,redis版本:3.0.5
  • 事先下载好redis-3.0.5
  • 要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下

      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
    

解压,编译

  • 解压到/opt/redis-3.0.5/ 目录以后称为主目录
  • 解压后主要有几个目录(deps,src,tests,utils)和一些构建文件
  • 编译

    cd /opt/redis-3.0.5/ make make install

  • 在主目录下创建bin目录和etc目录,
  • 在/opt/redis-3.0.5/src/拷贝可执行文件(redis-benchmark,redis-check-aof,redis-check-dump,redis-cli,redis-sentinel,redis-server,redis-trib.rb)到/opt/redis-3.0.5/bin目录
  • 拷贝额配置文件redis.conf到etc目录

集群环境准备

  • 在主目录中创建津群所需要目录

      mkdir -p /opt/redis-3.0.5/cluster
      cd /opt/redis-3.0.5/cluster
      mkdir 7000
      mkdir 7001
      mkdir 7002
      mkdir 7003
      mkdir 7004
      mkdir 7005
    
  • 修改配置文件redis.conf

      vi redis.conf
      ##修改配置文件中的下面选项
      port 7000
      daemonize yes
      cluster-enabled yes
      cluster-config-file nodes.conf
      cluster-node-timeout 5000
      appendonly yes
    
  • 修改完redis.conf配置文件中的这些配置项之后,把这个配置文件分别拷贝到7000/ 7001/ 7002/ 7003/ 7004/ 7005/ 目录下面,并且端口更改为对应文件夹的名称

启动集群

  • 分別進入7000 到7005 6个目录,运行如下命令

      ./redis-server redis.conf
    
  • 运行完毕后,运行ps -ef grep redis,查看下如下进程即为正常:
      [root@zt 7005]# ps -ef|grep redis
      root     28198     1  0 22:24 ?        00:00:00 ./redis-server *:7000 [cluster]
      root     28207     1  0 22:24 ?        00:00:00 ./redis-server *:7001 [cluster]
      root     28212     1  0 22:24 ?        00:00:00 ./redis-server *:7002 [cluster]
      root     28217     1  0 22:24 ?        00:00:00 ./redis-server *:7003 [cluster]
      root     28221     1  0 22:24 ?        00:00:00 ./redis-server *:7004 [cluster]
      root     28227     1  0 22:24 ?        00:00:00 ./redis-server *:7005 [cluster]
      root     28236 21341  0 22:25 pts/1    00:00:00 grep redis
    
  • 在目录中运行redis的创建集群命令,以创建集群

      cd /opt/redis-3.0.5/bin/
      ./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 
    
  • 运行过程中会提示需要ruby环境,因为redis-trib.rb 是个ruby脚本
  • 安装ruby相关依赖:

      yum install ruby # ruby运行时环境
      yum install rubygems #用来安装ruby代码些的相关软件包
      gem install redis #因为ruby脚本redis-trib.rb 要调用redis接口,所以需要ruby的redis接口
    
  • 重新执行之前创建集群命令:

      [root@zhangtao bin]# ./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 
      >>> Creating cluster
      Connecting to node 127.0.0.1:7000: OK
      Connecting to node 127.0.0.1:7001: OK
      Connecting to node 127.0.0.1:7002: OK
      Connecting to node 127.0.0.1:7003: OK
      Connecting to node 127.0.0.1:7004: OK
      Connecting to node 127.0.0.1:7005: OK
      >>> Performing hash slots allocation on 6 nodes...
      Using 3 masters:
      127.0.0.1:7000
      127.0.0.1:7001
      127.0.0.1:7002
      Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
      Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
      Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
      M: 3bdc356afba36daeb605eed62d94e3fef93e83ac 127.0.0.1:7000
         slots:0-5460 (5461 slots) master
      M: 761d8891d8f21d3a43741466bcaac9339ac5ad8b 127.0.0.1:7001
         slots:5461-10922 (5462 slots) master
      M: ae7c6ac13cec007a999a1a71f3c78a69688d26b5 127.0.0.1:7002
         slots:10923-16383 (5461 slots) master
      S: ce0746ff514a1a738a7c901e193433cce23d28fe 127.0.0.1:7003
         replicates 3bdc356afba36daeb605eed62d94e3fef93e83ac
      S: bb0a142e2429035e118f0bb078d5ee2f94e83227 127.0.0.1:7004
         replicates 761d8891d8f21d3a43741466bcaac9339ac5ad8b
      S: 4eb893c9b052e10e728e18889c16ef6e573a7fe9 127.0.0.1:7005
         replicates ae7c6ac13cec007a999a1a71f3c78a69688d26b5
      Can I set the above configuration? (type 'yes' to accept):  #这里输入yes摁回车
    
      Can I set the above configuration? (type 'yes' to accept): yes
      >>> Nodes configuration updated
      >>> Assign a different config epoch to each node
      >>> Sending CLUSTER MEET messages to join the cluster
      Waiting for the cluster to join....
      >>> Performing Cluster Check (using node 127.0.0.1:7000)
      M: 3bdc356afba36daeb605eed62d94e3fef93e83ac 127.0.0.1:7000
         slots:0-5460 (5461 slots) master
      M: 761d8891d8f21d3a43741466bcaac9339ac5ad8b 127.0.0.1:7001
         slots:5461-10922 (5462 slots) master
      M: ae7c6ac13cec007a999a1a71f3c78a69688d26b5 127.0.0.1:7002
         slots:10923-16383 (5461 slots) master
      M: ce0746ff514a1a738a7c901e193433cce23d28fe 127.0.0.1:7003
         slots: (0 slots) master
         replicates 3bdc356afba36daeb605eed62d94e3fef93e83ac
      M: bb0a142e2429035e118f0bb078d5ee2f94e83227 127.0.0.1:7004
         slots: (0 slots) master
         replicates 761d8891d8f21d3a43741466bcaac9339ac5ad8b
      M: 4eb893c9b052e10e728e18889c16ef6e573a7fe9 127.0.0.1:7005
         slots: (0 slots) master
         replicates ae7c6ac13cec007a999a1a71f3c78a69688d26b5
      [OK] All nodes agree about slots configuration.
      >>> Check for open slots...
      >>> Check slots coverage...
      [OK] All 16384 slots covered.
    
  • 测试集群环境安装配置成功

测试集群环境

  • 使用./redis-cli -c -p 7000 命令连接集群环境:

      [root@zhangtao bin]# cd /opt/redis-3.0.5/bin/
      [root@zhangtao bin]# ./redis-cli -c -p 7000
      127.0.0.1:7000> set foo bar
      -> Redirected to slot [12182] located at 127.0.0.1:7002
      OK
      127.0.0.1:7002> set hello world
      -> Redirected to slot [866] located at 127.0.0.1:7000
      OK
      127.0.0.1:7000> get hello
      "world"
      127.0.0.1:7000> get foo
      -> Redirected to slot [12182] located at 127.0.0.1:7002
      "bar"
      127.0.0.1:7002> 
      127.0.0.1:7002> quit
    
  • redis对集群的支持是非常基本的, 所以它总是依靠 Redis 集群节点来将它转向(redirect)至正确的节点。

参考资料

http://www.th7.cn/Program/Ruby/201501/365797.shtml

http://www.redis.cn/topics/cluster-tutorial.html





blog comments powered by Disqus
Fork me on GitHub