redis一个比较大的改变,redis3.0.1 稳定版本发布,支持集群。redis 集群配置 reids Cluster

本文介绍了配置Redis 3.0.1集群的详细过程,包括遇到的Zlib问题的解决方案,以及集群创建、节点添加与移除、故障转移等操作。同时概述了Redis Cluster的主要特性,如数据分片、主从模型和一致性保证。通过这个实战指南,读者将了解如何搭建和管理Redis Cluster。
摘要由CSDN通过智能技术生成

先说一下在配置的流程和遇到的一些问题:

  • 安装redis

$ wget http://download.redis.io/releases/redis-3.0.1.tar.gz
$ tar xzf redis-3.0.1.tar.gz
$ cd redis-3.0.1
$ make
$ src/redis-server
$ src/redis-cli
redis> set 11 22
OK
redis> get 11
"22" 
  •  配置redis.conf 修改如下参数

port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

  • 进入到redis目录下,注意:

mkdir cluster-test
cd cluster-test
mkdir 6379 6380 6381 6382 6383 6384 #这里需要注意,最少是6个,copy之前修改的redis.conf 文件到对应的文件夹中,修改每个conf中的端口号
<code>cd 6379
</code><code>../redis-server ./redis.conf</code> #启动redis实例
  • Ok,redis这边先不管了,接下来需要装几个有依赖的软件,如下操作必须使用系统管理员(root)进行登陆和安装:

  • 安装 Ruby

检查是否已经安装过:rpm -qa |grep ruby  有:rpm -e 软件名 无:继续下面

安装ruby(安装ruby到 /usr/local/ruby):

$mkdir /usr/local/ruby
<pre name="code" class="python">$wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz
$tar -xzvf ruby-2.2.2.tar.gz
<pre name="code" class="python">$cd ruby-2.2.2

 $./configure -prefix=/usr/local/ruby 
  
