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

How to build Redis Cluster by CentOS7

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to build a Redis cluster in CentOS7". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to build a Redis cluster in CentOS7.

First, manually build 1. Prepare the node

CentOS7 install Redis

The number of nodes is at least 6 in order to form a complete and highly available 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) prepares the configuration file cd clustermkdir-p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/datacp.. / redis-6.0.9/redis.conf 9001/redis-9001.confvi 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 mode Run cluster-config-file nodes-9001.conf cluster-node-timeout 15000 # appendonly yes logfile "/ root/cluster/9001/redis-9001.log"

Copy & replace:% sqqver 9001max 9002max g

(3) start the service / usr/local/redis/bin/redis-server / root/cluster/9001/redis-9001.conf...tail 9001/redis-9001.logkill `cat / var/run/redis_ 9001.pid`

The cluster profile nodes-9001.conf is automatically created in the data directory after startup. When the information of the nodes in the cluster changes, the nodes will automatically save the cluster state to the configuration file, and it is best not to modify it manually. If a cluster profile exists at startup, the node initializes the cluster information with the contents of the profile

$cat 9001/data/nodes-9001.conf8ccdb0963411ebd05ce21952bdd4b7597825afdc: 0 myself,master-00 connectedvars currentEpoch 0 lastVoteEpoch 0 $/ usr/local/redis/bin/redis-cli-h 192.168.11.40-p 9001192.168.11.40 lastVoteEpoch 9001 > cluster nodes8ccdb0963411ebd05ce21952bdd4b7597825afdc: 900119001 myself,master-00 connected

The node ID:40 bit hexadecimal string is used to uniquely identify a node in the cluster. The node ID is created only once, and it changes each time you run ID and restart

two。 Node handshake

Node handshake means that a group of nodes running in cluster mode communicate with each other through the Gossip protocol to perceive each other.

/ usr/local/redis/bin/redis-cli-h 192.168.11.40-p 9001192.168.11.40 9002OK192.168.11.40:9001 9001 > cluster meet 192.168.11.40 9002OK192.168.11.40:9001 > cluster nodes8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40 cluster nodes8ccdb0963411ebd05ce21952bdd4b7597825afdc 19001 myself,master-00 0 connected5786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40 cluster nodes8ccdb0963411ebd05ce21952bdd4b7597825afdc 9002 master-0 1620703357871 1 connected

Cluster meet is an asynchronous command that exchanges status data information with each other, returns immediately after execution, and communicates internally with the target node by shaking hands:

Node 9001 locally creates 9002 node information objects and sends meet messages

After the node 9002 receives the meet message, it saves the 9001 node information and replies to the pong message

Nodes 9001 and 9002 then communicate periodically with each other through ping/pong messages

Any node in the cluster can execute the cluster meet command to join the new node, and the handshake status will spread through the cluster through the message: other nodes will automatically discover the new node and initiate the handshake process.

After the node establishes the handshake, the cluster does not work properly. At this time, the cluster is offline and all data reading and writing are prohibited. Because the slot is not assigned to the node, the cluster cannot complete the slot-to-node mapping

192.168.11.40 CLUSTERDOWN Hash slot not served192.168.11.40:9001 9001 > set hello world (error) CLUSTERDOWN Hash slot not served192.168.11.40:9001 > cluster infocluster_state:failcluster_slots_assigned:0cluster_slots_ok:0cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:0...3. Distribution slot

The Redis cluster maps all data to 16384 slots. The cluster goes online only when the slots are all allocated to the nodes.

Allocation slot:

/ 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 connected1b7785f80c4712c6ba4abd71cc93027fa85a02f8 9001 > cluster infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3...192.168.11.40:9001 > cluster nodescebb7ed63d469748d4015ede6b4a0f5ff59c9322 192.168.11.40 master-0 1620704406746 0 connected1b7785f80c4712c6ba4abd71cc93027fa85a02f8 192.168.11.40 connected1b7785f80c4712c6ba4abd71cc93027fa85a02f8 19005 master-01620704406000 4 connected8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40 Vol 9001 19001 myself Master-0 1620704404000 2 connected 0-54619408059de8b2dd712f0a9381a3b7aad561aef206 192.168.11.40 master-0 1620704407753 5 connected85ceb9826e8aa003169c46fb4ba115c72002d4f9 192.168.11.40 connected85ceb9826e8aa003169c46fb4ba115c72002d4f9 19003 "19003 master-0 1620704407000 3 connected 10923-163835786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40 44040 9002 master-01620704408763 1 connected 5462-10922

