search
HomeDatabaseRedisHow to build a Redis cluster in CentOS7

1. Manual construction

1. Prepare nodes

CentOS7 to install Redis

The number of nodes must be at least 6 to ensure a complete and high availability configuration Cluster

(1) Directory structure

cluster
├── 9001
│   ├── data
│   │   ├── appendonly.aof
│   │   └── nodes-9001.conf
│   ├── redis-9001.conf
│   └── redis-9001.log
├── 9002
│   ├── data
│   │   ├── appendonly.aof
│   │   └── nodes-9002.conf
│   ├── redis-9002.conf
│   └── redis-9002.log
...

(2) Prepare configuration file

cd cluster
mkdir -p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/data
cp ../redis-6.0.9/redis.conf 9001/redis-9001.conf
vi 9001/redis-9001.conf
    port 9001
    daemonize yes
    bind 192.168.11.40
    dir /root/cluster/9001/data/
    pidfile /var/run/redis_9001.pid
    cluster-enabled yes  # 集群模式运行
    cluster-config-file nodes-9001.conf
    cluster-node-timeout 15000
    # appendonly yes
    logfile "/root/cluster/9001/redis-9001.log"

Copy & Replace::%s/9001/9002/g

(3) Start the service

/usr/local/redis/bin/redis-server /root/cluster/9001/redis-9001.conf
...
tail 9001/redis-9001.log
kill `cat /var/run/redis_9001.pid`

After startup, the cluster configuration file nodes-9001.conf will be automatically created in the data directory. When the node information in the cluster changes, the node will automatically save the cluster status to this configuration file. It is best not to modify it manually. If a cluster configuration file exists at startup, the node will use the configuration file content to initialize the cluster information

$ cat 9001/data/nodes-9001.conf
8ccdb0963411ebd05ce21952bdd4b7597825afdc :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
$ /usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9001
192.168.11.40:9001> cluster nodes
8ccdb0963411ebd05ce21952bdd4b7597825afdc :9001@19001 myself,master - 0 0 0 connected

Node ID: 40-digit hexadecimal string used to uniquely identify a node in the cluster. The node ID is only created once, and the running ID will change every time it is restarted

2. Node handshake

Node handshake A batch of nodes running in cluster mode communicate with each other through the Gossip protocol to communicate with each other Perception

$ /usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9001
192.168.11.40:9001> cluster meet 192.168.11.40 9002
OK
192.168.11.40:9001> cluster nodes
8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 myself,master - 0 0 0 connected
5786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40:9002@19002 master - 0 1620703357871 1 connected

cluster meet is an asynchronous command, which is used for nodes to exchange status data information with each other. It returns immediately after execution and performs internal handshake communication with the target node:

  • Node 9001 creates the 9002 node information object locally and sends the meet message

  • After receiving the meet message, node 9002 saves the 9001 node information and replies to the pong message

  • After that, nodes 9001 and 9002 communicate with each other regularly through ping/pong messages

In the clusterAny node can execute cluster meet command to join a new node. The handshake status will be propagated within the cluster through messages: other nodes will automatically discover the new node and initiate the handshake process.

After the node establishes the handshake, the cluster cannot work normally. At this time, the cluster is in Offline status, all data reading and writing are prohibited. Because the slot is not assigned to the node, the cluster cannot complete the mapping of slots to nodes

192.168.11.40:9001> set hello world
(error) CLUSTERDOWN Hash slot not served
192.168.11.40:9001> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
...

3. Allocate slots

Redis cluster maps all data to 16384 slots middle. Only when all slots are assigned to nodes, the cluster enters the online state

Assigned slots:

/usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9001 cluster addslots {0..5461}
/usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9002 cluster addslots {5462..10922}
/usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9003 cluster addslots {10923..16383}

View cluster information:

192.168.11.40:9001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
...
192.168.11.40:9001> cluster nodes
cebb7ed63d469748d4015ede6b4a0f5ff59c9322 192.168.11.40:9006@19006 master - 0 1620704406746 0 connected
1b7785f80c4712c6ba4abd71cc93027fa85a02f8 192.168.11.40:9005@19005 master - 0 1620704406000 4 connected
8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 myself,master - 0 1620704404000 2 connected 0-5461
9408059de8b2dd712f0a9381a3b7aad561aef206 192.168.11.40:9004@19004 master - 0 1620704407753 5 connected
85ceb9826e8aa003169c46fb4ba115c72002d4f9 192.168.11.40:9003@19003 master - 0 1620704407000 3 connected 10923-16383
5786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40:9002@19002 master - 0 1620704408763 1 connected 5462-10922

