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

premise

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

Explain

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: 192.168.83.139 Port: 6379
From Node 1 (slave): 192.168.83.140 Port: 6379
From Node 2 (slave): 192.168.83.141 Port: 6379

Install redis

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

#!/bin/bash
# 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' ]
        then
                mkdir /usr/local/redis
else
        rm -rf /usr/local/redis
        mkdir /usr/local/redis
fi

cd /usr/local/redis

wget https://download.redis.io/releases/redis-6.2.4.tar.gz

tar xzf redis-6.2.4.tar.gz

cd redis-6.2.4

make

cd src

./redis-server &

./redis-cli

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

After successful installation on 192.168.83.139, copy the script redisInstall.sh 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 redisInstall.sh to 192.168.83.140 and 192.168.83.141
scp redisInstall.sh root@192.168.83.140:/home/admin/script-sh/
scp redisInstall.sh root@192.168.83.141:/home/admin/script-sh/

After the copy is completed, redis are installed on 192.168.83.140 and 192.168.83.141 using the redisInstall.sh 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 (192.168.83.139) allows two secondary nodes (192.168.83.140 and 192.168.83.141 access), which are configured as follows:

   	bind 192.168.83.140 	192.168.83.141
   	
   	#   Or configure it to 0.0.0.0 to indicate that all IP s are accessible
   	# bind 0.0.0.0
  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 open6379Port.sh to open port 6379 to the server's firewall
#!/bin/bash
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 192.168.83.140, the configuration in step 2 is changed to

bind 192.168.83.139 	192.168.83.141

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

replicaof 192.168.83.139  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
./redis-cli 
  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 (192.168.83.139)

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

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

The key values set by the primary node can be obtained from node 192.168.83.141. 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