Many of the company's projects are using Redis master-slave. Due to the various destructive operations of coder, an architecture with fail-back is urgently needed. So the new version of cluster, began the test.
First, Cluster Theory Foundation Cluster Introduction
A Redis cluster is an assembly that provides the sharing of data among multiple Redis nodes.
Redis clusters do not support commands to handle multiple keys, because this requires moving data between different nodes, which does not achieve performance like Redis, which can cause unpredictable errors in high load situations.
A Redis cluster provides a degree of availability through partitioning, in a real-world environment where a node continues to process commands if it is down or unreachable.
Advantages of the Redis cluster:
- Automatically split the data to different nodes.
- A partial node of the entire cluster can continue processing the command if it fails or is unreachable.
Redis Conformance Assurance
Redis does not guarantee strong data consistency. This means that in practice, the cluster may lose write operations under certain conditions.
The first reason is because the cluster is using asynchronous replication. Write operation Process:
The client writes a command to master Node B.
Master Node B replies to the client's command state.
The master node copies the write operations to him from the nodes B1, B2 and B3.
The primary node's copy of the command occurs after returning a command reply, because if the command request is processed every time it needs to wait for the copy operation to complete, the master node can handle the command request much less quickly-we have to trade off between performance and consistency.
Redis Cluster Another scenario where you might lose a command is if the cluster has a network partition, and a client is orphaned with a few instances, including at least one master node.
For example, assume that the cluster contains a, B, C, A1, B1, C1 six nodes, where a, B, C is the primary node, A1, B1, C1 from the A,b,c node, and a client Z1
Assuming a network partition occurs in a cluster, the cluster may be divided into two parties, most of which contain nodes A, C, A1, B1, and C1, and a small portion of which contains node B and client Z1.
Z1 still be able to write to the master Node B, if the network partition takes a short time, then the cluster will continue to function normally, if the partition time is enough for most of the party to elect B1 as the new master, then Z1 write B in the data is lost.
Cluster Architecture
1. Redis-cluster Frame composition
Architectural Details:
- All Redis nodes are interconnected (the ping-pong mechanism), using binary protocols internally to optimize transmission speed and bandwidth.
- The fail of a node takes effect only when the detection of more than half of the nodes in the cluster fails.
- The client is directly connected to the Redis node and does not require an intermediate proxy layer. The client does not need to connect to all nodes in the cluster to connect to any of the available nodes in the cluster.
- Redis-cluster maps all the physical nodes to the [0-16383]slot, cluster is responsible for maintaining node<->slot<->value.
2. Redis-cluster election: Fault tolerance
- Lead the election process is all master in the cluster, if more than half of the master node communicates with the master node (cluster-node-timeout), the current master node is considered to be dead.
- When the entire cluster is unavailable (Cluster_state:fail), when the cluster is unavailable, all operations on the cluster are unavailable, and the error is received ((error) Clusterdown the cluster is down).
- If the cluster any master hangs, and the current master does not have slave. The cluster enters the fail state, it can also be understood that the slot mapping into the group [0-16383] does not complete when entering the fail state.
- If more than half of the master hangs, regardless of whether there is a slave cluster into the fail state.
Installing Cluster
Required Software:
Redis-3.0.6.tar.gz
Tcl8.6.1-src.tar.gz
Rubygems-2.4.2.tgz
Redis-3.0.0.gem
Note, due to the official website, recommended 6 servers, my notebook on the start of three VMs do.
192.168.11.12:6379/6380
192.168.11.13:6379/6380
192.168.11.14:6379/6380
1. Install Redis (show only one)
Mkdir-pv/usr/local/redis6379/{etc,log,var,data}
CD redis-3.0.6
Make
Make prefix=/usr/local/redis6379 Install
2. configuration file (test only, minimum option):
Cat/usr/local/redis6379/etc/redis6379.conf
Daemonize Yes
Port 6379
AppendOnly Yes
Appendfilename "Appendonly-6379.aof"
cluster-enabled Yes
Cluster-config-file/opt/nodes-6379.conf
Cluster-node-timeout 5000
cluster-enabled option for open instance cluster mode
Cluster-config-file the path to save the node profile, the default value is nodes.conf. The node profile is not human-modified, it is created by the Redis cluster at startup, and is automatically updated when necessary.
3. Start Redis
/usr/local/redis6379/bin/redis-server/usr/local/redis6379/etc/redis6379.conf
# ps aux | grep redis root 14968 0.1 0.9 137444 9616 ? Ssl 20:23 0:11 /usr/local/redis6379/bin/redis-server *:6379 [cluster] root 15002 0.1 0.7 137444 7520 ? Ssl 20:23 0:11 /usr/local/redis6380/bin/redis-server *:6380 [cluster]
4. Start the cluster
[[email protected] 10.19.166.212/usr/local/src/redis-3.0.6/src]
#/usr/local/src/redis-3.0.6/src/redis-trib.rb Create-replicas 1 192.168.11.12:6379 192.168.11.12:6380 192.168.11.13:6379 192.168.11.13:6380 192.168.11.14:6379 192.168.11.14:6380
/usr/bin/env:ruby: No file or directory
No Ruby found in error
Because we haven't installed it yet.
5. Install Ruby and Ruby dependencies
Yum-y Install ruby Ruby-rdoc
Note that there is also a lack of dependency, directly start building the cluster will also error:
#/usr/local/src/redis-3.0.6/src/redis-trib.rb Create-replicas 1 192.168.11.12:6379 192.168.11.12:6380 192.168.11.13:6379 192.168.11.13:6380 192.168.11.14:6379 192.168.11.14:6380
/usr/local/src/redis-3.0.6/src/redis-trib.rb:24:in ' require ': No such file to Load-rubygems (Loaderror)
From/usr/local/src/redis-3.0.6/src/redis-trib.rb:24
Continue installing Rubygem
Tar zxmf rubygems-2.4.2.tgz
Ruby Setup.rb
CP bin/gem/usr/local/bin/
Gem Install-l Redis-3.0.0.gem
6. Building a cluster
/usr/local/src/redis-3.0.6/src/redis-trib.rb create- Replicas 1 192.168.11.12:6379 192.168.11.12:6380 192.168.11.13:6379 192.168.11.13:6380 192.168.11.14:6379 192.168.11.14:6380
>>> Creating cluster>>> performing hash slots allocation on 6 nodes ... Using 3 masters:192.168.11.14:6379192.168.11.13:6379192.168.11.12:6379adding Replica 192.168.11.13:6380 to 192.168.11.14:6379adding replica 192.168.11.14:6380 to 192.168.11.13:6379adding replica 192.168.11.12:6380 to 192.168.11.12:6379m:c776fbe75505f6cc5c452cea363626804d675433 192.168.11.12:6379 slots:10923-16383 (5461 slots) MASTERS:4DEC205BD3C73333F3976E202FE4282C5B72286A 192.168.11.12:6380 replicates C776fbe75505f6cc5c452cea363626804d675433m:945d62135ac298c06e56ea5d3da0bdf4eda86eb0 192.168.11.13:6379 Slots : 5461-10922 (5462 slots) Masters:00b47d4d1b3b26a276a96975fe33063225b87fc9 192.168.11.13:6380 replicates Ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8m:ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8 192.168.11.14:6379 slots:0- 5460 (5461 slots) Masters:a8334f26be6b35a3e75f037fa5f44779cf970b12 192.168.11.14:6380 replicates 945d62135ac298c06e56ea5d3da0bdf4eda86eb0can I Set the above Configuration? (Type ' yes ' to accept): yes>>> Nodes configuration updated>>> Assign a different config epoch to each no De>>> sending CLUSTER MEET messages to join the clusterwaiting for the CLUSTER to Join...>>> performing Cluster Check (using node 192.168.11.12:6379) m:c776fbe75505f6cc5c452cea363626804d675433 192.168.11.12:6379 Slots : 10923-16383 (5461 slots) masterm:4dec205bd3c73333f3976e202fe4282c5b72286a 192.168.11.12:6380 slots: (0 slots) Master Replicates c776fbe75505f6cc5c452cea363626804d675433m:945d62135ac298c06e56ea5d3da0bdf4eda86eb0 192.168.11.13:6379 slots:5461-10922 (5462 slots) masterm:00b47d4d1b3b26a276a96975fe33063225b87fc9 192.168.11.13:6380 slots: (0 slots) Mas TER replicates ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8m:ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8 192.168.11.14:6379 slots:0-5460 (5461 slots) masterm:a8334f26be6b35a3e75f037fa5f44779cf970b12 192.168.11.14:6380 SLO TS: (0 Slots) Master replicates 945d62135ac298c06e56ea5d3da0bdf4eda86eb0[ok] All nodes agree on slots configuration.>>> Check for open slots...>>> Chec K-Slots coverage ... [OK] All 16384 slots covered.
Okay, the installation is complete.
Note:
REDIS-TRIB.RB This command is used here to create a new cluster, the option-replicas 1 means that we want to create a slave node for each master node in the cluster.
The other parameters followed are the address list of the cluster instance, with 3 Master3 of slave.
Here you can:/usr/local/src/redis-3.0.6/src/redis-trib.rb Create-replicas 0 192.168.11.12:6379 192.168.11.13:6379 192.168.11.14:6379
Add a node after redis-trib.rb Add-node 192.168.11.12:6380 192.168.11.13:6380 after not doing slave
Here's a look at the node:
/USR/LOCAL/SRC/REDIS-3.0.6/SRC/REDIS-TRIB.RB Check 192.168.11.12:6379
>>> performing Cluster Check (using node 192.168.11.12:6379) m:c776fbe75505f6cc5c452cea363626804d675433 192.168.11.12:6379 slots:10923-16383 (5461 Slots) Master 1 additional replica (s) s:a8334f26be6b35a3e75f037fa5f44779cf9 70B12 192.168.11.14:6380 Slots: (0 slots) Slave replicates 945d62135ac298c06e56ea5d3da0bdf4eda86eb0m:945d62135ac298c0 6e56ea5d3da0bdf4eda86eb0 192.168.11.13:6379 slots:5461-10922 (5462 Slots) Master 1 additional replica (s) s:00b47d4d1b3 B26A276A96975FE33063225B87FC9 192.168.11.13:6380 Slots: (0 slots) Slave replicates ED3F0377B8C7CDACE570FCDC8EB60B2FCE 61bba8m:ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8 192.168.11.14:6379 slots:0-5460 (5461 Slots) Master 1 additional rep Lica (s) s:4dec205bd3c73333f3976e202fe4282c5b72286a 192.168.11.12:6380 slots: (0 slots) Slave replicates C776fbe75505f6 Cc5c452cea363626804d675433[ok] All nodes agree on slots configuration.>>> Check for open slots...>> > Check Slots Coverage ... [OK] All 16384 slots covered.
7. Test Write
/usr/local/redis6379/bin/redis-cli-c-H 192.168.11.12-p 6379
192.168.11.12:6379> Set AA 123
-Redirected to Slots [1180] located at 192.168.11.14:6379
Ok
192.168.11.14:6379>
Test Delete:
[[email protected] 10.19.166.212/usr/local/src]
/usr/local/redis6379/bin/redis-cli-c-H 192.168.11.12-p 6379 shutdown
[[email protected] 10.19.166.212/usr/local/src]/usr/local/src/redis-3.0.6/src/redis-trib.rb check 192.168.11.13:6379>>> performing Cluster Check (using node 192.168.11.13:6379) M: 945d62135ac298c06e56ea5d3da0bdf4eda86eb0 192.168.11.13:6379 slots:5461-10922 (5462 Slots) Master 1 additional replica ( s) m:ed3f0377b8c7cdace570fcdc8eb60b2fce61bba8 192.168.11.14:6379 slots:0-5460 (5461 Slots) Master 1 additional replica (s) m:4dec205bd3c73333f3976e202fe4282c5b72286a 192.168.11.12:6380 slots:10923-16383 (5461 Slots) Master 0 additional Repl ICA (s) s:00b47d4d1b3b26a276a96975fe33063225b87fc9 192.168.11.13:6380 slots: (0 slots) Slave replicates ED3F0377B8C7CDA CE570FCDC8EB60B2FCE61BBA8S:A8334F26BE6B35A3E75F037FA5F44779CF970B12 192.168.11.14:6380 Slots: (0 slots) Slave replic Ates 945d62135ac298c06e56ea5d3da0bdf4eda86eb0[ok] All nodes agree on slots configuration.>>> Check for open s Lots...>>> Check Slots Coverage ... [OK] All 16384 Slots CovereD./usr/local/redis6379/bin/redis-cli-c-H 192.168.11.12-p 6380 shutdown/usr/local/redis6379/bin/redis-cli-c-H 192.168.11.14-p 6379 192.168.11.14:6379> get AA (Error) Clusterdown The cluster is down192.168.11.14:6379> get BB (er ROR) Clusterdown The cluster is down192.168.11.14:6379> quit
/usr/local/redis6379/bin/redis-cli-c-H 192.168.11.14-p 6379
192.168.11.14:6379> get AA
"123"
192.168.11.14:6379> Get BB
-Redirected to Slots [8620] located at 192.168.11.13:6379
"234"
Install to this end
?
Redis Cluster Installation Guide