This article introduces the implementation of the Redis cluster and the use of PHP to call, to share with you, the need for friends can refer to
First, to build a Redis cluster
1. Conceptual interpretation
After Redis 3.0, the version supports the Redis-cluster cluster, redis-cluster with a non-central structure, each node holds the data and the entire cluster state, and each node is connected to all other nodes. The composition of its redis-cluster frame is as follows:
Its structural characteristics:
1. All Redis nodes are interconnected (ping-pong mechanism), using binary protocols to optimize transmission speed and bandwidth internally. 2. The fail of a node is effective only if it is detected by more than half of the nodes in the cluster. 3, the client and the Redis node direct connection, do not need 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. 4, Redis-cluster all the physical nodes mapped to [0-16383]slot (not necessarily the average distribution), cluster responsible for maintaining node<->slot<->value. 5, Redis cluster good 16,384 buckets, when you need to place a key-value in the Redis cluster, according to the value of CRC16 (key) mod 16384, decide which bucket to put a key.
Now we are three main nodes: A, B, C three nodes, which can be three ports on a single machine or three different servers. Then, using the hash slot (hash slot) to allocate 16,384 slots, their three nodes respectively assume the slot interval is: Node A covers 0-5460; Node B covers 5461-10922; Node C covers 10923-16383.
Can't read,,,,
Get data: If a value is stored, follow the algorithm of Redis cluster hash slot: CRC16 (' key ') 384 = 6782. Then the storage for this key will be allocated to B. Similarly, when I connect (a,b,c) any node want to get ' key ' this key, it will also be such an algorithm, and then jump inside the B node to get the data
2. Redis cluster master-slave mode
Redis cluster in order to ensure the high availability of data, a master-slave mode, a primary node corresponding to one or more slave nodes, the master node to provide data access, from the node is to pull the data from the main node backup, when the main node hangs, there will be a slave node selected to act as the master node, This ensures that the cluster will not be hung out. And if the previous old master node is back to normal.
In the above example, the cluster has the ABC three master node, if the 3 nodes are not joined from the node, if B is dead, we will not be able to access the entire cluster. Slots A and C are also inaccessible. So when we set up the cluster, we must add the slave nodes for each master node, such as this, the cluster contains the primary Node A, B, C, and from the node A1, B1, C1, so even if B hangs the system can continue to work correctly. The B1 node replaces the b node, so the Redis cluster will select the B1 node as the new primary node, and the cluster will continue to provide the service correctly. When B is re-opened, it becomes B1 from the node. However, it is important to note that if Node B and B1 are hung at the same time, the Redis cluster cannot continue to provide the service correctly.
3.redis Cluster Build-up
The cluster should have at least an odd number of nodes, so there are at least three nodes, each node has at least one backup node, so the following uses 6 nodes (master node, backup node determined by the Redis-cluster cluster).
Well, all you need is a minimum of 6 Redis services.
Then start these 6 Redis services on this machine, but remember to make a configuration change before starting.
Port 7000 //Ports 7000,7002,7003 bind 10.93.84.53 //default IP for 127.0.0.1 need to change to other node machine accessible IP Otherwise, the corresponding port cannot be accessed when the cluster is created. Unable to create cluster daemonize Yes //redis background run pidfile ./redis_7000.pid //pidfile file corresponds to 7000, 7001,7002cluster-enabled Yes //Turn on cluster Note # remove Cluster-config-file nodes.conf //cluster configuration Configuration file first start auto generate 7000,7001,7002cluster-node-timeout 15000 //Request time -out default 15 seconds, you can set your own appendonly Yes //aof Log on if necessary, it will log each write operation
Then copy the redis-trib.rb from the SRC directory in the Redis source to the current path.
Installing the ruby Environment
Yum install ruby yum install rubygems gem install Redis-3.2.2.gem
Check the boot status of the native 6 Redis
Ps-ef | grep Redis
Finally we use REDIS-TRIB.RB to create a redis cluster. Use the Create command –replicas 1 parameter to create a slave node for each master node, a pair of two, and the first one for the second from.
./redis-trib.rb Create--replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
To test with REDIS-CLI, add the-c parameter to connect as a cluster.
Redis-cli-c
To enter the plus-c parameter, the connection is the cluster mode.
At this point, the Redis cluster is successfully built.
But I just want to test the 6 port, the real words are at least two machines. How do we do security?
1. Modify redis.conf To change the BIND option to the current LAN IP
2. Configure the firewall to allow access only to another Redis server, and of course, to allow access to the IP of the application server ... Otherwise the program is not connected to the Redis service, play hair ah ....
Turn off 6379 (the port used by the Redis service) iptables-i input-p TCP--dport 6379 -j DROP//Allow 192.168.1.0 this machine to access the native 6379 port Iptables-i Input-s 192.168.1.0-p TCP--dport 6379-j ACCEPT
Second, connect to the Redis cluster in PHP
First Use Php–ri Redis to view the Redis extended version. To be on top of version 3.0.
Reference
The second parameter, as long as one node in the cluster is on the line, does not need to be fully populated
$obj _cluster = new Rediscluster (NULL, [' 127.0.0.1:6380 ']); Echo $obj _cluster->get (' name1 ');
Get