集群架构

(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. solr 6.1 服务端 tomcat 搭建及调用

    一.下载 apache solr6.1.0 最新版本zip,解压缩生成一个solr6.1.0文件夹 二.安装 1.在d:/projects下新建一个solr 2.把solr6.1.0/server/s ...

  2. 【转】PDF电子书分享

    http://www.voidcn.com/blog/u013830841/article/p-4343018.html http://www.voidcn.com/blog/u013830841/a ...

  3. U-Boot

    U-Boot U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.从FADSROM.8xxROM.PPCBOOT逐步发展演化而来.其源码目录.编译形式与Li ...

  4. 最流行的JavaScript库jQuery不再支持旧版IE

    直到JQuery2.0的发布,流行的jQuery JavaScript库到了一个重要里程碑.2.0版本比前任版本在大小上缩减了12%,但是更大的新闻是,jQuery 2.0不在对IE6,7,8三个版本 ...

  5. 使用NodeJS、GruntCLI遇到的问题

    运行环境CMD NODEJS版本v0.8.4  node --version 需求:需要用到Grunt的JS编译功能,各位别笑我. 使用代理 npm config set proxy http://i ...

  6. mongostat 3.2指标详解

    存储引擎:wiredTiger /usr/local/mongodb-3.2.8/bin/mongostat  -uroot -pcEqHuoqiJYhjVpuL --host 127.0.0.1   ...

  7. eclipse配置ros cakin编译环境

    先安装eclipse,之前的博客:http://www.cnblogs.com/CZM-/p/5942435.html Catkin-y 方法使用catkin无法make eclipse工程,生成pr ...

  8. webix源码阅读

    最近在用webix,需要一个类似九宫格的监控界面.自带的控件里没有,于是萌生出做一个Custom Component的需求.不过webix关于自定义控件的文档比较少,官方只有一篇<Creatin ...

  9. BCG界面库下的Windows8 UI界面样式www.webui8.com

    BCG界面库下的Windows8 UI界面样式(Metro风格)控件主要有以下一些功能: 规则的大块磁贴 支持完整键盘导航 Tile组 标题(Caption) 标题按钮(Caption buttons ...

  10. SQL Server2012关于表内事项出现次数降序排列(存储过程)

    USE [growup] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[S_GetRanking ...