集群架构

(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.

(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot(插槽)上,cluster 负责维护node<->slot<->value

准备环境

创建一个目录,存放集群的配置文件
[root@master redis]# mkdir redis-cluster
[root@master redis]# cd redis-cluster/
[root@master redis-cluster]# mkdir 6380
[root@master redis-cluster]# mkdir 6381
[root@master redis-cluster]# mkdir 6382
[root@master redis-cluster]# pwd
/opt/redis/redis-cluster

拷贝安装源码中的redis.conf刚创建好的三个目录中

[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6380/
[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6381/
[root@master redis-cluster]# cp /opt/redis/redis-3.2.1/redis.conf /opt/redis/redis-cluster/6382/

分别进入这三个目录,修改配置文件redis.conf

1、将端口分别设置为:6380、6381、6382
2、设置pidfile文件为不同的路径
3、开启集群,cluster-enabled yes
4、指定集群的配置文件,cluster-config-file "nodes-xxxx.conf"
以6380为例:
[root@master redis-master-slave]# vim 6380/redis.conf
1
2
3
4
5
6
port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
cluster-enabled yes
cluster-config-file nodes-6380.conf
dir /opt/redis/redis-cluster/6380/
启动redis-server
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6380/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6381/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6382/redis.conf

[root@master redis-cluster]# ps -ef | grep redis

root      2948     1  0 11:11 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6380 [cluster]                
root      2970     1  0 11:16 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6381 [cluster]                
root      2974     1  0 11:16 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6382 [cluster]  

创建redis集群

创建集群要使用redis-trib.rb脚本,执行此脚本查看使用方法
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
 del-node        host:port node_id
 info            host:port
 rebalance       host:port
                 --timeout <arg>
                 --auto-weights
                 --pipeline <arg>
                 --threshold <arg>
                 --weight <arg>
                 --simulate
                 --use-empty-masters
 check           host:port
 reshard         host:port
                 --timeout <arg>
                 --to <arg>
                 --pipeline <arg>
                 --from <arg>
                 --slots <arg>
                 --yes
 call            host:port command arg arg .. arg
 import          host:port
                 --copy
                 --from <arg>
                 --replace
 help            (show this help)
 set-timeout     host:port milliseconds
 create          host1:port1 ... hostN:portN
                 --replicas <arg>
 add-node        new_host:new_port existing_host:existing_port
                 --slave
                 --master-id <arg>
 fix             host:port
                 --timeout <arg>
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

使用redis-trib.rb create创建集群,--replicas 0:指定了从数据的数量为0

注意:这里不能使用127.0.0.1,否则在Jedis客户端使用时无法连接到!

[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb create --replicas 0 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382

>>> Creating cluster
[ERR] Sorry, can't connect to node 192.168.56.101:6380

修改刚创建的三个redis.conf配置文件中的bind

bind 192.168.56.101

重启redis-server

[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6380/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6381/redis.conf
[root@master redis-cluster]# /usr/local/bin/redis-server /opt/redis/redis-cluster/6382/redis.conf
[root@master redis-cluster]# ps -ef | grep redis
root      3756     1  0 13:11 ?        00:00:00 /usr/local/bin/redis-server 192.168.56.101:6380 [cluster]          
root      3760     1  0 13:11 ?        00:00:00 /usr/local/bin/redis-server 192.168.56.101:6381 [cluster]          
root      3764     1  0 13:11 ?        00:00:00 /usr/local/bin/redis-server 192.168.56.101:6382 [cluster]

再创建集群

[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb create --replicas 0 192.168.56.101:6380 192.168.56.101:6381 192.168.56.101:6382
>>> Creating cluster
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
192.168.56.101:6380
192.168.56.101:6381
192.168.56.101:6382
M: 8c45a68a798aed7c1ed5e636f2899709717952c8 192.168.56.101:6380
  slots:0-5460 (5461 slots) master
M: c9267b81af4bb2c186d07c5ebc777a4b8551069a 192.168.56.101:6381
  slots:5461-10922 (5462 slots) master
M: 16c6db308b37be4d4dd337cd62ea1b676672e096 192.168.56.101:6382
  slots:10923-16383 (5461 slots) master
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 192.168.56.101:6380)
M: 8c45a68a798aed7c1ed5e636f2899709717952c8 192.168.56.101:6380
  slots:0-5460 (5461 slots) master
M: c9267b81af4bb2c186d07c5ebc777a4b8551069a 192.168.56.101:6381
  slots:5461-10922 (5462 slots) master
M: 16c6db308b37be4d4dd337cd62ea1b676672e096 192.168.56.101:6382
  slots:10923-16383 (5461 slots) master【集群及槽信息】
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试集群

[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380
192.168.56.101:6380> keys *
(empty list or set)
192.168.56.101:6380> set k1 123
(error) MOVED 12706 192.168.56.101:6382
因为k1的hash槽信息是在6382上,现在使用redis-cli连接的6380,无法完成set操作,需要客户端跟踪重定向,使用-c参数
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380 -c
192.168.56.101:6380> set k1 123
-> Redirected to slot [12706] located at 192.168.56.101:6382
OK

获取数据

k1的hash槽信息在6382上,在6382上可以直接获取,在其他节点还是要重定向去获取
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6380 -c
192.168.56.101:6380> get k1
-> Redirected to slot [12706] located at 192.168.56.101:6382
"123"
[root@master hadoop]# /usr/local/bin/redis-cli -h 192.168.56.101 -p 6382 -c
192.168.56.101:6382> get k1
"123"

执行redis-trib.rb脚本可能遇到的问题

1、/usr/bin/env: ruby: 没有那个文件或目录
[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/usr/bin/env: ruby: 没有那个文件或目录
解决方案:由于redis-trib.rb是用ruby语言编写,所以需要安装ruby环境,这里推荐使用yum install ruby

2、redis-trib.rb:24:in `require': no such file to load -- rubygems

[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/opt/redis/redis-3.2.1/src/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from /opt/redis/redis-3.2.1/src/redis-trib.rb:24
解决方案:yum install rubygems

3、/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)

[root@master redis-cluster]# /opt/redis/redis-3.2.1/src/redis-trib.rb
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /opt/redis/redis-3.2.1/src/redis-trib.rb:25
解决方案:缺少redis的接口,使用gem install redis或gem install redis --version 3.2.1
[root@master redis-cluster]# gem install redis --version 3.2.1
Successfully installed redis-3.2.1
1 gem installed
Installing ri documentation for redis-3.2.1...
Installing RDoc documentation for redis-3.2.1...

备注:

如果连接不上gem服务器安装,就手动下载并安装
gem install redis --version 3.0.0  
ERROR:  Could not find a valid gem 'redis' (= 3.0.0) in any repository
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
需要手工下载并安装:
wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
gem install -l ./redis-3.2.1.gem

书生参考于网络整理

如非特别注明,本站内容均为领悟书生原创,转载请务必注明作者和原始出处。
本文地址:http://www.656463.com/redis/aAvu2u.htm

redis 集群环境搭建-redis集群管理的更多相关文章

  1. Spark集群环境搭建——Hadoop集群环境搭建

    Spark其实是Hadoop生态圈的一部分,需要用到Hadoop的HDFS.YARN等组件. 为了方便我们的使用,Spark官方已经为我们将Hadoop与scala组件集成到spark里的安装包,解压 ...

  2. HBase —— 集群环境搭建

    一.集群规划 这里搭建一个3节点的HBase集群,其中三台主机上均为Regin Server.同时为了保证高可用,除了在hadoop001上部署主Master服务外,还在hadoop002上部署备用的 ...

  3. Spark集群环境搭建——部署Spark集群

    在前面我们已经准备了三台服务器,并做好初始化,配置好jdk与免密登录等.并且已经安装好了hadoop集群. 如果还没有配置好的,参考我前面两篇博客: Spark集群环境搭建--服务器环境初始化:htt ...

  4. 【redis】 linux 下redis 集群环境搭建

    Redis集群 (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下) 127.0.0.1:63791 ...

  5. Redis.之.环境搭建(集群)

    Redis.之.环境搭建(集群) 现有环境: /u01/app/ |- redis # 单机版 |- redis-3.2.12    # redis源件 所需软件:redis-3.0.0.gem -- ...

  6. redis哨兵集群环境搭建

    一.哨兵的介绍 哨兵(sentinal)是redis集群架构中非常重要的一个组件,主要功能如下: 集群监控,负责监控redis master和slave进程是否正常工作 消息通知,如果某个redis实 ...

  7. Windows 环境搭建Redis集群(win 64位)

    转: http://blog.csdn.net/zsg88/article/details/73715947 参考:https://www.cnblogs.com/tommy-huang/p/6240 ...

  8. Windows 环境搭建Redis集群

    环境以及引用资料 1.windows server 2008 r2 enterprise  (木有办法,公司的服务器全是如此,就这种环境搭建吧) 2.redis官方资料下载: https://redi ...

  9. Windows及Linux环境搭建Redis集群

    一.Windows环境搭建Redis集群 参考资料:Windows 环境搭建Redis集群 二.Linux环境搭建Redis集群 参考资料:Redis Cluster的搭建与部署,实现redis的分布 ...

随机推荐

  1. hdu3247Resource Archiver(ac自动机+spfa)

    链接 这题没想到怎么做,问了下p队长,大悟.. 先求出任意两串的在trie树上的最短距离,期间是不能走到不合法的地方,我是用spfa求得,在更新和加入节点时判断一下是不是合法位置. 求出最短距离之后, ...

  2. JavaScript学习笔记及知识点整理_3

    1.js的事件冒泡及阻止方法:事件冒泡的概念:在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事 ...

  3. 使用Android点击按钮跳转页面

    1.首先新建一个Android工程,命名为MyApp(名字可以自己随意起); 2.以原有的MainActivity.java文件为登录界面,然后在src文件中的包上面右击选择New目录下的Other中 ...

  4. HBase之计数器

    HBase计数器 #创建counters表 列族['daily','weekly','monthly'] hbase(main):001:0> create 'counters','daily' ...

  5. Android之打log

    Android之打log 1.在代码中加上自己的log 2,模块编译mm -B或者./mk mm/mmm packages/apps/Contacts/ 3编译成功后install或者push生成的a ...

  6. JS Map 和 List 的简单实现代码

    javascript中是没有map和list 结构的. 本篇文章是对在JS中Map和List的简单实现代码进行了详细的分析介绍,需要的朋友参考下 代码如下: /* * MAP对象,实现MAP功能 *  ...

  7. Ipad Safari iframe cookie 当浏览器默认禁用第三方COOKIE

    前一阵子,我们发现高版本的Safari中默认会阻止第三方cookie,如下图所示. 问题 什么是第三方cookie呢?在访问一个网站A时,网站A算作第一方,如果网站A中引用了另一个网站X(网站X的域名 ...

  8. Objective-C( 语法二)

     分类(Category):可以给某一个类扩充一些方法(不修改原来类的代码)  作用:在不改变原来类内容基础上,可以为类增加一些方法  使用注意: 1. 只能增加方法,不能增加成员变量 2. 分类方法 ...

  9. C# 跨线程调用控件

    在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应.  同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线程间操作无效 第一种办法:禁 ...

  10. HDU 2871 Memory Control

    一共4种操作 其中用线段树 区间合并,来维护连续空的长度,和找出那个位置.其他用vector维护即可 #include<cstring> #include<cstdio> #i ...