Redis summary (eight) How to build a highly available cluster Redis

Redis previous summary of some basic installation and use, we can see here this series of articles Redis: https://www.cnblogs.com/zhangweizhong/category/771056.html .

Redis clustering capabilities today make up about it. It should be noted, only after Redis 3.0 cluster feature, downloading Redis when the attention of the next version. 

1. Redis cluster principle

Take a look at redis-cluster architecture diagram:

 

Architectural details:

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

fail (2) is to take effect when the node cluster by more than half of the nodes detect the failure.

(3) redis client node is connected, without an intermediate proxy layer, the client need not be connected to all cluster nodes in the cluster connected to an available node.

(4) redis-cluster mapping all of the physical node to [0-16383] slot (slot hash) on, cluster maintains node <-> slot <-> value.

Redis cluster built 16384 slot (slot hash), when the key-value to be placed in a 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.

 

Fault Tolerance: redis-cluster vote

 

 

(1) led the voting process is the master of all involved in the cluster, if more than half of the master node to communicate with the master over (cluster-node-timeout), that the current master node hang.

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

    a: If the cluster master hang any, and the current master does not fail to enter a state slave cluster, the cluster is to be understood that slot mapping [0-16383] enters fail state when not completed. 

    b: If the cluster master hang up more than half, regardless of whether the slave cluster fail to enter the state.

  Note : 1. When the cluster is unavailable, all operations on the cluster to make unavailable, received ((error) CLUSTERDOWN The cluster is down) error.

     2. redis-3.0.0.rc1 added cluster-require-full-coverage parameters, off by default, change the open configuration, allowing the cluster compatible partial failure.

 

2. Install Redis

redis stand-alone installation has been talked about before, there are many tutorials on the web, not repeat it here.

Finally, it provides a source Redis 3.0, Redis cluster scripts and other resources. We can provide me with a version to test.

 

3. Install ruby ​​environment

In redis3.0 source cluster management tool that comes with redis-trib.rb rely ruby ​​environment, you first need to install ruby ​​environment:

1. Install ruby ​​environment

yum install ruby

yum install rubygems   

 

2. Install ruby ​​and redis interface program

Redis-3.0.0.gem to copy the / usr / local

Execution: gem install /usr/local/redis-3.0.0.gem

 

4. Create a cluster

Planning a cluster node

Examples of general Redis cluster are installed from the server, where for convenience of presentation, but represent different redis server with a different port on the same respective main server, as follows:

The master node: 172.16.0.17: 7001,172.16.0.17.3: 7002,172.16.0.17: 7003

From node: 172.16.0.17: 7004,172.16.0.17: 7005,172.16.0.17: 7006

1. created under / usr / local redis-cluster directory, which creates 6 to Redis06 redis instance Redis01 the like, port number: 7001-7006, specific directory as follows:

 

 

2. redis source directory in the src redis-trib.rb copy to redis-cluster directory.

 

3. Modify profile redis.conf redis each instance:

Port 7001   // Here we change the port corresponding to the respective Examples, 7001-7006 
#bind 172.16 . 0.17 
Cluster -Enabled Yes

 

Start each node redis service

Respectively, into Redis01, Redis02, ... Redis06 directory, execute:

./redis-server ./redis.conf

 

Redis View process: ps aux | grep redis

 

More than six Redis instance, it has been started.

 

Create a cluster command execution

Execution Redis-trib.rb , this script is ruby script, it depends ruby environment.

./redis-trib.rb create --replicas 1 172.16.0.17:7001 172.16.0.17:7002 172.16.0.17:7003 172.16.0.17:7004 172.16.0.17:7005  172.16.0.17:7006

说明:

redis集群至少需要3个主节点,每个主节点有一个从节点总共6个节点

replicas指定为1表示每个主节点有一个从节点

 

注意:

如果执行时报如下错误:

