CentOS Perfect build Redis3.0 cluster with test

Source: Internet
Author: User
Tags download redis redis version redis cluster install redis

Unified chat and push projects on the line are using Redis master-slave, Redis version 2.8.6

Redis Master-Slave is similar to MySQL master-slave, but the Redis master-slave configuration is simple, mainly specifying the primary node IP and port from the node configuration file: Slaveof 192.168.1.197 6379, then start the master-slave, Master-Slave builds the Redis master. If the primary node fails, it does not automatically switch and requires the use of a Redis sentinel or keepalive to implement a master failover

The Redis cluster is a decentralized, distributed Redis storage architecture that can share data across multiple nodes to address Redis's high availability, scalability, and more, and the Redis cluster offers the following two benefits
1. Automatic data splitting (split) to multiple nodes
2. Redis can also continue processing client requests when one of the nodes in the cluster fails.
A Redis cluster contains 16,384 hash slots (hash slots), and each data in the database belongs to one of these 16,384 hash slots. The cluster uses the formula CRC16 (key)% 16384来 to calculate which slot the key belongs to. Each node in the cluster is responsible for processing a portion of a hash slot.
Master-slave replication in a cluster
Each node in the cluster has 1 to n replicas, one of which is the primary node, and the rest is the slave node, and if the master node goes offline, the cluster will set up a slave node of the master node as the new master node and continue to work. This way, the cluster will not work properly because of a primary node's downline


Start building Redis clusters below

Since the smallest Redis cluster requires 3 master nodes, one machine can run multiple Redis instances, I build using two machines, 6 Redis instances, three primary nodes, three slave nodes as backup
Online many use a single server to open 6 ports, operation is similar, just configure the basic relatively simple point, more than one server closer to the production environment

IP and port correspondence between Redis 6 nodes
Server1:
192.168.1.198:7000
192.168.1.198:7001
192.168.1.198:7002
Server2:
192.168.1.199:7003
192.168.1.199:7004
192.168.1.199:7005

1, install the required dependency package


[email protected] ~]# Yum install gcc gcc-c++ kernel-devel automake autoconf libtool make wget tcl vim Ruby RubyGems Unzi P Git-y

2. Two machines download Redis and install separately


[Email protected] src]# cd/usr/local/
[Email protected] local]# wget http://download.redis.io/releases/redis-3.0.6.tar.gz
[Email protected] local]# tar xzf redis-3.0.6.tar.gz
[Email protected] local]# CD redis-3.0.6
[[email protected] redis-3.0.6]# make


3. Create the directory required by the cluster

Server1 execution:

Mkdir-p/usr/local/cluster
Cd/usr/local/cluster
mkdir 7000
mkdir 7001
mkdir 7002server2 Execution:

Mkdir-p/usr/local/cluster
Cd/usr/local/cluster
mkdir 7003
mkdir 7004
mkdir 7005
4. Modify the configuration file redis.conf
Cp/usr/local/redis-3.0.6/redis.conf/usr/local/cluster
Cd/usr/local/cluster
VI redis.conf

# #注意每个实例的端口号不同
Port 7000
Daemonize Yes
cluster-enabled Yes
Cluster-config-file nodes.conf
Cluster-node-timeout 5000
AppendOnly yes# #修改完redis. conf These configuration entries in the configuration file, copy the configuration file to the 7000/7001/7002/7003/7004/7005 node directory separately
Server1 execution:

cp/usr/local/cluster/redis.conf/usr/local/cluster/7000
cp/usr/local/cluster/redis.conf/usr/local/cluster/7001
Cp/usr/local/cluster/redis.conf/usr/local/cluster/7002server2 Execution:

cp/usr/local/cluster/redis.conf/usr/local/cluster/7003
cp/usr/local/cluster/redis.conf/usr/local/cluster/7004
cp/usr/local/cluster/redis.conf/usr/local/cluster/7005# #注意: To modify the 7001/7002/7003/7004/after the copy is complete 7005 directory The port parameter in the redis.conf file below, change the name of the corresponding folder respectively

