Redis Cluster Cluster detailed description and pseudo-distributed Cluster Setup

1 What is the Redis-Cluster

Why build Redis cluster. Redis data is stored in memory, and the memory of our computers are generally not large, which means that Redis is not suitable for large data storage for storing large data is Hbase Hadoop ecosystem or MogoDB. Redis is more suitable for highly concurrent processing, storage capacity of one device is very limited, but multiple devices to collaborate, you can let the memory many times larger, which requires use of the cluster.

There are a variety of ways to build Redis cluster, for example using a client patch, Twemproxy, Codis and so on, but after redis 3.0 version supports redis-cluster cluster , it is the solution Redis Crown, Redis-Cluster-free center structure, save data for each node and the entire state of the cluster, each node and all other nodes. Redis-cluster architecture which is shown below:

 

Redis client node is connected, without an intermediate layer proxy. The client need not be connected to any available cluster node is connected to all the nodes in the cluster.

All nodes are interconnected and redis (PING-PONG mechanism) , the internal transmission speed using the binary protocol and bandwidth optimization.

1.2 Distribution storage mechanism - groove

(1) redis-cluster mapping all of the physical nodes to [0-16383] slot, cluster responsible for maintaining

node<->slot<->value

(2) built into the cluster 16384 Redis hash slot, when a key-value to be placed in the cluster Redis, Redis crc16 algorithm key using the first calculated result, and the results of the remainder number of 16384, such that each key will correspond to a slot number in the hash between 0-16383, redis will be substantially equal to the number of nodes according to hash slot map to a different node.

    For example three nodes: the value of the distribution channel is as follows:

SERVER1:  0-5460

SERVER2:  5461-10922

SERVER3:  10923-16383

1.3 Fault Tolerance - Voting

(1) the electoral process is a cluster of all master involved, if more than half of the master node to communicate with the failure of more than (cluster-node-timeout), believes that a node failure, automatically triggers a failover operation   failed node from the corresponding node automatically upgraded to The master node

(2) When the entire cluster is unavailable (cluster_state: fail)? 

If the cluster master hang arbitrary, and is not the current master Slave. Enter the cluster fail state, as may be appreciated that slot cluster mapping [0-16383] enters fail state when not completed.

 

2 structures Redis-Cluster

2.1 Construction request

Redis need six servers. Build a pseudo-cluster.

Redis need six instances.

You need to run at different ports 7001-7006

2.2 Preparations

(1) This step is omitted installed gcc []

Redis is c language development. Install redis need c compiler environment language. If you do not need to install gcc online.

yum install gcc-c++

(2) using the command yum install ruby ​​(ruby script we need to achieve Cluster Setup) [This step is omitted]

yum install ruby

yum install rubygems 

(3) Source Package redis upload linux system, extract the source package redis

(4) compile redis source code into the source folder redis

make

 See the following output showing successful compilation

(5) create the directory / usr / local / redis-cluster directory, the installation redis example 6, are installed in the directory

/usr/local/redis-cluster/redis-1

/usr/local/redis-cluster/redis-2

/usr/local/redis-cluster/redis-3

/usr/local/redis-cluster/redis-4

/usr/local/redis-cluster/redis-5

/usr/local/redis-cluster/redis-6

 

以第一个redis实例为例,命令如下

make install PREFIX=/usr/local/redis-cluster/redis-1

出现此提示表示成功,按此方法安装其余5个redis实例 

(6)复制配置文件  将 /redis-3.0.0/redis.conf 复制到redis下的bin目录下