[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

解决方法是删除生成的配置文件nodes.conf,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除redis的持久化文件后再重启redis,比如:appendonly.aof、dump.rdb

 

创建集群输出如下:

>>> Creating cluster

Connecting to node 172.16.0.17:7001: OK

Connecting to node 172.16.0.17:7002: OK

Connecting to node 172.16.0.17:7003: OK

Connecting to node 172.16.0.17:7004: OK

Connecting to node 172.16.0.17:7005: OK

Connecting to node 172.16.0.17:7006: OK

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

Using 3 masters:

172.16.0.17:7001

172.16.0.17:7002

172.16.0.17:7003

Adding replica 172.16.0.17:7004 to 172.16.0.17:7001

Adding replica 172.16.0.17:7005 to 172.16.0.17:7002

Adding replica 172.16.0.17:7006 to 172.16.0.17:7003

M: cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 172.16.0.17:7001

   slots:0-5460 (5461 slots) master

M: 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 172.16.0.17:7002

   slots:5461-10922 (5462 slots) master

M: 1a8420896c3ff60b70c716e8480de8e50749ee65 172.16.0.17:7003

   slots:10923-16383 (5461 slots) master

S: 69d94b4963fd94f315fba2b9f12fae1278184fe8 172.16.0.17:7004

   replicates cad9f7413ec6842c971dbcc2c48b4ca959eb5db4

S: d2421a820cc23e17a01b597866fd0f750b698ac5 172.16.0.17:7005

   replicates 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841

S: 444e7bedbdfa40714ee55cd3086b8f0d5511fe54 172.16.0.17:7006

   replicates 1a8420896c3ff60b70c716e8480de8e50749ee65

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 172.16.0.17:7001)

M: cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 172.16.0.17:7001

   slots:0-5460 (5461 slots) master

M: 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841 172.16.0.17:7002

   slots:5461-10922 (5462 slots) master

M: 1a8420896c3ff60b70c716e8480de8e50749ee65 172.16.0.17:7003

   slots:10923-16383 (5461 slots) master

M: 69d94b4963fd94f315fba2b9f12fae1278184fe8 172.16.0.17:7004

   slots: (0 slots) master

   replicates cad9f7413ec6842c971dbcc2c48b4ca959eb5db4

M: d2421a820cc23e17a01b597866fd0f750b698ac5 172.16.0.17:7005

   slots: (0 slots) master

   replicates 4e7c2b02f0c4f4cfe306d6ad13e0cfee90bf5841

M: 444e7bedbdfa40714ee55cd3086b8f0d5511fe54 172.16.0.17:7006

   slots: (0 slots) master

   replicates 1a8420896c3ff60b70c716e8480de8e50749ee65

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

5. 查询集群信息

集群创建成功登陆任意redis结点查询集群中的节点情况。

客户端以集群方式登陆:

./redis-cli -c -h 172.16.0.17 -p 7001 -c //其中-c表示以集群方式连接redis,-h指定ip地址,-p指定端口号

 

查询集群信息

cluster nodes 查询集群结点信息

 

cluster info 查询集群状态信息

 

 

 

6. 添加主节点

集群创建成功后可以向集群中添加节点,下面是添加一个master主节点

1. 增加Redis07实例,参考集群结点规划章节添加一个“7007”目录作为新节点。

 

2. 将Redis07实例添加到集群中,执行下边命令:

./redis-trib.rb add-node  172.16.0.17:7007 172.16.0.17:7001

 

3. 查看集群结点发现7007已添加到集群中:

