集群架构

(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. Unity5 GI与PBS渲染从用法到着色代码

    http://www.cnblogs.com/zhouxin/p/5168632.html 本文主要介绍Untiy5以后的GI,PBS,以及光源探头,反射探头的用法以及在着色器代码中如何发挥作用,GI ...

  2. Linux编程之驱动

    增加自己写的驱动程序:http://blog.chinaunix.net/uid-23065002-id-115739.html http://os.51cto.com/art/201108/2840 ...

  3. SPSS数据分析—单因素及多因素方差分析

    t检验可以解决单样本.两个样本时的均值比较问题,但是对于两个以上样本,就不能用t检验了,而要使用方差分析.t检验是借助t分布,方差分析是借助F分布,基于变异分解的思想进行. 在算法上,由于线性模型的引 ...

  4. App_GlobalResources.afvubzdv.resources.dll”--“拒绝访问。“

    在使用ArcGIS Viewer for Silverlight创建应用程序的时候有时会出现编译错误 前面的忘了  最后是App_GlobalResources.afvubzdv.resources. ...

  5. ListView在列表的头部和底部添加布局——addHeaderView,addFooterView

    addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 以addHeaderView为例: View he ...

  6. Java类的成员函数调用顺序

    class A { public A() { System.out.println("----------A 构造-------------"); } static void sb ...

  7. Valid Sudoku

    理解题目的意思后这题不难.扫描一遍数独输入并按照要求进行判断就可以了.提交了两次,第一次用了stl的set,第二次本来想借助位运算的,想想觉得有些操作略显麻烦,因此用整数数组代替.代码如下: 解法一: ...

  8. A web crawler design for data mining

    Abstract The content of the web has increasingly become a focus for academic research. Computer prog ...

  9. iOS ShareSDK 使用

    流量精灵软件中,也在大部分地方使用到了shareSDK 这个三方开源库.具体的有两种需求 a.弹出所有分享模块 b.只弹出指定的平台:如微信朋友圈和QQ . 配置方法,三方库中也很详细,这里我只有写出 ...

  10. jQueryMobile控件之展开与合并

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...