[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-1/bin

[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-2/bin

[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-3/bin

[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-4/bin

[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-5/bin

[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-6/bin

2.3配置集群 

(1)修改每个redis节点的配置文件redis.conf

修改运行端口为7001 (7002 7003 .....)

 

将cluster-enabled yes 前的注释去掉(632行)

 

(2)启动每个redis实例

以第一个实例为例,命令如下

cd /usr/local/redis-cluster/redis-1/bin/

./redis-server redis.conf

把其余的5个也启动起来,然后查看一下是不是都启动起来了

[root@localhost ~]# ps -ef | grep redis

root     15776 15775  0 08:19 pts/1    00:00:00 ./redis-server *:7001 [cluster]

root     15810 15784  0 08:22 pts/2    00:00:00 ./redis-server *:7002 [cluster]

root     15831 15813  0 08:23 pts/3    00:00:00 ./redis-server *:7003 [cluster]

root     15852 15834  0 08:23 pts/4    00:00:00 ./redis-server *:7004 [cluster]

root     15872 15856  0 08:24 pts/5    00:00:00 ./redis-server *:7005 [cluster]

root     15891 15875  0 08:24 pts/6    00:00:00 ./redis-server *:7006 [cluster]

root     15926 15895  0 08:24 pts/7    00:00:00 grep redis

(3)上传redis-3.0.0.gem ,安装 ruby用于搭建redis集群的脚本。 

[root@localhost ~]# gem install redis-3.0.0.gem

Successfully installed redis-3.0.0

1 gem installed

Installing ri documentation for redis-3.0.0...

Installing RDoc documentation for redis-3.0.0...

进入redis源码目录中的src目录  执行下面的命令

(4)使用 ruby 脚本搭建集群。

 

./redis-trib.rb create --replicas 1 192.168.25.140:7001 192.168.25.140:7002 192.168.25.140:7003

192.168.25.140:7004 192.168.25.140:7005 192.168.25.140:7006

出现下列提示信息

>>> Creating cluster

Connecting to node 192.168.25.140:7001: OK

Connecting to node 192.168.25.140:7002: OK

Connecting to node 192.168.25.140:7003: OK

Connecting to node 192.168.25.140:7004: OK

Connecting to node 192.168.25.140:7005: OK

Connecting to node 192.168.25.140:7006: OK

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.25.140:7001

192.168.25.140:7002

192.168.25.140:7003

Adding replica 192.168.25.140:7004 to 192.168.25.140:7001

Adding replica 192.168.25.140:7005 to 192.168.25.140:7002

Adding replica 192.168.25.140:7006 to 192.168.25.140:7003

M: 1800237a743c2aa918ade045a28128448c6ce689 192.168.25.140:7001

   slots:0-5460 (5461 slots) master

M: 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d 192.168.25.140:7002

   slots:5461-10922 (5462 slots) master

M: 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 192.168.25.140:7003

   slots:10923-16383 (5461 slots) master

S: c2a39a94b5f41532cd83bf6643e98fc277c2f441 192.168.25.140:7004

   replicates 1800237a743c2aa918ade045a28128448c6ce689

S: b0e38d80273515c84b1a01820d8ecee04547d776 192.168.25.140:7005

   replicates 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d

S: 03bf6bd7e3e6eece5a02043224497c2c8e185132 192.168.25.140:7006

   replicates 436e88ec323a2f8bb08bf09f7df07cc7909fcf81

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.25.140:7001)

M: 1800237a743c2aa918ade045a28128448c6ce689 192.168.25.140:7001

   slots:0-5460 (5461 slots) master

M: 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d 192.168.25.140:7002

   slots:5461-10922 (5462 slots) master

M: 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 192.168.25.140:7003

   slots:10923-16383 (5461 slots) master

M: c2a39a94b5f41532cd83bf6643e98fc277c2f441 192.168.25.140:7004

   slots: (0 slots) master

   replicates 1800237a743c2aa918ade045a28128448c6ce689

M: b0e38d80273515c84b1a01820d8ecee04547d776 192.168.25.140:7005

   slots: (0 slots) master

   replicates 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d

M: 03bf6bd7e3e6eece5a02043224497c2c8e185132 192.168.25.140:7006

   slots: (0 slots) master

   replicates 436e88ec323a2f8bb08bf09f7df07cc7909fcf81

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.
View Code

4.3连接Redis-Cluster 

4.3.1客户端工具连接

Redis-cli 连接集群:

redis-cli -h 主机ip -p 端口(集群中任意端口) -c

测试值的存取:-c:代表连接的是 redis 集群

(1)从本地连接到集群redis  使用7001端口 加 -c 参数

(2)存入name值为abc ,系统提示此值被存入到了7002端口所在的redis (槽是5798)

(3)提取name的值,可以提取。

(4)退出(quit)

(5)再次以7001端口进入 ,不带-cR

(6)查询name值,无法获取,因为值在7002端口的redis上

(7)我们以7002端口进入,获取name值发现是可以获取的,而以其它端口进入均不能获取

 

Guess you like

Origin www.cnblogs.com/alexzhang92/p/10497650.html