$make
$make install
$cp ruby /usr/local/bin
$ruby -v #验证是不是安装成功 
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
 
  

  • 安装 Ruby Gems (https://rubygems.org/pages/download)

$wget http://production.cf.rubygems.org/rubygems/rubygems-2.4.7.tgz
$tar -xzvf rubygems-2.4.7.tgz
$cd rubygems-2.4.7
$ruby setup.rb
$cp bin/gem /usr/local/bin
$gem  -v

  • 依赖的软件安装完成,接下来开始创建集群 (Creating the cluster)

$cd /data/redis-3.0.1/src
$./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
$..... <code>[OK] All 16384 slots covered</code> #最后提示这样的语句表示成功

在执行上面的时候可能会提示如下错误:

/opt/cluster_test/redis-unstable/src# src/redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381127.0.0.1:6382127.0.0.1:6383 127.0.0.1:6384
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- redis (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /opt/cluster_test/redis-unstable/src/redis-trib.rb:25:in `<main>'

研究了很久,发现ruby是要访问redis的,但是并没有安装redis的第三方小插件,所以需要在gems中安装redis的访问接口:

gem install redis --version 3.0.0  
#由于源的原因,可能下载失败,就手动下载下来安装   
#download地址:http://rubygems.org/gems/redis/versions/3.0.0   
gem install -l /data/soft/redis-3.0.0.gem  

安装的时候报错了吧,提示说什么 Zlib 没找到什么的:

gem install redis
ERROR:  Loading command: install (LoadError)
    no such file to load -- zlib
ERROR:  While executing gem ... (NameError)
    uninitialized constant Gem::Commands::InstallCommand

解决办法是:

进入ruby源码文件夹 
安装ruby自身提供的zlib包 
#cd ext/zlib
#ruby ./extconf.rb
#make
#make install

  • 结束,可以尽情的玩了:

$ redis-cli -c -p 6379
redis 127.0.0.1:6379> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:6380
OK
redis 127.0.0.1:6380> set hello world
-> Redirected to slot [866] located at 127.0.0.1:6381
OK
redis 127.0.0.1:6379> get foo
-> Redirected to slot [12182] located at 127.0.0.1:6382
"bar"
redis 127.0.0.1:6379> get hello
-> Redirected to slot [866] located at 127.0.0.1:7379
"world" 

如上是今天安装的全过程,其中卡在zlib这里和在gems这里浪费了很多时间,在此记录下,各位大神如有用到,有问题也可与我交流 QQ:154833488

 下面是官网的介绍和配置的使用:

Redis cluster tutorial

This document is a gentle introduction to Redis Cluster, that does not use complex to understand distributed systems concepts. It provides instructions about how to setup a cluster, test, and operate it, without going into the details that are covered in the Redis Cluster specification but just describing how the system behaves from the point of view of the user.

However this tutorial tries to provide information about the availability and consistency characteristics of Redis Cluster from the point of view of the final user, stated in a simple to understand way.

Note this tutorial requires Redis version 3.0 or higher.

If you plan to run a serious Redis Cluster deployment, the more formal specification is a suggested reading, even if not strictly required. However it is a good idea to start from this document, play with Redis Cluster some time, and only later read the specification.

*Redis Cluster 101

Redis Cluster provides a way to run a Redis installation where data is automatically sharded across multiple Redis nodes.

Redis Cluster also provides some degree of availability during partitions, that is in practical terms the ability to continue the operations when some nodes fail or are not able to communicate. However the cluster stops to operate in the event of larger failures (for example when the majority of masters are unavailable).

So in practical terms, what you get with Redis Cluster?

  • The ability to automatically split your dataset among multiple nodes.
  • The ability to continue operations when a subset of the nodes are experiencing failures or are unable to communicate with the rest of the cluster.

*Redis Cluster TCP ports

Every Redis Cluster node requires two TCP connections open. The normal Redis TCP port used to serve clients, for example 6379, plus the port obtained by adding 10000 to the data port, so 16379 in the example.

This second high port is used for the Cluster bus, that is a node-to-node communication channel using a binary protocol. The Cluster bus is used by nodes for failure detection, configuration update, failover authorization and so forth. Clients should never try to communicate with the cluster bus port, but always with the normal Redis command port, however make sure you open both ports in your firewall, otherwise Redis cluster nodes will be not able to communicate.

The command port and cluster bus port offset is fixed and is always 10000.

Note that for a Redis Cluster to work properly you need, for each node:

  1. The normal client communication port (usually 6379) used to communicate with clients to be open to all the clients that need to reach the cluster, plus all the other cluster nodes (that use the client port for keys migrations).
  2. The cluster bus port (the client port + 10000) must be reachable from all the other cluster nodes.

If you don't open both TCP ports, your cluster will not work as expected.

The cluster bus uses a different, binary protocol, for node to node data exchange, which is more suited to exchange information between nodes using little bandwidth and processing time.

*Redis Cluster data sharding

Redis Cluster does not use consistent hashing, but a different form of sharding where every key is conceptually part of what we call anhash slot.

There are 16384 hash slots in Redis Cluster, and to compute what is the hash slot of a given key, we simply take the CRC16 of the key modulo 16384.

Every node in a Redis Cluster is responsible of a subset of the hash slots, so for example you may have a cluster with 3 nodes, where:

  • Node A contains hash slots from 0 to 5500.
  • Node B contains hash slots from 5501 to 11000.
  • Node C contains hash slots from 11001 to 16384.

This allows to add and remove nodes in the cluster easily. For example if I want to add a new node D, I need to move some hash slot from nodes A, B, C to D. Similarly if I want to remove node A from the cluster I can just move the hash slots served by A to B and C. When the node A will be empty I can remove it from the cluster completely.

Because moving hash slots from a node to another does not require to stop operations, adding and removing nodes, or changing the percentage of hash slots hold by nodes, does not require any downtime.

Redis Cluster supports multiple key operations as long as all the keys involved into a single command execution (or whole transaction, or Lua script execution) all belong to the same hash slot. The user can force multiple keys to be part of the same hash slot by using a concept called hash tags.

Hash tags are documented in the Redis Cluster specification, but the gist is that if there is a substring between {} brackets in a key, only what is inside the string is hashed, so fo examplethis{foo}key andanother{foo}key are guaranteed to be in the same hash slot, and can be used together in a command with multiple keys as arguments.

*Redis Cluster master-slave model

In order to remain available when a subset of master nodes are failing or are not able to communicate with the majority of nodes, Redis Cluster uses a master-slave model where every hash slot has from 1 (the master itself) to N replicas (N-1 additional slaves nodes).

In our example cluster with nodes A, B, C, if node B fails the cluster is not able to continue, since we no longer have a way to serve hash slots in the range 5501-11000.

However if when the cluster is created (or at a latter time) we add a slave node to every master, so that the final cluster is composed of A, B, C that are masters nodes, and A1, B1, C1 that are slaves nodes, the system is able to continue if node B fails.

Node B1 replicates B, and B fails, the cluster will promote node B1 as the new master and will continue to operate correctly.

However note that if nodes B and B1 fail at the same time Redis Cluster is not able to continue to operate.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值