Home >Database >Redis >How to build a Redis cluster in CentOS7

How to build a Redis cluster in CentOS7

WBOY
WBOYforward
2023-05-26 13:34:061243browse

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:yisu.com. If there is any infringement, please contact admin@php.cn delete