I. Overview
Cluster is supported after redis3.0.
1.1 redis cluster status quo
Currently, redis supports the following cluster features:
1): Automatic Node Discovery
2): slave-> master election, cluster Fault Tolerance
3): Hot resharding: Online sharding
4): Group Management: Cluster xxx
5): Cluster Management Based on configuration (nodes-port.conf)
6): Ask steering/moved steering mechanism.
1.2 redis cluster architecture
1) redis-cluster architecture
Architecture details:
(1) All redis nodes are interconnected (Ping-Pong mechanism), and the binary protocol is used internally to optimize the transmission speed and bandwidth.
(2) fail takes effect only when more than half of nodes in the cluster fail to be detected.
(3) The client is directly connected to the redis node, without the intermediate proxy layer. The client does not need to connect to all nodes in the cluster, just connect to any available node in the cluster.
(4) redis-cluster maps all physical nodes to the [0-16383] slot, and the cluster maintains the node <-> slot <-> Value
2) redis-cluster election: Fault Tolerance
(1) The election process involves the participation of all the master nodes in the cluster. If more than half of the master nodes communicate with the master node (cluster-node-timeout), the current master node is considered to have crashed.
(2) When the entire cluster is unavailable (cluster_state: fail). When the cluster is unavailable, all operations on the cluster are unavailable and receive (error) clusterdown the cluster is down) Error
A: If any master node of the cluster fails and the current master node does not have a Server Load balancer instance. The cluster enters the Fail status, it can also be understood as the Fail status when the slot ing to the cluster [0-16383] is not completed.
B: if more than half of the master nodes in the cluster are down, whether or not the slave cluster is in the Fail status.
Ii. Install redis Cluster
1. Download and Package
123 |
cd /usr/local/ wget http: //download.redis.io/releases/redis-3.2.1.tar.gz tar -zxvf /redis- 3.2 . 1 .tar.gz |
2. Compile and install
cd redis-3.2.1 make && make install
3. Create a redis Node
In the test, we select two servers: 192.168.1.237 and 192.168.1.238. Each server has three nodes.
I will first create three nodes in 192.168.1.237:
CD/usr/local/mkdir redis_cluster // create the cluster directory mkdir 7000 7001 7002 // represents the corresponding port 7000 7001 7002 of the three nodes respectively. // create a 7000 node as an example, copy to the 7000 directory CP/usr/local/redis-3.2.1/redis. conf. /redis_cluster/7000 // copy to the 7001 directory CP/usr/local/redis-3.2.1/redis. conf. /redis_cluster/7001 // copy to the 7002 directory CP/usr/local/redis-3.2.1/redis. conf. /redis_cluster/7002/
Modify the corresponding configurations for the three files in the 7003, and folders respectively.
Daemonize Yes // run pidfile/var/run/redis_7000.pid in the redis background // pidfile corresponding to 7003, 7000, port // port, 7003cluster-enabled Yes // enable the cluster to remove the comment # Cluster-config-file nodes_7000.conf // automatically generate, for the first time when the cluster configuration file is started, 7002cluster-node-timeout 5000 // The request timeout value is set to 5 seconds. It is enough to enable appendonly Yes // aof log if necessary. It records a log for each write operation.
Create three nodes in 192.168.1.238: The corresponding port is changed to 7003,7004, 7005. You can change the corresponding configuration.
4. Start each node on two machines (the two servers are the same)
cd /usr/localredis-server redis_cluster/7000/redis.confredis-server redis_cluster/7001/redis.confredis-server redis_cluster/7002/redis.confredis-server redis_cluster/7003/redis.confredis-server redis_cluster/7004/redis.confredis-server redis_cluster/7005/redis.conf
5. View services
PS-Ef | grep redis # Check whether startup is successful
Netstat-tnlp | grep redis # The redis listening port is displayed.
3. Create a cluster
We have prepared the redis nodes for building the cluster. Next we will connect these nodes to build the cluster. Official offers a tool: redis-trib.rb (/usr/local/redis-3.2.1/src/redis-trib.rb) Look at the suffix to know that this bird cannot be executed directly, it is a program written in Ruby, so we have to install Ruby.
yum -y install ruby ruby-devel rubygems rpm-build
Use the gem command to install the redis interface. Gem is a ruby toolkit.
Gem install redis // wait for a while. Of course, for easy operation, both servers must be installed.
The above steps are complete, then run the redis-trib.rb
/usr/local/redis-3.2.1/src/redis-trib.rb
Usage: redis-Trib <command> <Options> <arguments...>
Reshard HOST: Port
-- To <Arg>
-- Yes
-- Slots <Arg>
-- From <Arg>
Check HOST: Port
Call HOST: PORT command Arg .. ARG
Set-Timeout HOST: Port milliseconds
Add-node new_host: new_port existing_host: existing_port
-- Master-id <Arg>
-- Slave
Del-node HOST: Port node_id
Fix HOST: Port
Import HOST: Port
-- From <Arg>
Help (show this help)
Create host1: port1... hostn: portn
-- Replicas <Arg>
For check, fix, reshard, Del-node, set-Timeout you can specify the host and port of any working node in the cluster.
See this, you should understand, that is, the above operations to complete the establishment of the redis cluster.
Make sure all nodes are started. Then create with the parameter "CREATE" (created in 192.168.1.237)
/usr/local/redis-3.2.1/src/redis-trib.rb create --replicas 1 192.168.1.237:7000 192.168.1.237:7001 192.168.1.237:7003 192.168.1.238:7003 192.168.1.238:7004 192.168.1.238:7005
-- Replicas 1 indicates that each master node is automatically allocated with six nodes on the slave node. The program will generate three master nodes and three slave nodes according to certain rules)
The firewall that has been warned earlier must open the listening port; otherwise, the creation will fail.
Running, can I set the above configuration? (Type 'yes' to accept): Yes // enter Yes
The following prompt is waiting for the cluster to join .......... during the installation, I waited for a long time and waited for a long time. I didn't respond. I was so stupid that I waited for a long time. I read this prompt that the sending cluster meet message to join the cluster.
Now I understand that I didn't need to wait for configuration on a server at the beginning. Here I still need to run to server2.
On 192.168.1.238, redis-cli-C-P 700 * respectively, enter the Client Command windows of each redis node, and then enter cluster meet 192.168.1.238 7000 ......
Return to server1, And the creation is complete.
Check/usr/local/redis/src/redis-trib.rb check 192.168.1.237: 7000
The cluster has been initially set up here.
Iv. Test
1) Get and Set Data
Redis-cli-C-P 7000
Enter the command window and set Hello howareyou directly.
Switch directly to the corresponding slot Node Based on Hash matching.
It should be noted that the redis cluster consists of 16383 slots, which are distributed to multiple nodes through sharding, and read/write occurs on the master node.
2) hypothesis test
Run 192.168.1.238, 192.168.1.238 with one master and two slave servers. Run 192.168.1.238 back to check what happened. All the three nodes of 192.168.1.237 are master nodes, several other server2 instances are missing.
After testing, the cluster can continue to work.
Cause: the redis cluster is fault tolerant by means of election to ensure that one server can run after it fails. This election is because more than half of the master nodes in all the clusters find that other master nodes have crashed, will upgrade other corresponding slave nodes to master.
Q: What if the host is 192.168.1.237? Brother tried, cluster is down !! No way. If more than half of the cluster fails, the whole cluster will not be able to work. If there are three servers, each of which has two masters, remember the corresponding master and slave nodes.
Don't put it on one server. Don't ask me why I use my mind to think about it. It's okay to configure the master and slave nodes for each other. You have to say two crash servers at the same time, haha ......
3) Consistency
I am not so bold in using redis for database persistence, But I just use it for Cache. The official website clearly states that redis cluster is not able to guarantee strong consistency.
V. Problems Encountered during installation
1,
CC adlist. o
/Bin/sh: CC: Command not found
Make [1]: *** [adlist. O] Error 127
Make [1]: Leaving directory/usr/local/redis-3.2.1/src
Make: *** [all] Error 2
Solution: If GCC is not installed or the version is incorrect, install it.
yum install gcc
2,
Zmalloc. h: 50: 31:
Error: jemalloc/jemalloc. h: no such file or directory
Zmalloc. h: 55: 2: Error:
# Error "newer version of jemalloc required"
Make [1]: *** [adlist. O] Error
1
Make [1]: Leaving directory '/data0/src/redis-2.6.2/src
Make: *** [all]
Error 2
Solution: Because the jemalloc memory distributor is not installed, you can install jemalloc or directly
Enter make malloc = libc & make install
More questions
Redis cluster construction problem summary
Redis (16): Cluster Construction