2. Set up the Redis cluster environment; 2. Set up the redis Cluster

Source: Internet
Author: User
Tags install redis

2. Set up the Redis cluster environment; 2. Set up the redis Cluster

Reprinted from source: Http://www.cnblogs.com/hd3013779515/

I. Basic Concepts

1. A redis cluster is a facility for data sharing between multiple nodes. Redis clusters provide the following two benefits:
1.1 automatically split data into multiple nodes
1.2 When a node in the cluster fails, redis can continue to process client requests.

2. A Redis cluster contains 16384 hash slots. Each data in the database belongs to one of the 16384 hash slots. The cluster uses the formula CRC16 (key) % 16384 to calculate the slot of the key. Each node in the cluster is responsible for processing part of the hash slots. In this way, you can easily add or remove nodes to or from the cluster. (Assume that the cluster has three nodes, a B C)

2.1 Add a node: to add a D node to the cluster, first add the node to the cluster, and then move some hash slots in node ABC to node D.
2.2 remove node: to remove node A, you only need to transfer all hash slots in node A to Node B and node C, and then remove the blank node.

3. Master-slave replication in the Cluster
Each node in the cluster has one to N replicas, one of which is the master node, and the other is the slave node. If the master node is offline, the cluster will set a slave node of the master node as the master node to continue working. In this way, the cluster will not work normally because a master node is offline.
Note: If a master node and all its slave nodes are offline, The redis cluster will stop working.

4. redis clusters do not guarantee strong data consistency. Under certain circumstances, redis clusters may lose the write commands that have been executed.

4.1 asynchronous replication is one of the reasons why Redis clusters may lose write commands

4.2 network reasons: if the network is disconnected for too long, the redis cluster will enable the new master node, and the data sent to the master node will be lost.

Ii. redis cluster Construction

To make the cluster work normally, at least three master nodes are required. here we need to create six redis nodes, three of which are master nodes and three are slave nodes, the relationship between the ip address and port of the corresponding redis node is as follows:

192.168.137.174: 6379
192.168.137.174: 6380

192.168.137.174: 6381

192.168.137.174: 6382

192.168.137.174: 6383

192.168.137.174: 6384

1: Install redis

wget http://download.redis.io/releases/redis-3.2.9.tar.gztar -xzvf redis-3.2.9.tar.gz -C /usr/localcd /usr/local/redis-3.2.9make
If an error is reported, see http://www.cnblogs.com/hd3013779515/p/6914374.html

2: Create the directory required by the cluster (simulate 6 instances on one machine)

Mkdir-p/usr/local/redis-cluster

Cd/usr/local/redis-cluster

Mkdir6379

Mkdir6380

Mkdir6381

Mkdir6382

Mkdir6383

Mkdir6384

Note: three master nodes are required for the cluster to operate normally.

3. modify the configuration fileRedis. conf

Cp/usr/local/redis-3.2.9/redis. conf/usr/local/redis-cluster

Vi redis. conf

# Modify the following options in the configuration file

Port 6379

Bind 192.168.137.174

Daemonize yes

Cluster-enabled yes

Cluster-config-file nodes. conf

Cluster-node-timeout 5000

Appendonly yes

# After modifying the configuration items in the redis. conf configuration file, copy the configuration files to 6379 ~ 6384 under the Directory

Cp/usr/local/redis-cluster/redis. conf/usr/local/redis-cluster/6379

# Note: 6379 ~ must be modified after the copy is completed ~ In the redis. conf file under the 6384 directory, change the port parameter to the corresponding folder name.

4: Start6ItemsRedis Instance

Cd/usr/local/redis-cluster/6379/

/Usr/local/redis-3.2.9/src/redis-server redis. conf

Cd/usr/local/redis-cluster/6380/

/Usr/local/redis-3.2.9/src/redis-server redis. conf

Cd/usr/local/redis-cluster/6381/

/Usr/local/redis-3.2.9/src/redis-server redis. conf

Cd/usr/local/redis-cluster/6382/

/Usr/local/redis-3.2.9/src/redis-server redis. conf

Cd/usr/local/redis-cluster/6383/

/Usr/local/redis-3.2.9/src/redis-server redis. conf

Cd/usr/local/redis-cluster/6384/

/Usr/local/redis-3.2.9/src/redis-server redis. conf