Each person is responsible The node that handles the slot should have a slave node to ensure that it can automatically failover when it fails.

The node that is started for the first time and the node to which the slot is assigned are both master nodes. The slave node is responsible for replicating the master node slot information and Related data

192.168.11.40:9004> cluster replicate 8ccdb0963411ebd05ce21952bdd4b7597825afdc
OK
192.168.11.40:9005> cluster replicate 5786e3237c7fa413ed22465d15be721f95e72cfa
OK
192.168.11.40:9006> cluster replicate 85ceb9826e8aa003169c46fb4ba115c72002d4f9
OK
192.168.11.40:9001> cluster nodes
cebb7ed63d469748d4015ede6b4a0f5ff59c9322 192.168.11.40:9006@19006 slave 85ceb9826e8aa003169c46fb4ba115c72002d4f9 0 1620704825926 3 connected
1b7785f80c4712c6ba4abd71cc93027fa85a02f8 192.168.11.40:9005@19005 slave 5786e3237c7fa413ed22465d15be721f95e72cfa 0 1620704825000 1 connected
8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 myself,master - 0 1620704824000 2 connected 0-5461
9408059de8b2dd712f0a9381a3b7aad561aef206 192.168.11.40:9004@19004 slave 8ccdb0963411ebd05ce21952bdd4b7597825afdc 0 1620704824921 2 connected
85ceb9826e8aa003169c46fb4ba115c72002d4f9 192.168.11.40:9003@19003 master - 0 1620704824000 3 connected 10923-16383
5786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40:9002@19002 master - 0 1620704823914 1 connected 5462-10922

2. Use redis-trib.rb to build a cluster (outdated)

[Use redis-cli to build a cluster after Redis5.0]

redis-trib.rb is a Redis cluster management tool implemented in Ruby. Internally, Cluster related commands are used to help us simplify common operation and maintenance operations such as cluster creation, inspection, slot migration and balancing

1. Ruby environment

Install Ruby

wget https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.3.tar.gz
tar -zxvf ruby-2.7.3.tar.gz
cd ruby-2.7.3
./configure --prefix=/usr/local/ruby
make
make install
cd /usr/local/ruby
cp bin/ruby /usr/local/bin
cp bin/gem /usr/local/bin

Install rubygem redis Dependency

wget http://rubygems.org/downloads/redis-4.2.5.gem
gem install -l redis-4.2.5.gem
gem list

Encountered a problem

$ gem install -l redis-4.2.5.gem
ERROR:  Loading command: install (LoadError)
	cannot load such file -- zlib
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args' for nil:NilClass

Solution (same for openssl)

yum -y install zlib-devel
cd ruby-2.7.3/ext/zlib
ruby ./extconf.rb
make
make install

Install redis-trib.rb

cp /{redis_home}/src/redis-trib.rb /usr/local/bin
redis-trib.rb

2. Create a cluster

# --replicas 1:指定集群中每个主节点配备几个从节点
redis-trib.rb create --replicas 1 192.168.11.40:9001 192.168.11.40:9002 192.168.11.40:9003 192.168.11.40:9004 192.168.11.40:9005 192.168.11.40:9006
# 集群完整性检查
redis-trib.rb check 192.168.11.40:9001
  • will automatically complete the node handshake and slot allocation process

  • will try its best to ensure that the master and slave nodes are not allocated on the same machine

  • The order of the node list is used to determine the master-slave role, first the master node and then the slave node

  • The node address must be a node that does not contain any slots/data, otherwise Will refuse to create a cluster

3. Use redis-cli to build a cluster

redis-cli --cluster create 192.168.11.40:9001 192.168.11.40:9002 192.168.11.40:9003 192.168.11.40:9004 192.168.11.40:9005 192.168.11.40:9006 --cluster-replicas 1

The above is the detailed content of How to build a Redis cluster in CentOS7. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. 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 Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft