search
HomeDatabaseRedisDetailed explanation of Redis cluster operation examples

This article brings you relevant knowledge about Redis, which mainly organizes issues related to cluster operations, including adding redis instances, configuring 8007 as the master node, and configuring 8008 as 8007 Let’s take a look at the nodes and so on. I hope it will be helpful to everyone.

Detailed explanation of Redis cluster operation examples

Recommended learning: Redis video tutorial

Based on the existing foundation, here is a basic version of three main and three From, the architecture is as follows

##1. Start the cluster
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8001/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8002/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8003/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8004/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8005/redis.conf 
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8006/redis.conf

View cluster status: cluster nodes

As can be seen from the above figure, the entire cluster is running normally, with three master nodes and three slave nodes,

  • The instance node of port 8001 stores hash slots 0-5460,
  • The instance node of port 8002 stores hash slots 5461-10922,
  • The instance node of port 8003 stores these hash slots 10923-16383,

The three master nodes store all the hash slots The storage slots that make up the redis cluster. The slave point is the backup slave node of each master node. The storage slots are not displayed.

2. Cluster operation

We add one more master (8007) and one slave (8008) based on the original cluster. After adding nodes See the following figure for the cluster. New nodes are represented by dotted boxes

2.1. Add a redis instance

## Create 8007 and 8008 folders under /usr/local/redis-cluster, and copy the redis.conf file under the 8001 folder to the 8007 and 8008 folders Next

mkdir 8007 8008 
cd 8001
cp redis.conf /usr/local/redis‐cluster/8007/ 
cp redis.conf /usr/local/redis‐cluster/8008/
 
# 修改8007文件夹下的redis.conf配置文件
vim /usr/local/redis‐cluster/8007/redis.conf 
# 修改如下内容:
port:8007 
dir /usr/local/redis‐cluster/8007/
cluster‐config‐file nodes‐8007.conf 

# 修改8008文件夹下的redis.conf配置文件
vim /usr/local/redis‐cluster/8008/redis.conf
# 修改内容如下:
port:8008
dir /usr/local/redis‐cluster/8008/
cluster‐config‐file nodes‐8008.conf

# 启动8007和8008俩个服务并查看服务状态
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8007/redis.conf
/usr/local/redis‐5.0.3/src/redis‐server /usr/local/redis‐cluster/8008/redis.conf

ps ‐el | grep redis

2.2. Configure 8007 as the master node

Use the add-node command to add a new master node 8007 (master), the previous ip:port is a new node, and the latter ip:port is a known existing node. If you see the "[OK] New node added correctly" prompt at the end of the log, it means that the new node has been added successfully

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster add‐node 192.168.0.61:8007 192.168.0.61:8001

View the cluster status

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐c ‐h 192.168.0.61 ‐p 8001

192.168.0.61:8001> cluster nodes

is as follows:

Note: When the node is added successfully, the newly added node will not have any data because it has not been allocated any slot (hash slot). We need to manually allocate hash for the new node. Slot

Use the redis-cli command to allocate a hash slot for 8007, find any master node in the cluster, and re-shard it

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster reshard 192.168.0.61:8001

2.3. Configure 8008 as the slave node of 8007

Add slave node 8008 to the cluster and check the cluster status

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster add‐node 192.168.0.61:8008 192.168.0.61:8001

is as follows:

##As shown in the figure, it is still a master node. No hash slots have been allocated.

Reason: We need to execute the replicate command to specify the master node ID of the current node (slave node). First, we need to connect the client of the newly added 8008 node, and then Use the cluster command to operate and assign the current 8008 (slave) node to a master node (the 8007 master node created before is used here)

Execute the command

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐c ‐h 192.168.0.61 ‐p 8008 

192.168.0.61:8008> cluster replicate 2728a594a0498e98e4b83a537e19f9a0a3790f38 #后面这串id为8007的节点id
Check the cluster status, node 8008 has been successfully added as the slave node of node 8007

 2.4、删除8080从节点[不删除四主四从]

用del-node删除从节点8008,指定删除节点ip和端口,以及节点id(红色为8008节点id)

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster del‐node 192.168.0.61:8008 a1cfe35722d151cf70585cee212755653 93c0956

再次查看集群状态,如下图所示,8008这个slave节点已经移除,并且该节点的redis服务也已被停止

 2.5、删除8007主节点[不删除四主四从]

因为主节点的里面是有分配了hash槽的,所以我们这里必须先把8007里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能),执行命令如下:

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster reshard 192.168.0.61:8007

迁移验证:会发现8007下面已经没有任何hash槽了,证明迁移成功!

 用del-node命令删除8007主节点即可

/usr/local/redis‐5.0.3/src/redis‐cli ‐a user ‐‐cluster del‐node 192.168.0.61:8007 2728a594a0498e98e4b83a537e19f9a0a 3790f38

查看最终集群状态,发现一切恢复如初,至此水平扩展结束

推荐学习:Redis视频教程

The above is the detailed content of Detailed explanation of Redis cluster operation examples. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),