Note: you must first enter 6379 ~ 6384 directory, and then start the service. After the service is started, the appendonly. aof, nodes. conf file is automatically generated.

# Run the following command to check the startup status of redis: ps-ef | grep redis

If it is displayed, the startup is successful.

5: ExecutionRedisCreate cluster command to create Cluster

Cd/usr/local/redis-3.2.9/src

./Redis-trib.rb create -- replicas 1 192.168.137.174: 6379 192.168.137.174: 6380 192.168.137.174: 6381 192.168.137.174: 6382 192.168.137.174: 6383 192.168.137.174: 6384

Note: replicas is a parameter that assigns the number of Server Load balancer instances to the master. The parameter we give is 1, and each master has a slave node.

5.1 An error may be reported when you execute the preceding command. This is because the ruby script is executed and the ruby environment is required.

Error message:/usr/bin/env: ruby: No such file or directory

Therefore, you need to install the ruby environment. We recommend that you install it using yum install ruby.

Yum install ruby

5.2 and then execute the create cluster command in step 1. An error may be reported, indicating that the rubygems component is missing and is installed using yum.

Error message:

./Redis-trib.rb: 24: in 'require ': no such file to load -- rubygems (LoadError)

From./redis-trib.rb: 24

Yum install rubygems

5.3 If you execute Step 1 again, an error may be returned, prompting that you cannot load redis because the interfaces of redis and ruby are missing and you can install them using gem.

Error message:

/Usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb: 31: in 'gem _ original_require ': no such file to load -- redis (LoadError)

From/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb: 31: in 'require'

From./redis-trib.rb: 25

Gem install redis

Note: If the company has a proxy, install in gem install -- http-proxy http://proxy.xxx.com: 8080 redis mode, the red part is the company's proxy

5.4 execute the command in step 1 again and run it normally

Enter yes, and the configuration is complete.

Now, the redis cluster is successfully built!

6: Use the redis-cli command to enter the Cluster Environment

/Usr/local/redis-3.2.9/src/redis-cli-c-h 192.168.137.174-p 6379

Note:-c indicates that the cluster mode is in.-p indicates a port number of the cluster.

After logging on to the cluster, perform the verification:

7:View information about all nodes in a cluster

/Usr/local/redis-3.2.9/src/redis-cli-h 192.168.137.174-p 6379 cluster nodes [| grep master]

8:Check a node in the cluster to check the status of the entire cluster.

/Usr/local/redis-3.2.9/src/redis-trib.rb check 192.168.137.174: 6379

9. Cluster Configuration Analysis

By default, data cannot be read from slaves, but after a connection is established, run the READONLY command to read the data.

Otherwise, only the redis-cli-c-h-p command can be used to log on to-c as a cluster.

Cluster-enabled yes cluster switch. The cluster mode is disabled by default.

The name of the cluster-config-file nodes-6379.conf cluster configuration file, each node has a cluster-related configuration file that persistently stores cluster information. This file does not need to be configured manually. This configuration file is generated and updated by Redis. Each Redis cluster node needs a separate configuration file, make sure that the name of the configuration file does not conflict with the name of the system in which the instance runs.

Threshold Value for cluster-node-timeout 15000 node connection timeout. Cluster node timeout in milliseconds. That is, the duration of the disconnection between the node and other nodes in the cluster will be deemed as timeout. It is recommended to be a little larger

Cluster-slave-validity-factor 10 when performing failover, all slave requests to apply for a master, but some slave may be disconnected from the master for a while, resulting in too old data, such slave should not be promoted to master. This parameter is used to determine whether the slave node and master are disconnected for too long. The method to determine is: Compare the slave disconnection time and (node-timeout * slave-validity-factor) + repl-ping-slave-period if the node timeout time is thirty seconds, and the slave-validity-factor is 10. Assume that the default repl-ping-slave-period is 10 seconds, that is, if it exceeds 310 seconds, slave will not try to perform failover.

Cluster-migration-barrier 1 master has a higher number of slave than this value, and the slave can be migrated to other isolated masters. If this parameter is set to 2, then, only when one master node has two slave nodes that can work, one of them will try to migrate.

Cluster-require-full-coverage yes by default, all slots of the cluster are responsible for nodes, and the cluster status is OK to provide services. Set to no to provide services when not all slots are allocated. We do not recommend that you enable this configuration. This will cause the master of the Small Partition to accept write requests for a long time, resulting in data inconsistency.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.