Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed Guide to Building Redis clusters

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Chicken or egg first?

Recently, a friend asked a question: to go to a big city or a small city after graduation? To a big company or a small company? My answers are all big cities! Big company!

Why do you say that? think about it. No matter whether girls and boys find friends, they like to find tall ones with big breasts. By the same token, "big" is always good.

Of course, it would be perfect if you had the ability to find a tall guy with big breasts.

Brief introduction of Redis Cluster

Redis is an open source key-value storage system. Due to its excellent performance, most Internet companies use it as a server-side cache. Redis only supports single instance mode before version 3.0. although it supports master-slave mode and Sentinel mode deployment to solve a single point of failure, now Internet enterprises often have hundreds of gigabytes of data, but it is completely unable to meet the needs of the business. Therefore, Redis has launched the cluster mode after version 3.0.

The Redis cluster adopts the P2P model and is completely decentralized. Redis divides all Key into 16384 slot, and each Redis instance is responsible for some of the slot. All the information in the cluster (nodes, ports, slot, etc.) is updated through regular data exchanges between nodes.

The Redis client can make a request in any Redis instance, and if the required data is not in that instance, direct the client to the desired instance through the redirect command.

Casually set up a cluster

Installing and deploying any application is actually very simple, as long as the installation steps are taken step by step. Let's talk about the Redis cluster building plan. Since the cluster needs at least 6 nodes (3 master and 3 slave mode), there are not so many machines for me to play with, and I cannot afford so many virtual machines locally (the computer is too bad). Now the plan is to simulate a cluster on one machine, which is no different from the cluster building in the production environment.

I'm going to start building a cluster under a CentOS that already has Redis installed. If you don't know how to install Redis under Linux, you can read this article, "learn about Redis and install and configure it under CentOS". Note that all of the cluster-building environments below are based on the installed Redis.

1. Create a folder

We plan that the port number of the Redis node in the cluster is 9001-9006, which means each instance folder under the cluster. The data is stored in the port number / data folder.

Mkdir / usr/local/redis-clustercd redis-cluster/mkdir-p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/data

two。 Copy execution script

Create a bin folder under / usr/local/redis-cluster to hold the cluster running scripts and copy the running scripts under the src path of the installed Redis. Look at the command:

Mkdir redis-cluster/bincd / usr/local/redis/srccp mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server redis-trib.rb / usr/local/redis-cluster/bin

3. Copy a new Redis instance

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

Cp / usr/local/redis/* / usr/local/redis-cluster/9001

Note that the only difference between modifying the redis.conf configuration and a single point is the following figure, and the rest are the regular ones:

Port 9001 (port number of each node) daemonize yesbind 192.168.119.131 (bind current machine IP) dir / usr/local/redis-cluster/9001/data/ (data file storage location) pidfile / var/run/redis_9001.pid (pid 9001 and port to correspond) cluster-enabled yes (boot cluster mode) cluster-config-file nodes9001.conf (9001 and port to correspond) cluster-node-timeout 15000appendonly yes

The key point of cluster configuration is to cancel the three configurations in the following figure:

4. Five new Redis instances are copied

We have already completed one node, in fact, the next step is to mechanically complete the other five nodes. In fact, we can do this: copy the 9001 instance to another five folders, and the only thing you need to modify is all the port-related information in the redis.conf. In fact, there are only four locations. To start the 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 / 9001amp * / usr/local/redis-cluster/9006

The\ cp-rf command is copied without an alias, because cp is actually an alias cp-I, and it is annoying to have interactive confirmation during operation.

5. Modify the redis.conf file of 9002-9006

In fact, it is very simple, you will find that there are only four points that need to be modified, let's replace it globally, enter the corresponding node folder, and do the replacement. The command is very simple. Look at the picture:

Vim redis.conf:%s/9001/9002g

After you enter, you will be prompted to replace several places successfully. If you are not sure, you can check it manually:

In fact, we have just replaced the following four lines:

Port 9002dir / usr/local/redis-cluster/9002/data/cluster-config-file nodes-9002.confpidfile / var/run/redis_9002.pid

At this point, we have taken care of the most basic environment, and the next step is to start.

Actually, we've almost built it.

1. Start 9001-9006 six nodes

Cut the crap and look directly 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

You can check whether the startup is successful: ps-el | grep redis

It can be seen that all six nodes have been started successfully.

two。 Try to find any node to test.

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

The connection was successful, but it seems to have made a mistake.

(error) CLUSTERDOWN Hash slot not served (no hash slots for clusters), what the heck is this?

This is because although we have configured and started the Redis cluster service, they are not in a cluster yet, cannot find each other directly, and have no storage location, which is called slot (slot).

3. Install the software required for the cluster

Since the Redis cluster needs to use the ruby command, we need to install ruby and related interfaces.

Yum install rubyyum install rubygemsgem install redis

This is the real creation of a cluster.

Don't talk nonsense, just hit the command:

/ usr/local/redis-cluster/bin/redis-trib.rb create-- replicas 1 192.168.119.131VR 9001 192.168.119.131R 9002 192.168.119.131R 9003 192.168.119.131005 9004 192.168.119.131005 192.168.119.1319006

Explain this command briefly: call the ruby command to create a cluster.-- replicas 1 indicates that the master-slave replication ratio is 1:1, that is, one master node corresponds to one slave node. Then, we are assigned each master node and corresponding slave node service by default, as well as the size of the solt, because there are only 16383 solt in the Redis cluster, which will be evenly distributed to us by default. Of course, you can specify, and subsequent nodes can also be reassigned.

M: 10222dee93f6a1700ede9f5424fccd6be0b2fb73 primary node Id

S: 9ce697e49f47fec47b3dc290042f3cc141ce5aeb 192.168.119.131Id 9004 replicates 10222dee93f6a1700ede9f5424fccd6be0b2fb73 corresponding to master node under slave node

At present, 9001-9003 master node, 9004-9006 slave node, and confirm to you whether you agree to this configuration. After entering yes, the cluster creation will begin.

The above picture indicates that the cluster has been built successfully!

Verify:

It is still connected through the client command, and take a look at the status and node information through the cluster command.

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

Through the command, you can see in detail the cluster information and the status of each node, master-slave information, as well as the number of connections, slot information and so on. From this point of view, we have really built and deployed the Redis cluster successfully!

Set a mafly:

You will find that when we set name mafly, the Redirected to slot message appears and automatically connects to the 9002 nodes. This is also a data allocation feature of the cluster, which will not be discussed in detail here.

To sum up.

This article on Redis cluster deployment and construction really comes down step by step. As long as you install my steps, you can ensure that you can successfully build a Redis cluster for fun. It can also be said that, except for the tedious steps, there is almost no technical content, and people who can finish it are estimated to feel tired.

The next step may be dynamic expansion, increase and decrease of nodes, reallocation of slot size, and of course, the most important thing is how to integrate with our program, and how to better make the Redis cache cluster play its due effect.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report