5. Start each of these 6 Redis instances and see if they are successful: Ps-ef|grep Redis
Server1 execution:

[Email protected] cluster]# cd/usr/local/cluster/7000
[Email protected] 7000]# Redis-server redis.conf
[Email protected] 7000]# cd/usr/local/cluster/7001
[Email protected] 7001]# Redis-server redis.conf
[Email protected] 7001]# cd/usr/local/cluster/7002
[Email protected] 7002]# Redis-server redis.conf
[Email protected] 7002]# Ps-ef|grep Redis
Root 2741 1 0 09:39? 00:00:00 redis-server *:7000 [cluster]
Root 2747 1 0 09:40? 00:00:00 redis-server *:7001 [cluster]
Root 2751 1 0 09:40? 00:00:00 redis-server *:7002 [cluster]
Root 2755 2687 0 09:40 pts/0 00:00:00 grep Redisserver2 performed:
[Email protected] cluster]# cd/usr/local/cluster/7003
[Email protected] 7003]# Redis-server redis.conf
[Email protected] 7003]# cd/usr/local/cluster/7004
[Email protected] 7004]# Redis-server redis.conf
[Email protected] 7004]# cd/usr/local/cluster/7005
[Email protected] 7005]# Redis-server redis.conf
[Email protected] 7005]# Ps-ef|grep Redis
Root 1619 1 0 09:40? 00:00:00 redis-server *:7003 [cluster]
Root 1623 1 0 09:40? 00:00:00 redis-server *:7004 [cluster]
Root 1627 1 0 09:41? 00:00:00 redis-server *:7005 [cluster]
Root 1631 1563 0 09:41 pts/0 00:00:00 grep redis


6. Execute redis Create cluster command to create cluster (note IP address and port number)

[Email protected] cluster]# CD/USR/LOCAL/REDIS-3.0.6/SRC
[Email protected] src]#/redis-trib.rb create--replicas 1 192.168.1.198:7000 192.168.1.198:7001 192.168.1.198:7002 19 2.168.1.199:7003 192.168.1.199:7004 192.168.1.199:70056.1 to this step because the previous 1th step has a dependency package, not prompting for Ruby and RubyGems errors, but will still error, prompted not to load Redis is due to the lack of a redis and Ruby interface and the use of gem installation
Error content:
/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
FIX: Gem install Redis
6.2 Perform the 6th step command again, execute normally, prompt to allow the configuration file to be modified, enter Yes, and complete the cluster configuration!
[Email protected] src]#/redis-trib.rb create--replicas 1 192.168.1.198:7000 192.168.1.198:7001 192.168.1.198:7002 19 2.168.1.199:7003 192.168.1.199:7004 192.168.1.199:7005
>>> Creating Cluster
>>> performing hash slots allocation on 6 nodes ...
Using 3 Masters:
192.168.1.199:7003
192.168.1.198:7000
192.168.1.199:7004
Adding replica 192.168.1.198:7001 to 192.168.1.199:7003
Adding replica 192.168.1.199:7005 to 192.168.1.198:7000
Adding replica 192.168.1.198:7002 to 192.168.1.199:7004
M:2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
slots:5461-10922 (5462 slots) Master
s:e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
Replicates 02f1958bd5032caca2fd47a56362c8d562d7e621
S:26101DB06B5C2D4431CA8308CF43D51F6939B4FC 192.168.1.198:7002
Replicates 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299
m:02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
slots:0-5460 (5461 slots) Master
m:6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
slots:10923-16383 (5461 slots) Master
s:ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
Replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
Can I Set the above configuration? (Type ' yes ' to accept): Yes
>>> Nodes Configuration Updated
>>> Assign a different config epoch to each node
>>> sending CLUSTER MEET messages to join the CLUSTER
Waiting for the cluster to join ...
>>> performing Cluster Check (using node 192.168.1.198:7000)
M:2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
slots:5461-10922 (5462 slots) Master
m:e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
Slots: (0 slots) Master
Replicates 02f1958bd5032caca2fd47a56362c8d562d7e621
M:26101DB06B5C2D4431CA8308CF43D51F6939B4FC 192.168.1.198:7002
Slots: (0 slots) Master
Replicates 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299
m:02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
slots:0-5460 (5461 slots) Master
m:6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
slots:10923-16383 (5461 slots) Master
m:ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
Slots: (0 slots) Master
Replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
[OK] All nodes agree about slots configuration.
>>> Check for open Slots ...
>>> Check Slots Coverage ...
[OK] All 16384 slots covered.


