A tutorial on Redis cluster construction and the handling of problems encountered

Source: Internet
Author: User
Tags node redis redis cluster install redis
This article is mainly to tell you how to build a Redis cluster and the process needs to pay attention to the problem, very good, recommend to everyone, there is a need for small partners to refer to the next

Here, a 6-node Redis pseudo-cluster is built on a Linux virtual machine, and the idea is simple to open 6 Redis instances on a single virtual machine and each Redis instance has its own port. That would be equivalent to simulating 6 machines, and then building a Redis cluster with these 6 instances.

Prerequisite: Redis has been installed, directory for/usr/local/redis-4.0.1 if not, you can refer to the article Windows under the installation of Redis Linux under the installation of Redis

The Redis cluster is a ruby script, so the ruby environment is required to execute the script. The REDIS-TRIB.RB,REDIS-TRIB.RB, which corresponds to Redis's source src directory, is the official Redis-managed Redis cluster tool, which is packaged as a simple, convenient and practical tool based on the cluster commands provided by Redis. So

To install the Ruby environment:

1.yum install ruby


2.yum install rubygems


3.gem install redis


The CentOS default support for Ruby to 2.0.0,redis requires a minimum of 2.2.2. The workaround is to first install RVM and then upgrade the Ruby version to 2.3.3

1.sudo yum install curl

2. Installing RVM

    curl -L get.rvm.io | bash -s stable


   source /usr/local/rvm/scripts/rvm

4. View a known version of Ruby in the RVM library

    rvm list known

5. Install a ruby version

    rvm install 2.3.3

6. Use a ruby version

    rvm use 2.3.3

7. Uninstalling a known version

   rvm remove 2.0.0 

8. View version

   ruby --version

9. Re-install Redis

   gem install redis

Redis Cluster Setup

Create the Redis-cluster directory, create the redis-8001,redis-8002,redis-8003 node directory, and then copy the redis-conf to the node directory


Modify the node redis-conf file separately, because on a machine (192.16819.129), so each instance should have a different port, at the same time, each instance will obviously have its own place to store data, turn on the AOF mode, turn on the cluster configuration, turn on the background mode;


Turn on the Redis service to see if it can be started. OK, no problem.


Create startall.sh script (prompt permission denied description insufficient permissions, execute command chmod 777 startall.sh Modify permissions)


Start the startall.sh script


Create a stopall.sh script



Create a cluster

Next, we'll create the cluster through the Ruby script.


You can see that REDIS-TRIB.RB has the following features:

    1. Create: Creating a Cluster

    2. Check: Checking the cluster

    3. Info: View cluster information

    4. FIX: Fix cluster

    5. Reshard: Online Migration Slots

    6. Rebalance: Balancing the number of cluster node slots

    7. Add-node: Adding a new node to the cluster

    8. Del-node: Removing nodes from the cluster

    9. Set-timeout: Setting the time-out for heartbeat connections between cluster nodes

    10. Call: Execute commands on all nodes of the cluster

    11. Import: Importing external Redis data into a cluster

There are two main classes of REDIS-TRIB.RB: ClusterNode and RedisTrib . The ClusterNode information of each node is saved, RedisTrib it is the implementation of each REDIS-TRIB.RB function


Note: Tips for a minimum of 3 master cluster nodes, previously said to be created 6, but the actual operation I only created 3 nodes, so we can conclude that we create a Redis cluster is a minimum of three master nodes, and should be an odd number, so, do not lazy, and then create three.

Special note: The key here is the optional replicas parameter,--replicas 2 meaning to allocate 2 each Slave,replicas for each master represents the need for several slave. Do not fill in this parameter can be created successfully, this is three master. About the replicas parameter, let me introduce you later.



First,--replicas 1 1 actually represents a ratio, which is the ratio of the number of primary nodes/nodes. So think about what nodes are the primary nodes when creating a cluster? Which nodes are from the node? The answer is to follow the order of Ip:port in the command, first 3 primary nodes, then 3 slave nodes.

Second, notice the concept of the slot in the diagram. Slot for a redis cluster, it is a place where data is stored, a slot. For each master, there is a range of slots, and slave does not. In a Redis cluster, master can read, write, and slave read-only. The writing of the data is actually distributed in the slot, which is the previous 1. The master-slave mode of x is different (Master/slave data storage is identical in master-slave mode) because the data storage of 3 master in a Redis cluster is not the same. This will be verified in the following essay.

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.