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

What is the Redis cluster?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "what is a Redis cluster". In daily operation, I believe many people have doubts about what is a Redis cluster. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful for you to answer the doubts about "what is a Redis cluster?" Next, please follow the editor to study!

Cluster building

Redis released a stable version 5.0 in October 2018, and introduced a variety of new features, one of which is the migration of cluster management tools from Ruby-based redis-trib.rb to C-based redis-cli to facilitate cluster construction and management.

Redis Cluster cluster needs at least 3 master nodes to run, and at least 3 master, 3 slaves and 6 nodes to achieve high availability

The following steps are based on Redis version 5.0.5 and describe how to build a Redis cluster with 3 masters, 3 slaves and 6 nodes on a Linux server

Step 1 create the installation directory

Mkdir-p / data/project/redis-cluster

Step 2 download the source code and decompress the compilation

Cd / data/project/redis-clusterwget http://download.redis.io/releases/redis-5.0.5.tar.gztar xzf redis-5.0.5.tar.gzcd redis-5.0.5make

After make, if you get the error "jemalloc/jemalloc.h: there is no such file or directory", you can use the following command instead:

Make MALLOC=libc

Step 3 create 6 Redis configuration files. 6 configuration files cannot be in the same directory. The following 6 Redis nodes are installed on port 7000 / 7005 respectively. First, create a configuration file directory and files, as defined as follows:

Mkdir-p / data/project/redis-cluster/nodes/7000mkdir-p / data/project/redis-cluster/nodes/7001mkdir-p / data/project/redis-cluster/nodes/7002mkdir-p / data/project/redis-cluster/nodes/7003mkdir-p / data/project/redis-cluster/nodes/7004mkdir-p / data/project/redis-cluster/nodes/7005touch / data/project/redis-cluster/nodes/7000/redis.conftouch / data/project/redis-cluster/nodes/7001/redis.conftouch / data/ Project/redis-cluster/nodes/7002/redis.conftouch / data/project/redis-cluster/nodes/7003/redis.conftouch / data/project/redis-cluster/nodes/7004/redis.conftouch / data/project/redis-cluster/nodes/7005/redis.conf

The contents of the redis.conf configuration file are:

# # Network # # Port port 700mm unprotected mode, if the value is yes It must be the machine connection of the ip specified by the bind configuration or connect with the password protected-mode no # # generic # # run daemonize yes # record the redis process pidpidfile / var/run/redis_7000.pid# in the background # # Cluster # # enable cluster mode cluster-enabled yes cluster-config-file nodes_7000.conf# cluster node if it is unreachable within the timeout period (milliseconds) Then it is considered that the node is in a fault state cluster-node-timeout 5000 persistent # # AOF RDB persistent file directory dir / data/project/redis-cluster/nodes# enable AOF persistent appendonly yes# AOF file name appendfilename "appendonly_7000.aof" # when the current aof file size exceeds how much of the last rewritten aof file size, override auto-aof-rewrite-percentage 10 sets the minimum aof file size that is allowed to be rewritten It is necessary to rewrite the auto-aof-rewrite-min-size 64mb# RDB filename dbfilename dump_7000.rdb to avoid reaching the agreed percentage but the size is still very small.

The configurations of port, pidfile, cluster-config-file, appendfilename and dbfilename need to be adjusted according to different nodes.

For instructions on configuration items, please refer to redis-5.0.5/redis.conf. Each item is described in detail. It is recommended to read

Step 4 start the node

/ data/project/redis-cluster/redis-5.0.5/src/redis-server / data/project/redis-cluster/nodes/7000/redis.conf/data/project/redis-cluster/redis-5.0.5/src/redis-server / data/project/redis-cluster/nodes/7001/redis.conf/data/project/redis-cluster/redis-5.0.5/src/redis-server / data/project/redis-cluster/nodes/7002/redis.conf/data/project/redis -cluster/redis-5.0.5/src/redis-server / data/project/redis-cluster/nodes/7003/redis.conf/data/project/redis-cluster/redis-5.0.5/src/redis-server / data/project/redis-cluster/nodes/7004/redis.conf/data/project/redis-cluster/redis-5.0.5/src/redis-server / data/project/redis-cluster/nodes/7005/redis.conf

Ps-ef | grep redis, you can see that 6 redis processes have been started:

Step 5 start the cluster with the following command, and the IP address will be replaced by itself:

/ data/project/redis-cluster/redis-5.0.5/src/redis-cli-- cluster create 192.168.56.102 cluster-replicas 7000 192.168.56.102 cluster-replicas 7001 192.168.56.102 7002 192.168.56.102

The startup success message is as follows:

At this point, the Redis Cluster cluster has been built.

View cluster information

Redis5 added a series of cluster OPS features to redis-cli to view the details of the command:

/ data/project/redis-cluster/redis-5.0.5/src/redis-cli-- cluster help

For the specific role of command parameters, please refer to the official documentation. The following will manage the cluster based on some of the commonly used commands.

Check node status

/ data/project/redis-cluster/redis-5.0.5/src/redis-cli-- cluster check 192.168.56.102

View cluster information

/ data/project/redis-cluster/redis-5.0.5/src/redis-cli-- cluster info 192.168.56.102

Cluster expansion

The cluster now has three masters and three slaves, and four new nodes are added to expand the capacity to five masters and five slaves.

Step 1 start the new node to create 4 Redis configuration files with port number 7006 to 7009, and then start the node (see steps 3 and 4 of "Cluster Building")

Step 2 New node join cluster setup 4 nodes join the existing redis cluster, 2 master nodes, 2 slave nodes

/ data/project/redis-cluster/redis-5.0.5/src/redis-cli-- cluster add-node 192.168.56.102 data/project/redis-cluster/redis-5.0.5/src/redis-cli 7006 192.168.56.102 cluster add-node 7007 192.168.56.102 cluster add-node 7007 192.168.56.102 data/project/redis-cluster/redis-5.0.5/src/redis-cli is the id of node 7006 The slave node / data/project/redis-cluster/redis-5.0.5/src/redis-cli that joins the cluster on behalf of this node and is 7006-- cluster add-node 192.168.56.102 cluster-slave 7008 192.168.56.102 cluster-slave-- cluster-master-id 24e2c369678952b07d95c0a4b49c2d7a7b2e2bf7 # 24e2c is the id of node 7007 Slave node / data/project/redis-cluster/redis-5.0.5/src/redis-cli that joins the cluster on behalf of this node and is 7007-- cluster add-node 192.168.56.102 cluster-slave 7009 192.168.56.102 cluster-slave-- cluster-master-id ab0f74a19819a74238df7a510494e9418678cbe1

At this point, the cluster status is as follows, in which no slot has been assigned to master node 7006 and master node 7007, which will be assigned in the following steps:

Step 3 simulate slot rebalancing allocation based on the rebalance command, add the-- cluster-simulat parameter to see which slots will be migrated without actually performing the migration operation

/ data/project/redis-cluster/redis-5.0.5/src/redis-cli-- cluster rebalance 192.168.56.102 cluster-threshold 7000-- cluster-use-empty-masters-- cluster-simulat

The following migration information is returned:

Step 4 execute slot rebalance allocation execute rebalance command, balance the number of cluster nodes slot, reassign slot (remove-- cluster-simulat)

/ data/project/redis-cluster/redis-5.0.5/src/redis-cli-- cluster rebalance 192.168.56.102 cluster-threshold 7000-- cluster-use-empty-masters

At this point, if the cluster capacity is expanded and reduced, you need to move the slot in the node to be offline to another node based on reshard, and then delete the node based on the del-node command.

At this point, the study on "what is a Redis cluster" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report