Which came first, the chicken or the egg?

Recently, a friend asked me a question about going to a big city or a small city after graduation. Big company or small company? My answer is always big cities! Big companies! Why do you say so? If you think about it, no matter girls or boys looking for friends, they all like to find tall people with big breasts. In the same way, big is always better. Of course, if you want to be able to find a big chest tall even more perfect.

Introduction to the Redis cluster

Redis is an open source key-value storage system. Due to its outstanding performance, most Internet enterprises use it for server-side caching. Redis only supports single instance mode before version 3.0. Although it supports master-slave mode and sentry mode deployment to solve single point of failure, nowadays Internet enterprises often need hundreds of GIGABytes of data, which cannot completely meet business requirements. Therefore, Redis launched cluster mode after version 3.0.

Redis cluster adopts P2P model and is completely decentralized. Redis divides all keys into 16,384 slots, and each Redis instance is responsible for some of the slots. All information (nodes, ports, and slots) in a cluster is updated periodically through data exchange between nodes. A Redis client can make a request from any Redis instance, and if the desired data is not in the instance, the client can be directed to the desired instance by a redirection command.

Build a cluster randomly

Installing and deploying any application is actually very simple, as long as the installation step by step. Here said the Redis cluster structures, planning, because of the cluster will take at least six nodes (3 main 3 from mode), so, no so many machines for me to play, I will also play no more local virtual machine (computer is too bad), now plan is on one machine to simulate a cluster, of course, this and cluster structures, no essential difference between the production environment.

If you are not familiar with how to install Redis on Linux, you can read the article “Learn about Redis and Install and configure CentOS”. Please note that all of the cluster builder environments below are based on the installed Redis.

1. Create a folder. Port numbers of Redis nodes in the cluster are 9001-9006, and port numbers are instance folders in the cluster. The data is stored in the port number /data folder.

mkdir /usr/local/redis-cluster
cd redis-cluster/
mkdir -p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/data
Copy the code

2. Copy execution scripts. Create a bin folder in /usr/local/redis-cluster to store the cluster running scripts, and copy the scripts from the SRC directory of the installed redis. See the command:

mkdir redis-cluster/bin
cd /usr/local/redis/src
cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server redis-trib.rb /usr/local/redis-cluster/bin
Copy the code

We now copy a new instance from the installed Redis into the 9001 folder and modify the redis.conf configuration.

cp /usr/local/redis/* /usr/local/redis-cluster/9001
Copy the code

Note that the only difference between modifying the redis.conf configuration and a single point is the following:

Port 9001 daemonize yes bind 192.168.119.131 dir /usr/local/redis-cluster/9001/data/ Pidfile /var/run/redis_9001.pid (PID 9001 and port must correspond) cluster-enabled yes (cluster mode) cluster-config-file Nodes9001. conf (9001 corresponds to port) cluster-node-timeout 15000 appendonly yesCopy the code

The key to cluster setup configuration is to uncomment the three configurations shown in the following figure:

Now that we have one Redis node, we need to mechanically complete the other five nodes. Copy the 9001 instance to five other folders. The only thing to change is all the port-related information in redis. Start operation, look at the picture:

\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9002
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9003
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9004
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9005
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9006
Copy the code

The \cp -rf command is copied without an alias, because cp is actually the alias cp -i, which can be annoying with interactive validation.

9002-9006 redis.conf file is actually very simple, you can search to find that there are only four points need to be modified, let’s change the global, enter the corresponding node folder, do the replacement. The command is very simple, look at the picture:

vim redis.conf
:%s/9001/9002g
Copy the code

After enter, there will be several places to replace the successful prompt, do not trust can manually check:

We’re essentially replacing the following four lines:

port 9002
dir /usr/local/redis-cluster/9002/data/
cluster-config-file nodes-9002.conf
pidfile /var/run/redis_9002.pid
Copy the code

At this point, we’ve got the most basic environment out of the way, and it’s time to boot up.

Actually, we’ve almost built it

1. Start the six nodes 9001-9006

Cut the crap and just look at the picture:

/usr/local/bin/redis-server /usr/local/redis-cluster/9001/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9002/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9003/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9004/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9005/redis.conf 
/usr/local/bin/redis-server /usr/local/redis-cluster/9006/redis.conf
Copy the code

Can check whether startup success: ps – el | grep redis look out, six nodes have all started successfully.

2. Test a node

/usr/local/redis-cluster/bin/redis-cli -h 192.168.119.131 -p 9001

set name mafly
Copy the code

The connection succeeded, but it seems to be an error. (error) CLUSTERDOWN Hash slot not served, what the hell is this? This is because although we have configured and started the Redis cluster service, they are not in the same cluster, cannot be directly discovered, and there is no storage location (so-called slot).

Since Redis clustering requires Ruby commands, we need to install Ruby and related interfaces.

yum install ruby
yum install rubygems
gem install redis 
Copy the code

This is really creating a cluster

Without further ado, type the order directly:

Rb create --replicas 1 192.168.119.131:9001 192.168.119.131:9002 192.168.119.131:9003 192.168.119.131:9004 192.168.119.131:9005 192.168.119.131:9006Copy the code

–replicas 1 means that the replicas ratio is 1:1 for each primary and secondary node. We are then assigned by default to each of the master nodes and slave node services, as well as the size of solts. Since there are only 16,383 Solts in the Redis cluster, by default we are allocated evenly, you can specify of course, and any additional or subtracted nodes can be reassigned.

M: 10222 dee93f6a1700ede9f5424fccd6be0b2fb73 master node Id

S: 9 ce697e49f47fec47b3dc290042f3cc141ce5aeb 192.168.119.131:9004 10222 dee93f6a1700ede9f5424fccd6be0b2fb73 replicates Id of the primary node under the secondary node

At present, 9001-9003 is the primary node and 9004-9006 is the secondary node. I would like to confirm whether you agree with this configuration. After entering yes, cluster creation begins.

The above picture represents the cluster set up successfully!!

Verify: still through the client command to connect, through the cluster command to see the status and node information.

/usr/local/redis-cluster/bin/redis-cli -c -h 192.168.119.131 -p 9001
cluster info
cluster nodes
Copy the code

This section describes how to view cluster information, node status, master/slave information, connection number, and slot information. We have successfully deployed the Redis cluster.

Set a MAFLY: You’ll notice that when we set name mafly, the Redirected to Slot message appears and the 9002 node is automatically connected. This is also a data allocation feature of clustering, which is not detailed here.

To summarize

This article on the Redis cluster deployment build really is step by step go down, as long as you are my steps to installation, ensure you can successfully build a Redis cluster play, also can say so, in addition to the steps and trival, almost there is no technical content, estimates that can read people feel tired (seriously, write this article is really tiring).

The next step may be to dynamically expand, add and reduce nodes, redistribute slot size, etc., of course, and the most important thing is how to combine with our program, and how to better play the Redis cache cluster due to the effect, these are the most important.

The typhoon is not very severe tonight, and it seems that I have not slept and woke up in a day for a long time…

Original address:www.cnblogs.com/mafly/p/red…