[root@VM_0_17_centos redis-cluster]# ./redis-trib.rb add-node  172.16.0.17:7007 172.16.0.17:7001
>>> Adding node 172.16.0.17:7007 to cluster 172.16.0.17:7001
Connecting to node 172.16.0.17:7001: OK
.
.
.
Connecting to node
172.16.0.17:7003: OK Connecting to node 172.16.0.17:7005: OK Connecting to node 172.16.0.17:7002: OK Connecting to node 172.16.0.17:7006: OK Connecting to node 172.16.0.17:7004: OK >>> Performing Cluster Check (using node 172.16.0.17:7001) M: 977962f18ec51f363747961137dc903f0078b248 172.16.0.17:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) M: defe4ce0421ee6b50bdab3da58754e98cc80fca3 172.16.0.17:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: a64fc273c0b90700397f5bac2b393dc5587d8ba8 172.16.0.17:7005 slots: (0 slots) slave replicates f277758189eba36c5b5732e9189d8554bf4385cb M: f277758189eba36c5b5732e9189d8554bf4385cb 172.16.0.17:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 4f16e5adcc141ca284d4a9ec6d04f455aee84a48 172.16.0.17:7006 slots: (0 slots) slave replicates defe4ce0421ee6b50bdab3da58754e98cc80fca3 S: 479d5a077893184cd0b05a8e1b6cb5c0625215f4 172.16.0.17:7004 slots: (0 slots) slave replicates 977962f18ec51f363747961137dc903f0078b248 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. Connecting to node 172.16.0.17:7007: OK
.
.
.
>>> Send CLUSTER MEET to node 172.16.0.17:7007 to make it join the cluster. [OK] New node added correctly.

 

哈希槽重新分配

添加完主节点后,集群并不会自动给新添加的节点分配哈希槽,需要我们手动对主节点进行hash槽分配重新分配,这样该主节才可以存储数据。

redis集群有16384个槽,集群中的每个结点分配自已槽,通过查看集群结点可以看到槽占用情况。可以看到刚才添加的主节点Redis07,没有分配哈希槽(slot)。

 

 

 下面就来说说如何给刚添加的Redis01结点分配槽:

第一步:连接上集群

./redis-trib.rb reshard 172.16.0.17:7001   //(连接集群中任意一个可用结点都行)

 

第二步:输入要分配的槽数量

 

 

第三步:输入接收槽的结点id

这里准备给Redis07分配哈希槽,通过cluster nodes查看Redis07节点id为:e8461f9743e186ae8f67ed301d2d971186b1cc93

输入:e8461f9743e186ae8f67ed301d2d971186b1cc93,

 

第四步:输入源结点id

 

如果只是想从单个主节点获取哈希槽,那直接输入相应的节点id即可。

如果想从所有的主节点获取输入:all,

 

第五步:输入yes开始移动槽到目标结点id

 

 

第六步:分配完成之后,可以查询集群节点信息,查看哈希槽是否分配成功。

 

 

 

7. 添加从节点

集群创建成功后可以向集群中添加节点,下面是添加一个slave从节点的命令。

./redis-trib.rb add-node --slave --master-id 主节点id 添加节点的ip和端口 集群中已存在节点ip和端口

 

1. 添加Redis08实例为从结点,将Redis08作为Redis07的从结点。

执行如下命令:

./redis-trib.rb add-node --slave --master-id e8461f9743e186ae8f67ed301d2d971186b1cc93  172.16.0.17:7008 172.16.0.17:7001

e8461f9743e186ae8f67ed301d2d971186b1cc93  是Redis07实例的节点id,可通过cluster nodes查看。

 

注意:如果原来该结点在集群中的配置信息已经生成集群节点的配置文件(如果集群配置cluster-config-file默认指定则为nodes.conf),这时可能会报错:

[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

解决方法是:删除生成的配置文件nodes.conf,删除后再执行./redis-trib.rb add-node指令

 

2. 查看集群中的结点,刚添加的Redis08已经成为Redis07的从节点:

 

 

8. 删除结点:

集群创建成功后可以向集群中删除其中的一个节点,应该怎么删除呢?

执行如下命令即可:

./redis-trib.rb del-node 172.16.0.17 :7005 e8461f9743e186ae8f67ed301d2d971186b1cc93

注意:删除已经分配了有hash槽的节点会失败,报错如下:

[ERR] Node 172.16.0.17:7007 is not empty! Reshard data away and try again.

解决办法就是:将该结点占用的hash槽分配出去,请参考前面哈希槽重新分配的操作,这里就不重复了。

 

最后

以上就已经将如何搭建redis的集群讲完了。

redis3.0源码和ruby脚本,点击这里下载

 

Guess you like

Origin www.cnblogs.com/zhangweizhong/p/11301869.html