7. Test Cluster

Server1 log on to the Redis client and perform

[[email protected] src]# redis-cli-c-P 7000
127.0.0.1:7000> Get key
-Redirected to Slots [12539] located at 192.168.1.199:7004
"Val"
192.168.1.199:7004> Set name test
-Redirected to Slots [5798] located at 192.168.1.198:7000
Ok
192.168.1.198:7000> Set adress Shanghai
-Redirected to Slots [1562] located at 192.168.1.199:7003
Ok
192.168.1.199:7003>server2 Log on to the Redis client and perform


[[email protected] src]# redis-cli-c-P 7003
127.0.0.1:7003> Set key Val
-Redirected to Slots [12539] located at 192.168.1.199:7004
Ok
192.168.1.199:7004> Get Keyv
"Val"
192.168.1.199:7004> Set Key2 Val2
-Redirected to Slots [4998] located at 192.168.1.199:7003
Ok
192.168.1.199:7003> Get Key2
"Val2"
192.168.1.199:7003>


It can be found that the stored time is distributed storage, and is taken from the cluster, and the test is successful.


8. Redis Cluster architecture

1) Redis-cluster Frame composition

Architectural Details:

(1) All Redis nodes are interconnected (ping-pong mechanism), using binary protocols internally to optimize transmission speed and bandwidth.

(2) The fail of the node is effective only when the detection of more than half of the nodes in the cluster fails.

(3) The client is directly connected to the Redis node and does not require an intermediate proxy layer. The client does not need to connect to all nodes in the cluster and connects to any of the available nodes in the cluster

(4) Redis-cluster maps all the physical nodes to the [0-16383]slot, cluster is responsible for maintaining Node<->slot<->value

2) Redis-cluster election: Fault tolerance

(1) Lead the election process is all master in the cluster, if more than half of the master node communicates with the master node (cluster-node-timeout), the current master node is considered to be dead.

(2): When the entire cluster is not available (Cluster_state:fail), when the cluster is not available, all operations on the cluster are not available, received (error) Clusterdown the cluster is down) error

A: If any master of the cluster is hung, and the current master is not slave. The cluster enters the fail state, it can also be understood that the slot mapping into the group [0-16383] does not complete when entering the fail state.

B: If more than half of the master hangs, regardless of whether there is a slave cluster into the fail state.

You may also like the following articles about Redis, as you can see below:

Ubuntu 14.04 Redis installation and simple test http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis Master-slave replication basic configuration http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis Cluster Detail Document Http://www.linuxidc.com/Linux/2013-09/90118.htm

Installation of Redis under Ubuntu 12.10 (graphic) + Jedis connection Redis http://www.linuxidc.com/Linux/2013-06/85816.htm

Redis Series-Installation and deployment Maintenance Chapter Http://www.linuxidc.com/Linux/2012-12/75627.htm

CentOS 6.3 Installation Redis http://www.linuxidc.com/Linux/2012-12/75314.htm

Redis Installation Deployment Learning Note http://www.linuxidc.com/Linux/2014-07/104306.htm

Redis configuration file redis.conf detailed http://www.linuxidc.com/Linux/2013-11/92524.htm

a detailed introduction to Redis : please click here
Redis's : please click here

CentOS Perfect build Redis3.0 cluster with test

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.