Redis cluster construction and simple use "turn"

Source: Internet
Author: User
Tags redis cluster install redis

Redis cluster construction and simple use

Installation environment and version

Simulate 6 nodes with two virtual machines, 3 nodes of one machine, create 3 master, 3 salve environment.

Redis uses the redis-3.2.4 version.

Both virtual machines are Oracle Linux 6.6, one (ip:192.168.31.245), one (ip:192.168.31.210).

installation process

1. Download and unzip

Cd/root/software

wget http://download.redis.io/releases/redis-3.2.4.tar.gz

TAR-ZXVF redis-3.2.4.tar.gz

2. Compiling the installation

CD redis-3.2.4

Make && make install

3. Copy the REDIS-TRIB.RB to the/usr/local/bin directory

CD src

CP redis-trib.rb/usr/local/bin/

4. Create a Redis node

A, first on the 192.168.31.245 machine/root/software/redis-3.2.4 directory under the creation of Redis_cluster directory;

mkdir Redis_cluster

b, under the Redis_cluster directory, create a directory named 7000, 7001, 7002, and copy the redis.conf to these three directories

mkdir 7000 7001 7002

CP redis.conf redis_cluster/7000

CP redis.conf redis_cluster/7001

CP redis.conf REDIS_CLUSTER/7002

C, modify the three configuration files separately, modify the following content

Port 7000//Ports 7000,7002,7003

Bind native IP//default IP for 127.0.0.1 need to change to other node machine accessible IP otherwise the corresponding port cannot be accessed when the cluster is created and the cluster cannot be created

Daemonize Yes//redis background run

Pidfile/var/run/redis_7000.pid//pidfile file corresponds to 7000,7001,7002

cluster-enabled Yes//turn on cluster to remove comment #

Cluster-config-file nodes_7000.conf//cluster configuration profile is first started automatically generated 7000,7001,7002

Cluster-node-timeout 15000//Request Timeout default 15 seconds, can be set by itself

AppendOnly Yes//aof log on when needed to open, it will log each write operation logs

D, and then on another machine (192.168.31.210), the operation of repeating the above three steps, just to change the directory to 7003, 7004, 7005, the corresponding configuration file will be modified according to this rule

5. Start each node

Execution on the first machine

Redis-server redis_cluster/7000/redis.conf

Redis-server redis_cluster/7001/redis.conf

Redis-server redis_cluster/7002/redis.conf

The other machine executes

Redis-server redis_cluster/7003/redis.conf

Redis-server redis_cluster/7004/redis.conf

Redis-server redis_cluster/7005/redis.conf

6. Check Redis boot status

# #一台机器

Ps-ef | grep Redis

Root 61020 1 0 02:14? 00:00:01 redis-server 127.0.0.1:7000 [cluster]

Root 61024 1 0 02:14? 00:00:01 redis-server 127.0.0.1:7001 [cluster]

Root 61029 1 0 02:14? 00:00:01 redis-server 127.0.0.1:7002 [cluster]

NETSTAT-TNLP | grep Redis

TCP 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 0 127.0.0.1:7002 0.0.0.0:* LISTEN 61029/redis-server

1

2

3

4

5

# #另外一台机器

Ps-ef | grep Redis

Root 9957 1 0 02:32? 00:00:01 redis-server 127.0.0.1:7003 [cluster]

Root 9964 1 0 02:32? 00:00:01 redis-server 127.0.0.1:7004 [cluster]

Root 9971 1 0 02:32? 00:00:01 redis-server 127.0.0.1:7005 [cluster]

Root 10065 4744 0 02:38 pts/0 00:00:00 grep--color=auto Redis

NETSTAT-TLNP | grep Redis

TCP 0 0 127.0.0.1:17003 0.0.0.0:* LISTEN 9957/redis-server 1

TCP 0 0 127.0.0.1:17004 0.0.0.0:* LISTEN 9964/redis-server 1

TCP 0 0 127.0.0.1:17005 0.0.0.0:* LISTEN 9971/redis-server 1

TCP 0 0 127.0.0.1:7003 0.0.0.0:* LISTEN 9957/redis-server 1

TCP 0 0 127.0.0.1:7004 0.0.0.0:* LISTEN 9964/redis-server 1

TCP 0 0 127.0.0.1:7005 0.0.0.0:* LISTEN 9971/redis-server 1

7. Create a cluster

Redis Official provides redis-trib.rb this tool, in the extracted directory src directory, the third step has copied it into the/usr/local/bin directory, can be used directly on the command line. Use 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 19 2.168.31.210:7004 192.168.31.210:7005

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

Execution of this command may cause an error:

A, the execution of the above command will be error, because is the execution of the script, error content:/usr/bin/env:ruby:no such file or directory
So you need to install Ruby's environment, it is recommended to use Yum Installruby


Yum Install Ruby


b, and then execute create cluster command, also error, hint missing RubyGems component
Error content:
./redis-trib.rb:24:in ' require ': No such fileto load--RubyGems (Loaderror) from./redis-trib.rb:24


Yum Install RubyGems


C, the second step of the command, but also error, indicating that the Redis can not be loaded because the interface is missing Redis
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

Gem Install Redis


This may not install because the GEM server cannot be connected:
[@zw_22_90 src]# gem install Redis--version3.0.0
Error:could not find a valid gem ' Redis ' (=3.0.0) in any repository
Error:while Executing gem ... (Gem::remotefetcher::fetcherror)


Manual download and installation required:
wget Https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
Gem Install-l./redis-3.2.1.gem

Yum-y install ruby Ruby-devel rubygems rpm-build

Gem Install Redis

After you run the REDIS-TRIB.RB command, the following prompt appears:

Enter Yes, and the following appears to indicate that the installation was successful.

  

8. Cluster validation

On the first machine connected to the cluster 7002 port node, in another connection 7005 node, the connection mode is Redis-cli-h 192.168.31.245-c-P 7002, plus the parameter-C can be connected to the cluster, because the above redis.conf to change bind to IP address, so the-H parameter cannot be omitted.

In the 7005 node execution command set Hello World, the execution results are as follows:

Then on another 7002 port, look at the contents of key for Hello, get hello, execute the result as follows:

Indicates that the cluster is functioning properly.

Simply say the principle

Redis cluster is designed to take into account the central, to the middleware, that is, each node in the cluster is an equal relationship, are equivalent, each node holds its own data and the state of the whole cluster. Each node is connected to all other nodes, and these connections remain active, which ensures that we only need to connect to any of the nodes in the cluster to get data to other nodes.

Redis clusters do not allocate data with traditional consistency hash, but instead use a different method called a hash slot (Hashslot). Redis cluster is assigned 16,384 slots by default, and when we set a key, we use the CRC16 algorithm to get the slot that belongs to, and then divide the key into the hash slot interval node, the algorithm is: CRC16 (key)% 16384. So when we see the set and get at the test, we jump directly to the 7000-port node.

The Redis cluster will have a master node for the data and then synchronize the data between the master and its corresponding salve. When data is read, data is also obtained from the corresponding master node according to the consistent hash algorithm. A corresponding salve node is started only after a master has been hung, acting as master.

It is important to note that you must have 3 or more master nodes, otherwise the cluster will fail, and the entire cluster will not be able to provide services when the number of primary nodes surviving is less than half the total number of nodes.

Redis cluster construction and simple use "turn"

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.