Redis5.0.7 cluster building (latest version of actual combat!!!)

Experimental environment

Two Centos 7 virtual machines are added with three network cards to simulate the real situation of six servers
Server role IP address
Master node M1
Master node M2
Master node M3
Slave node S1
Slave node S2
Slave node S3

Experimental steps

Step 1: install Redis

#Install build environment
[root@localhost ~]# yum install gcc gcc-c++ make -y

#Remote mount source package
[root@localhost ~]# mount.cifs // /mnt
Password for root@//  

#Decompress the source package
[root@localhost ~]# cd /mnt
[root@localhost mnt]# tar zxvf redis-5.0.7.tar.gz -C /opt

#Compilation and installation
[root@localhost mnt]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis/ install

#Establish service command soft link to the system
[root@localhost redis-5.0.7]# ln -s /usr/redis/bin/* /usr/local/bin/

#Cut into utils directory
[root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/

#Execute startup script
[root@localhost utils]# ./
#Enter by default
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server
#Here, you need to manually specify the extension directory path / usr / local / redis / bin / redis server

#Start service with process control
[root@localhost utils]# /etc/init.d/redis_6379 start
Starting Redis server...

#Configure the 6379.conf file of redis, and add the listening address
[root@localhost utils]# vim /etc/redis/6379.conf 

#Note the listening 127 address on line 70 has listened to all addresses

#Turn off security without comment on line 89
protected-mode no

#Remove the comment on line 93 and open port 6379
port 6379

#Remove comment on line 137 to start as a stand-alone process
daemonize yes

#Remove the comment on line 833 and turn on clustering
cluster-enabled yes

#Remove line 841, cluster name file settings
cluster-config-file nodes-6379.conf

#Remove the comment at line 847 and set the cluster timeout
cluster-node-timeout 15000

#Remove the comment on line 700 to enable aof persistence
appendonly yes

#Restart service
[root@localhost utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

Step 2: install the rvm component on the primary server

#Import key file
[root@localhost utils]# gpg --keyserver hkp:// --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

#Install rvm
[root@localhost utils]# curl -sSL | bash -s stable

#Execute environment variable
[root@localhost utils]# source /etc/profile.d/

#List the versions ruby can install
[root@localhost utils]# rvm list known

#Install version 2.4.1
[root@localhost utils]# rvm install 2.4.1

#Using Ruby Version 2.4.1
[root@localhost utils]# rvm use 2.4.1
Using /usr/local/rvm/gems/ruby-2.4.1

#View the current version of ruby2.4.1
[root@localhost utils]# ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

#Install Redis again
[root@localhost utils]# gem install redis

Step 3: create Redis cluster

#Add all node addresses and 6379 port numbers
[root@localhost utils]# redis-cli --cluster create --cluster-replicas 1 

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica to
Adding replica to
Adding replica to
M: a27b43ec695099b36a5c79beae70cb0364f27338
   slots:[0-5460] (5461 slots) master
M: a27b43ec695099b36a5c79beae70cb0364f27338
   slots:[5461-10922] (5462 slots) master
M: a27b43ec695099b36a5c79beae70cb0364f27338
   slots:[10923-16383] (5461 slots) master
S: b6d317c5f192bf84e8f464ffbf6481529cd0708a
   replicates a27b43ec695099b36a5c79beae70cb0364f27338
S: b6d317c5f192bf84e8f464ffbf6481529cd0708a
   replicates a27b43ec695099b36a5c79beae70cb0364f27338
S: b6d317c5f192bf84e8f464ffbf6481529cd0708a
   replicates a27b43ec695099b36a5c79beae70cb0364f27338
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join

>>> Performing Cluster Check (using node
M: a27b43ec695099b36a5c79beae70cb0364f27338
   slots:[0-16383] (16384 slots) master
   1 additional replica(s)
S: b6d317c5f192bf84e8f464ffbf6481529cd0708a
   slots: (0 slots) slave
   replicates a27b43ec695099b36a5c79beae70cb0364f27338
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Step 4: verify cluster read and write

#Write data to database from master node
[root@localhost ~]# redis-cli -h -p 6379> set name yangjia
OK> get name

#View data information from another node
[root@localhost ~]# redis-cli -h -p 6379> keys *
1) "name"> get name

#Unable to view data at slave node, prompt to switch master node server address
[root@localhost ~]# redis-cli -h -p 6379> keys *
1) "name"> get name
(error) MOVED 5798

In Redis cluster, the master node server is responsible for writing data, and the slave node server is responsible for backing up the real-time data of the master node server

Tags: Linux Redis Ruby CentOS network

Posted on Wed, 11 Dec 2019 13:10:58 -0800 by hwttdz