Redis cluster construction and simple use, redis cluster Construction

Source: Internet
Author: User

[Switch] Redis cluster construction and simple use, redis cluster Construction

Introduction to the installation environment and version

Use two virtual machines to simulate six nodes, one machine with three nodes, and create three master nodes and three salve environments.

Redis uses the redis-3.2.4 version.

Both VMS are CentOS, one CentOS6.5 (IP: 192.168.31.245), and one CentOS7 (IP: 192.168.31.210 ).

Installation Process

1. Download and decompress

cd /root/software wget http: //download .redis.io /releases/redis-3 .2.4. tar .gz tar -zxvf redis-3.2.4. tar .gz 

2. Compile and install

cd redis-3.2.4make && make install

3. Copy the redis-trib.rb to the/usr/local/bin directory

cd srccp redis-trib.rb /usr/local/bin/  

4. Create a Redis Node

First, create the redis_cluster directory under the/root/software/redis-3.2.4 directory on the 192.168.31.245 machine;

mkdir redis_cluster  

Under the redis_cluster directory, create a directory named 7000, 7001, and 7002, and copy redis. conf to these three directories.

mkdir 7000 7001 7002<br>cp redis.conf redis_cluster/7000cp redis.conf redis_cluster/7001cp redis.conf redis_cluster/7002  

Modify the three configuration files respectively.

Port 7000 // port 7003, bind local ip // The default ip address is 127.0.0.1, which must be changed to the ip address accessible by other nodes; otherwise, the corresponding port cannot be accessed during cluster creation, unable to create cluster daemonize yes // redis background run pidfile/var/run/redis_7000.pid // pidfile corresponding to 7000,7001, 7002cluster-enabled yes // enable the cluster to remove the comment # cluster-config-file nodes_7000.conf // automatically generate 15000, 7002cluster-node-timeout at the first startup of the cluster configuration file, you can set the appendonly yes // aof log to be enabled as needed. It records a log every write operation.
  • Then, on another machine (192.168.31.210), the Operation repeats the preceding three steps, but changes the directory to 7003, 7004, and 7005. The corresponding configuration file can also be modified according to this rule.

5. Start each node

Run redis-server redis_cluster/7000/redis on the first machine. confredis-server redis_cluster/7001/redis. confredis-server redis_cluster/7002/redis. run redis-server redis_cluster/7003/redis on another machine in conf. confredis-server redis_cluster/7004/redis. confredis-server redis_cluster/7005/redis. conf

6. Check redis startup status

# One machine <br> ps-ef | grep redisroot 61020 1 0? 00:00:01 redis-server 127.0.0.1: 7000 [cluster] root 61024 1 0? 00:00:01 redis-server 127.0.0.1: 7001 [cluster] root 61029 1 0? 00:00:01 redis-server 127.0.0.1: 7002 [cluster] netstat-tnlp | grep redistcp 0 0 127.0.0.1: 17000 0.0.0.0: * LISTEN 61020/redis-server tcp 0 0 127.0.0.1: 17001 0.0.0.0: * LISTEN 61024/redis-server tcp 0 0 127.0.0.1: 17002 0.0.0.0: * LISTEN 61029/redis-server tcp 0 0 127.0.0.1: 7000 0.0.0.0: * LISTEN 61020/redis-server tcp 0 0 127.0.0.1: 7001 0.0.0.0: * LISTEN 61024/redis-server tcp 0 127.0.0.1: 7002 0.0.0. 0: * LISTEN 61029/redis-server # ps-ef | grep redisroot 9957 1 0? 00:00:01 redis-server 127.0.0.1: 7003 [cluster] root 9964 1 0? 00:00:01 redis-server 127.0.0.1: 7004 [cluster] root 9971 1 0? 00:00:01 redis-server 127.0.0.1: 7005 [cluster] root 10065 4744 0 00:00:00 pts/0 grep -- color = auto redisnetstat-tlnp | grep redistcp 0 0 127.0.0.1: 17003 0.0.0.0: * LISTEN 9957/redis-server 1tcp 0 0 127.0.0.1: 17004 0.0.0.0: * LISTEN 9964/redis-server 1tcp 0 0 127.0.0.1: 17005 0.0.0.0: * LISTEN 9971/redis-server 1tcp 0 0 127.0.0.1: 7003 0.0.0.0: * LISTEN 9957/redis-server 1tcp 0 0 127.0.0.1: 7004 0.0.0.0: * LISTEN 9964/redis-server 1tcp 0 0 127.0.0.1: 7005 0.0.0.0: * LISTEN 9971/redis-server 1

7. Create a cluster

Redis officially provides the redis-trib.rb tool, just in the src directory of the extract directory, step 3 has copied it to the/usr/local/bin directory, it can be used directly in the command line. Run the following command to complete the installation.

redis-trib.rb  create  --replicas  1  192.168.31.245:7000 192.168.31.245:7001  192.168.31.245:7002 192.168.31.210:7003  192.168.31.210:7004  192.168.31.210:7005

Among them, the first three ip: port is the node of the first machine, and the remaining three are the second machine.

And so on. This tool is implemented in ruby, so you need to install ruby. The installation command is as follows:

yum -y install ruby ruby-devel rubygems rpm-build

gem install redis

Then run the redis-trib.rb command, the following prompt will appear:

Enter yes, and the following message is displayed, indicating that the installation is successful.

  

8. Cluster Verification

Connect to the cluster's port 7002 node on the first machine and connect to the 7005 node on the other machine in the form of redis-cli-h 192.168.31.245-c-p 7002, add the parameter-C to connect to the cluster, because the above redis. conf changes bind to an IP address, so the-h Parameter cannot be omitted.

Run the set hello world command on node 7005. The execution result is as follows:

 

Then, on the other port 7002, check the content with the key as hello and get hello. The execution result is as follows:

This indicates that the cluster is operating normally.

Briefly describe the principle

During the design of redis cluster, the de-centralization and de-middleware are taken into account. That is to say, every node in the cluster is equal and equal, each node stores its own data and the status of the entire cluster. Each node is connected to all other nodes, and these connections are active. This ensures that we only need to connect to any node in the cluster to obtain data from other nodes.

The Redis cluster does not use the traditional consistent hash to distribute data, but uses anotherHash slot). Redis cluster allocates 16384 slots by default. When we set a keyCRC16Algorithm to obtainslotAnd then the key is assigned to the node in the hash slot range. The specific algorithm is:CRC16 (key) % 16384. So when we saw the set and get during the test, we directly jumped to the 7000 port node.

The Redis cluster will store the data on a master node, and then synchronize the data between the master and its corresponding salve. When reading data, you can also obtain data from the corresponding master node based on the consistent hash algorithm. A corresponding salve node is started to act as the master only when a master node fails.

Note that:3 or moreOtherwise, the cluster fails to be created. If the number of active master nodes is less than half of the total number of nodes, the whole cluster cannot provide services.

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.