Each node responsible for the processing slot should have a slave node to ensure that it can fail over automatically when it fails.

The node started for the first time and the node assigned to the slot are the master node, and the slave node is responsible for copying the master node slot information and related data.

192.168.11.40 cluster nodescebb7ed63d469748d4015ede6b4a0f5ff59c9322 9004 > cluster replicate 8ccdb0963411ebd05ce21952bdd4b7597825afdcOK192.168.11.40:9005 > cluster replicate 5786e3237c7fa413ed22465d15be721f95e72cfaOK192.168.11.40:9006 > cluster replicate 85ceb9826e8aa003169c46fb4ba115c72002d4f9OK192.168.11.40:9001 > 192.168.11.40 cluster nodescebb7ed63d469748d4015ede6b4a0f5ff59c9322 9006 "19006" 19006 slave 85ceb9826e8aa003169c46fb4ba115c72002d4f9 0 1620704825926 3 connected1b7785f80c4712c6ba4abd71cc93027fa85a02f8 192.168.11.40 cluster nodescebb7ed63d469748d4015ede6b4a0f5ff59c9322 9005 "19005 slave 5786e3237c7fa413ed22465d15be721f95e72cfa 0 1620704825000 1 connected8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.409001 19001 myself Master-0 1620704824000 2 connected 0-54619408059de8b2dd712f0a9381a3b7aad561aef206 192.168.11.40 slave 8ccdb0963411ebd05ce21952bdd4b7597825afdc 01620704824921 2 connected85ceb9826e8aa003169c46fb4ba115c72002d4f9 192.168.11.40 slave 8ccdb0963411ebd05ce21952bdd4b7597825afdc 01620704824921 2 connected85ceb9826e8aa003169c46fb4ba115c72002d4f9 19003 master-0 1620704824000 3 connected 10923-163835786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40 19002 master-01620704823914 1 connected 5462-10922 II, use redis-trib.rb to build clusters (obsolete)

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

Redis-trib.rb is a Redis cluster management tool implemented by Ruby. Internal Cluster-related commands 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.gztar-zxvf ruby-2.7.3.tar.gzcd ruby-2.7.3./configure-- prefix=/usr/local/rubymakemake installcd / usr/local/rubycp bin/ruby / usr/local/bincp bin/gem / usr/local/bin

Install rubygem redis dependencies

Wget http://rubygems.org/downloads/redis-4.2.5.gemgem install-l redis-4.2.5.gemgem list

Encounter problems

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

Solve (same as openssl)

Yum-y install zlib-develcd ruby-2.7.3/ext/zlibruby. / extconf.rbmakemake install

Install redis-trib.rb

Cp / {redis_home} / src/redis-trib.rb / usr/local/binredis-trib.rb2. Create a cluster #-- replicas 1: specify that each master node in the cluster is equipped with several slave nodes redis-trib.rb create-- replicas 1 192.168.11.40 replicas 9001 192.168.11.40 replicas 9002 192.168.11.40 replicas 9003 192.168.11.40 replicas 9004 192.168.11.40 redis-trib.rb check 192.168.11.40 redis-trib.rb check 192.168.11.40 redis-trib.rb check 9001

The process of node handshake and slot allocation is completed automatically.

Will try its best to ensure that the master and slave nodes are not assigned under the same machine.

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

The node address must be a node that does not contain any slots / data, otherwise the creation of a cluster will be refused

3. Use redis-cli to build cluster redis-cli-- cluster create 192.168.11.40 cluster-replicas 9001 192.168.11.40 cluster-replicas 9002 192.168.11.40 cluster-replicas 9003 192.168.11.40 cluster-replicas 9004 192.168.11.40 cluster-replicas 9005 192.168.11.40 CentOS7 9006-Thank you for your reading. This is the content of "how to build a Redis Cluster in CentOS7". I believe you have a deeper understanding of how to build a Redis cluster in CentOS7, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report