redis cluster cluster construction

Prepare:

   Linux CentOS 6.5(X64)

   redis-3.2.8.tar.gz

1 host: 172.19.59.48

    First install redis-3.2.8 according to the redis single-point steps, and then build the redis-cluster (start 6 redis servers on one machine to simulate the cluster).

 

1. Create a cluster test directory:

2. Modify the configuration file:

Copy the redis.conf of redis-3.2.8, redis.server and redis-cli in the src directory to the 6379 directory, and modify the following parameters:

 port 6379     #bind Host port

 bind 172.19.59.48  #bind Host Ip

 daemonize yes #Run as a daemon

 cluster-enabled yes #Open the cluster mode of the instance

 cluster-config-file nodes.conf #Save the path to the node configuration file

 cluster-node-timeout 5000 #node node timeout (5000 milliseconds)

 appendonly yes

 After the modification is completed, copy the modified redis.conf to several other directories, and modify the port to correspond to the folder. After the operation is complete, under each directory:


 

 3. Start each node in the cluster:

 Enter each directory and execute the startup command: ./redis-server redis.conf


 

 4. Install rubygems components

(The tool for creating redis cluster is written in ruby)

 yum install ruby rubygems –y

 

5. Install gem-redis components 

    Manual download address: https://rubygems.org/gems/redis/versions/3.0.0

 Then copy redis-3.0.0.gem to the /usr directory, and execute the installation command:

    gem install -l redis-3.0.0.gem


 

6. Create a redis cluster: 

#Copy the cluster manager redis-trib.rb

 cp redis-3.0.0/src/redis-trib.rb /usr/local/bin/redis-trib

 cd /usr/local/bin/

 ./redis-trib.rb create --replicas 1 172.19.59.48:6379 172.19.59.48:6380 172.19.59.48:6381 172.19.59.48:6382 172.19.59.48:6383 172.19.59.48:6384

 The meaning of this command is as follows: 

Use the redis-trib.rb program to create a cluster;

 The option --replicas 1 means we want to create 1 slave node for each master node in the cluster;

 The other parameters that follow are a list of addresses for the instance.

  Then redis-trib will print out an expected configuration for you to see. If you think there is no problem, you can enter yes, enter: yes and press Enter to confirm, the cluster will apply the configuration to each node, And connect (join) each node - that is, let each node start communicating with each other.

 If the installation process reports the following error:

 Solution: Uninstall the old ruby ​​and reinstall it.

 How to uninstall ruby:

 #Find out the installation path of ruby

 Find / -name ruby

 #Use the rpm –e <ruby component> command to uninstall ruby

 Be sure to uninstall all ruby ​​before reinstalling ruby:

 Download the ruby ​​installation package, unzip it, and perform the following steps:

 #./configure -prefix=/usr/local/ruby-1.9

 #make

 #make install

Then execute the create redis cluster command again.

So far, the construction of redis cluster has been completed. 

 

The following are some operations of redis cluster:

7. Add a master node to the cluster:

In the original cluster directory, create a new node folder: 6385, and copy the relevant scripts and configuration files to this folder:

 Then start the node: ./redis-server redis.conf


  Add the node to the cluster:

./redis-trib add-node 172.19.59.48:6385 172.19.59.48:6381

(add-node adds a node to the cluster, the first is the ip:port of the new node, and the second is the ip:port of any existing node)


 The newly added node does not contain any data because it does not contain any slots. The newly added point is a master node. When the cluster needs to upgrade a slave node to a new master node, the new node will not be selected. At the same time, the new master node does not participate in elections and failovers because it does not contain any slots.

 Assign slots to new nodes:

#Start

#Reshard operation for the node to be allocated slot

./redis-trib reshard 172.19.59.48:6385

#Select the number of slots to be migrated according to the prompt (ps: choose 500 here)

How many slots do you want to move (from 1 to 16384)? 500  

#Select the node-id to accept these slots  

What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf

#Select slot source:  

#all means to redistribute from all masters,  

#Or the master node id of the slot to extract the data, and finally end with done  

Please enter all the source node IDs.  

Type 'all' to use all the nodes as source nodes for the hash slots.  

Type 'done' once you entered all the source nodes IDs.  

Source node #1:all  

#After printing the moved slot, enter yes to start moving the slot and the corresponding data.  

Do you want to proceed with the proposed reshard plan (yes/no)? yes  

#end

 

8. Add a slave node to the cluster:

The first three steps are the same as adding a master node, and the last step:

Redis-cli connects to the new node, input: cluster replicate corresponds to the node-id of the master

    cluster replicate 7cfd67c3c7645b340c774a11231cd87ddd2a5a85

 

 9. Remove a slave node from the cluster:             

./redis-trib del-node 172.19.59.48:6384 '7cfd67c3c7645b340c774a11231cd87ddd2a5a85'

