Table of Contents
1. Manual construction
1. Prepare nodes
(1) Directory structure
(2) Prepare configuration file
(3) Start the service
2. Node handshake
3. Allocate slots
2. Use redis-trib.rb to build a cluster (outdated)
1. Ruby environment
2. Create a cluster
3. Use redis-cli to build a cluster
Home Database Redis How to build a Redis cluster in CentOS7

How to build a Redis cluster in CentOS7

May 26, 2023 pm 01:34 PM
redis centos

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
...
Copy after login

(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 after login

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`
Copy after login

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
Copy after login

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
Copy after login

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
...
Copy after login

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}
Copy after login

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
Copy after login

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
Copy after login
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
Copy after login

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
Copy after login

Install rubygem redis Dependency

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

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
Copy after login

Solution (same for openssl)

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

Install redis-trib.rb

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

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
Copy after login
  • 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
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to implement the underlying redis How to implement the underlying redis Apr 10, 2025 pm 07:21 PM

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

How to view the version number of redis How to view the version number of redis Apr 10, 2025 pm 05:57 PM

To view the Redis version number, you can use the following three methods: (1) enter the INFO command, (2) start the server with the --version option, and (3) view the configuration file.

What to do if redis-server can't be found What to do if redis-server can't be found Apr 10, 2025 pm 06:54 PM

Steps to solve the problem that redis-server cannot find: Check the installation to make sure Redis is installed correctly; set the environment variables REDIS_HOST and REDIS_PORT; start the Redis server redis-server; check whether the server is running redis-cli ping.

How is the key unique for redis query How is the key unique for redis query Apr 10, 2025 pm 07:03 PM

Redis uses five strategies to ensure the uniqueness of keys: 1. Namespace separation; 2. HASH data structure; 3. SET data structure; 4. Special characters of string keys; 5. Lua script verification. The choice of specific strategies depends on data organization, performance, and scalability requirements.

How to view all keys in redis How to view all keys in redis Apr 10, 2025 pm 07:15 PM

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

How to use redis zset How to use redis zset Apr 10, 2025 pm 07:27 PM

Redis Ordered Sets (ZSets) are used to store ordered elements and sort by associated scores. The steps to use ZSet include: 1. Create a ZSet; 2. Add a member; 3. Get a member score; 4. Get a ranking; 5. Get a member in the ranking range; 6. Delete a member; 7. Get the number of elements; 8. Get the number of members in the score range.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

See all articles