Redis Cluster

I believe that through reading Bowen: non-relational databases (NoSQL) - Redis Detailed installation and deployment
can be advantages and how to install a preliminary understanding Redis database, and today we come to know about how Redis database scale out to meet greater access demand.

Redis Cluster 3.0 or later began to support, using the hash slot (hash grooves), may be multiple instances throughout Redis together to form a cluster, the data is distributed to the plurality of clusters of machines.

Redis cluster principle

Redis Cluster is a non-central structure, each node keeps data and the entire state of the cluster. Each node stores information of other nodes, the other nodes to know the groove lock and responsible, and will regularly send heartbeat messages to other nodes, the ability to perceive nodes in the cluster exception. Figure:
Redis Cluster

When a node sends commands associated with the key database client to a cluster either that the key database node to accept the command of accounting to calculate commands to be processed belongs slot, and check whether the slot assigned to himself. If the tank key is located just assigned to the current node, then the node is directly execute this command; slot if the key resides and are not assigned to the current node, the node will be returned to the client a MOVED error to guide the client turned to the correct node, commands executed before and want to send it again.

Cluster roles have Master and Slave. Assigned slots between Master, a total of 16,384 slot. Slave to its designated Master data synchronization, backup. When one of the Master can not provide the service, which will be promoted to Master of Slave Master. To ensure the integrity of the cluster key slot. When one of one of its Master and Slave are invalid, it will lead slot is not complete, the cluster fail, this is the need to participate in the repair manual.

After the cluster to build a good, each node in the cluster will regularly want to send the PING messages to other nodes, if a node receives a message PING PONG message is not returned within the specified time, then the node sends PING message will be marked as suspect offline (PFAIL). Each node to each node in the cluster exchange state information by sending messages to each other. If in a cluster, more than half of the master node will be a node X reported as suspected offline, then the master node X will be marked as offline (FAIL), at the same time will be a cluster on the primary node X to broadcast FAIL message, all receive this message FAIL node X the master node will immediately marked as offline.

When the need to reduce or increase the cluster machines, we need to have slots assigned to a node (source node) is assigned to another node (the target node), and the key for the relevant channel from the source node belongs moved to the destination node.

Re-slicing Redis Redis cluster is the responsibility of the cluster management software redis-trib performed, it does not support automatic fragmentation, and how much Slot own calculations which migrate from the node. In the process of re-fragmentation, the cluster does not offline, and the source node and the destination node may continue to process the request command.

Architecture Details
1. All redis nodes interconnected to each other, the internal binary transmission speed and bandwidth optimization protocol;
failure in the master node 2. The node detects a failure until more than half of the cluster failure;
3. redis client node is connected, not need for an intermediate agent layer, the client need not be connected to all cluster nodes in the cluster connected to an available node;
4.redis-cluster maps all physical nodes [0-16383] to the slot, cluster maintains node <- > slot <-> key;

Redis-cluster elections:

The electoral process is the cluster master of all involved, if more than half of the current master node to communicate with the master node timeout that the current master node hang. Figure:
Redis Cluster

Two occasions when compared with the cluster is unavailable:

  • If the cluster hang any master, and the current master no slave, the cluster fail to enter the state, can be understood as slot cluster mapping [0-16383] fail to enter the state is not complete;
  • If half of the master cluster appears to hang, regardless of whether slave, the cluster will fail to enter the state;
    when the cluster is unavailable, all operations on the cluster are unavailable, received ((error) CLUSTERDOWN The cluster is down) error.

By default, each node in the cluster uses two TCP ports, one 6379, one 16379:

  • 6379 port services in the client's connection;
  • Port 16379 is used for cluster bus;
    i.e. using the binary protocol node to node communication channel. Cluster nodes bus fault detection, configuration updates the failover authorization. If you turn on the firewall, you need to open the port (in this Bowen forced off the firewall).

Redis Cluster Deployment

Once you understand the principle of Redis cluster, set up redis cluster becomes very simple. The experiment set up Redis cluster servers 6, wherein three of Master, three of salve. IP address of the server 6 is 192.168.1.1/24--192.168.1.6/24, servers are centos 7 system. Upload experiment here is not a topology map.

Redis cluster deployment is divided into specific steps:

1. Install Redis and modify configuration files

Redis server on how to build and package acquisition mode can reference Bowen: non-relational databases (NoSQL) - Redis Detailed installation and deployment
according to Bowen can be installed, six are required to install Redis server, each server after installation is complete need to modify the native real IP address, cancel the loopback address (127.0.0.1), specific modifications are as follows:

[root@localhost ~]# vim /etc/redis/6379.conf 
bind 192.168.1.1                                     //改为本机的真是IP地址
daemonize yes
logfile /var/log/redis_6379.log
cluster-enabled yes                                //启用群集
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-require-full-coverage no
//关于群集这几条默认是存在,只不过注释了,取消注释即可!
[root@localhost ~]# /etc/init.d/redis_6379 restart
//重启redis服务
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost ~]# netstat -anpt | grep 6379
tcp        0      0 192.168.1.1:6379        0.0.0.0:*               LISTEN      6497/redis-server 1 
tcp        0      0 192.168.1.1:16379       0.0.0.0:*               LISTEN      6497/redis-server 1 
tcp        0      0 127.0.0.1:6379          127.0.0.1:43304         TIME_WAIT   -                   
//6379端口和16379端口都在监听

The first redis server has been set up is completed, the remaining five different approach, but the profile is its own IP address, do not confused!

2. Use a script to create a cluster

Use a script to create a cluster of ruby, before creating a cluster, you need to install ruby Redis client's operating environment and ruby, and the operation in which a server can be. Download gem command is ahead of redis-3.2.0 gem package provided directly upload server to use!
gem package network disk link: https://pan.baidu.com/s/1Fx7YI-ZwZoHOPm13HYeFKw
extraction code: cj0x

[root@localhost ~]# yum -y install ruby rubygems
//安装ruby的运行环境
[root@localhost ~]# gem install redis --version 3.2.0
Successfully installed redis-3.2.0
Parsing documentation for redis-3.2.0
Installing ri documentation for redis-3.2.0
1 gem installed
//安装ruby的Redis客户端工具——gem命令

Creating a cluster using a script:

[root@localhost ~]# cd /usr/src/redis-3.2.9/src
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.1.1:6379 192.168.1.2:6379 192.168.1.3:6379 192.168.1.4:6379 192.168.1.5:6379 192.168.1.6:6379
                   …………                 //省略部分内容
Can I set the above configuration? (type 'yes' to accept): yes
                   …………                 //省略部分内容
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
//创建群集完成
[root@localhost src]# ./redis-trib.rb check 192.168.1.1:6379
//查看群集状态(可以清楚的看到哪台主机是master、哪台主机时salve)
>>> Performing Cluster Check (using node 192.168.1.1:6379)
M: afb2a0876b9c4c1c19e2bc492e398765bed0a311 192.168.1.1:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 00ed4b0da4f0444c7e2a54a44c2060dd2c51a19a 192.168.1.4:6379
   slots: (0 slots) slave
   replicates afb2a0876b9c4c1c19e2bc492e398765bed0a311
S: 081d87a0d26895605e4c237c5429d3ae6e01f7b2 192.168.1.5:6379
   slots: (0 slots) slave
   replicates 050d71e6ad9bbf0a2a90b743d5a9bb9fb77052bb
S: 00bf8cb3a48a696d9bfc4b124234335633dc14d0 192.168.1.6:6379
   slots: (0 slots) slave
   replicates bec4c3401ced5a43439568f5530d79dd2a911512
M: 050d71e6ad9bbf0a2a90b743d5a9bb9fb77052bb 192.168.1.2:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: bec4c3401ced5a43439568f5530d79dd2a911512 192.168.1.3:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

3. Test the cluster

Login cluster, set the key test, need to use the "-c" parameter to activate the cluster, as follows:

[root@localhost ~]# redis-cli -h 192.168.1.1 -p 6379 -c
192.168.1.1:6379> set k1 1
-> Redirected to slot [12706] located at 192.168.1.3:6379
OK
192.168.1.3:6379> get k1
"1"

Automatic data synchronization on their redis server, view the results:

[root@localhost ~]# redis-cli -h 192.168.1.5 -p 6379 -c
192.168.1.5:6379> get k1
-> Redirected to slot [12706] located at 192.168.1.3:6379
"1"
//提示是从192.168.1.3服务器上同步过来的

Completion of the experiment! ! !

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14157628/2439946