If there is data in the current node, the deletion will fail:


 A reshard operation must be performed before the node can be deleted.

The results of successful deletion are as follows:

 

10. Delete a master node:

Note: Before deleting the master node, first use reshard to remove all the slots of the master, and then delete the current node.

    #Migrate the slot and data of the node to be deleted (172.19.59.48:6380) to 172.19.59.48:6381  

./redis-trib reshard 172.19.59.48:6381

#Select the number of slots to be migrated according to the prompt (ps: select 4961 here (the number of all slots of the deleted master))  

How many slots do you want to move (from 1 to 16384)? 4961  

#Select the node-id to accept these slots (7cfd67c3c7645b340c774a11231cd87ddd2a5a85 (node-id of 172.19.59.48:6381)) 

What is the receiving node ID? 7cfd67c3c7645b340c774a11231cd87ddd2a5a85

Please enter all the source node IDs.  

    Type 'all' to use all the nodes as source nodes for the hash slots.  

    Type 'done' once you entered all the source nodes IDs.  

  Source node #1:6616b68a61976efb77f785a6fdd5953e4d28c652 (node-id of the deleted master)  

     Source node #2:done  

#After printing the moved slot, enter yes to start moving the slot and the corresponding data.  

Do you want to proceed with the proposed reshard plan (yes/no)? yes  

#end

After performing the above steps, you can delete the node.

./redis-trib del-node 172.19.59.48:6380 '6616b68a61976efb77f785a6fdd5953e4d28c652'

 

11. Test the cluster

Let's use redis-cli to test the redis cluster.

./redis-cli -c -h 172.19.59.48 -p 6379

11.1 Test get and set:

 

11.2 View the current cluster situation: cluster nodes


 

 11.3 Switch to any other node and test get and set again:

Note the set operation:

           If the connection is to the master, the set operation is completed directly;

   If it is connected to a slave, it will be redirected to the master of the slave first, and then the master will perform the set operation: 


  

11.4 Test failover: 

First kill the master process of 6379: the

 cluster detects that the master of 6379 has hung up, and migrates the slot (0-5460) of 6379 to 6382 (that is, the 6382 is upgraded from the original slave to the master, instead of 6379):

  

11.5 Some common commands for clusters:

CLUSTER INFO print cluster information 

CLUSTER NODES Lists all nodes currently known to the cluster, along with information about these nodes. 

CLUSTER MEET <ip> <port> Adds the node specified by ip and port to the cluster, making it a part of the cluster. 

CLUSTER FORGET <node_id> Removes the node specified by node_id from the cluster. 

CLUSTER REPLICATE <node_id> Sets the current node as the slave node of the node specified by node_id. 

CLUSTER SAVECONFIG saves the node configuration file to the hard disk. 

CLUSTER ADDSLOTS <slot> [slot ...] Assigns one or more slots to the current node. 

CLUSTER DELSLOTS <slot> [slot ...] Removes one or more slot assignments from the current node. 

CLUSTER FLUSHSLOTS removes all slots assigned to the current node, making the current node a node with no slots assigned. 

CLUSTER SETSLOT <slot> NODE <node_id> Assign the slot to the node specified by node_id. If the slot is already assigned to another node, let the other node delete the slot> before assigning it. 

CLUSTER SETSLOT <slot> MIGRATING <node_id> Migrate the slot of this node to the node specified by node_id. 

CLUSTER SETSLOT <slot> IMPORTING <node_id> Import the slot from the node specified by node_id to this node. 

CLUSTER SETSLOT <slot> STABLE Cancels the import or migration of a slot. 

CLUSTER KEYSLOT <key> Calculates in which slot the key key should be placed. 

CLUSTER COUNTKEYSINSLOT <slot> Returns the number of key-value pairs currently contained in the slot. 

CLUSTER GETKEYSINSLOT <slot> <count> Returns the keys in count slots.

 

11.6 java api test:

import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class TestRedisCluster {

	private static JedisCluster jedisCluster;

	static {
		// Just give one instance to the cluster
		Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
		jedisClusterNodes.add(new HostAndPort("172.19.59.48", 6379));
		jedisClusterNodes.add(new HostAndPort("172.19.59.48", 6380));
		jedisClusterNodes.add(new HostAndPort("172.19.59.48", 6381));
		jedisClusterNodes.add(new HostAndPort("172.19.59.48", 6382));
		jedisClusterNodes.add(new HostAndPort("172.19.59.48", 6383));
		jedisClusterNodes.add(new HostAndPort("172.19.59.48", 6384));
		jedisCluster = new JedisCluster(jedisClusterNodes);
	}

	public static void main(String[] args) {
		String string = jedisCluster.get("foo");
		System.out.println(string);   
	}

}

 

 

refer to:

           http://www.cnblogs.com/gomysql/p/4395504.html

            http://hot66hot.iteye.com/blog/2050676

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326253827&siteId=291194637