Build redis cluster on CentOs7

Keywords: Operation & Maintenance Database Redis

Get ready

  • vm virtual machines (you can also use virtual machines that have a system on wind10)
  • FinalShell Homemade Remote Tools
  • redis-6.2.4
  • centOs 7 System


  1. Install vm virtual machine
  2. FinalShell installed
  3. Create virtual machines for three centOs 7 systems on vm


Creating three centOs7 virtual machines in the vm to install redis requires a minimum of three redis for the redis cluster, because the underlying strategy for determining downtime in redis is to consider all nodes unavailable when they vote for more than half of a node.

The IP addresses of the three virtual machines are as follows:
master: Port: 6379
From Node 1 (slave): Port: 6379
From Node 2 (slave): Port: 6379

Install redis

Create a script file to install redis,, on the server. More scripts)

# Open display of IP address
cd /etc/sysconfig/network-scripts/
sed -i "/^ONBOOT=/s/no/yes/" ifcfg-ens33
service network restart
ip addr

# Install gcc-c++ dependencies
yum install gcc-c++ -y

# Install vim
yum  install vim-enhanced

#Install wget
yum -y install wget

#Start installing redis
if [ ! -d '/usr/local/redis' ]
                mkdir /usr/local/redis
        rm -rf /usr/local/redis
        mkdir /usr/local/redis

cd /usr/local/redis


tar xzf redis-6.2.4.tar.gz

cd redis-6.2.4


cd src

./redis-server &


Installation will be completed after
There is a redis folder under /usr/local/, which is the installation path.

After successful installation on, copy the script with the command to two other servers.

# Format: scp file name root@server ip: address where the destination server will store the file

#For example, copy to and
scp root@
scp root@

After the copy is completed, redis are installed on and using the script, respectively.

Modify redis configuration

Modify the configuration of the primary node redis

  1. Open/usr/local/redis/redis-6.2.4/redis.conf file with vim

  2. Find the configuration for bind, which allows remote access to ip
    For example, the primary node ( allows two secondary nodes ( and access), which are configured as follows:

   	#   Or configure it to to indicate that all IP s are accessible
   	# bind
  1. Find port, change to 6379, (port default 6379)
port 6379
  1. Turn off protection mode so that it is accessible externally. no means turn off
protected-mode no
  1. Set redis to background boot
daemonize yes
  1. Modify redis log storage address
logfile '/usr/local/redis/redis-6.2.4/logs/redis.log'
  1. Set redis connection password
# Format: requirepass password
 requirepass root
  1. Set connection password for accessing primary node from node
# Format: master auth password
masterauth root
  1. Save exit profile after modification is complete
  2. Create a folder and file (corresponding to the configuration in step 6) for the log files under the installation directory of redis.
    Create folder logs under /usr/local/redis/redis-6.2.4/and file redis.log under logs folder
  3. Run the following script file to open port 6379 to the server's firewall
firewall-cmd --add-port=6379/tcp --permanent --zone=public
#Restart the firewall (restart the firewall after modifying the configuration)
firewall-cmd --reload

Configuration is complete by now

Modify the configuration of slave nodes

Use the scp command to copy the redis.conf of the primary node to overwrite the original configuration file from the directory where the node's configuration file is located.
The configuration from the node is modified based on the configuration of the copied primary node.

Open the copied configuration file.

Depending on the modification steps of the primary node, by step 2, modify the bound IP address.
For example, if the current IP from the slave node is, the configuration in step 2 is changed to


After step 8, modify the following configuration, which identifies the IP and port of the primary node

replicaof  6379 

The remaining steps are consistent with the primary node.

Restart redis

  1. Use commands on three servers to view redis that are already running and kill-9 to shut them down
ps -ef|grep redis
kill -9 redis Process Number
  1. Enter the / usr/local/redis/redis-6.2.4/src/directory on three servers and restart redis with the following command
./redis-server ../redis.conf

Tip: You can view the log file at / usr/local/redis/redis-6.2.4/logs/after startup.

Validate Cluster

Verification method

Create a key value on the nodes in the cluster, which can be obtained on other nodes if the cluster is successfully built.

Primary Node

  1. Enter **/usr/local/redis/redis-6.2.4/src/** and enter the client of redis using the following command
  1. Log on to redis using commands after entering the client
# Format: The password that auth configured in requirepass when configuring
auth root
  1. Test whether you have successfully logged in to redis, and the following illustration shows that you have successfully logged in.

  2. Create a key value in the primary node (

  3. Go to other redis registered from nodes ( and
    The login steps are Step 1, Step 2, Step 3.

The key values set by the primary node can be obtained from node The following figure:

The key values set by the primary node can be obtained from node The following figure:

Indicates that the redis cluster has been successfully set up.

Posted by The Jackel on Wed, 15 Sep 2021 12:30:10 -0700