Deploy Redis pseudo-cluster on one server

redis-trib.rb Today’s article introduces how to build a Redis cluster (three masters and three slaves) through tools on a server (taking CentOS 7.9 as an example). 

redis-trib.rb It is a script written based on Ruby. Its functions cover all aspects of creating, managing and maintaining Redis clusters.

It's worth noting that over time, some newer versions of Redis have been  redis-trib.rb marked as deprecated

In view of this, the Redis version used in this article is the older version 4.0.9

For newer versions of Redis, Xianyu recommends using  redis-cli tools for cluster operation and management to ensure consistency with the latest features of Redis.

Let’s draw the architecture first, so that it will be very convenient for deployment later.

image

# 三个 Master
192.168.149.131:6379
192.168.149.131:6380
192.168.149.131:6381

# 三个 Slave
192.168.149.131:26379
192.168.149.131:26380
192.168.149.131:26381

It should be noted that in Redis Cluster, nodes establish TCP connections and use the gossip protocol to spread cluster information. The internal communication port of the node is the service port + 10000.

For example, after starting the Redis service, there will be a 6379 port (external port) and a 16379 port (internal communication port)

Start deployment

Preparation before deployment

First create a Redis storage directory to store rdb files, etc., and then create a pid file and log file storage directory.

mkdir -pv /var/data/{6379,6380,6381,26379,26380,26381}

mkdir /usr/local/redis-4.0.9/{pid,log} -pv

Install Redis and related dependent tools

Let’s first install redis and related dependencies (need to have access to the Internet)

# 安装相关依赖
yum install -y wget gcc gcc-c++ make tar openssl openssl-devel cmake

# 安装到 /usr/local 下
cd /usr/local/ && wget https://download.redis.io/releases/redis-4.0.9.tar.gz

# 解压缩
tar -xvf  redis-4.0.9.tar.gz

After decompression is complete, we enter the directory and start compilation and installation.

cd redis-4.0.9/ && make && make install

Configure and start the Redis service

First, let’s configure the configuration files of the six Redis services.

ll /usr/local/redis-4.0.9/
-rwxr-xr-x  1 root root   1524 Aug 24 17:21 redis-26379.conf
-rwxr-xr-x  1 root root   1524 Aug 24 17:18 redis-26380.conf
-rwxr-xr-x  1 root root   1524 Aug 24 17:18 redis-26381.conf
-rwxr-xr-x  1 root root   1519 Aug 24 17:21 redis-6379.conf
-rwxr-xr-x  1 root root   1519 Aug 24 17:17 redis-6380.conf
-rwxr-xr-x  1 root root   1519 Aug 24 17:17 redis-6381.conf

The configuration items that need to be changed in the configuration file are the following, and the others remain unchanged.

# 需要改动的地方
...
port 6379
...
pidfile /usr/local/redis-4.0.9/pid/redis-6379.pid
...
logfile /usr/local/redis-4.0.9/log/redis-6379.log
...
dir /var/data/6379
...
cluster-config-file nodes-6379.conf
...

After configuration, the six Redis services are started in sequence

/usr/local/redis-4.0.9/src/redis-server redis-6379.conf && /usr/local/redis-4.0.9/src/redis-server redis-26379.conf

/usr/local/redis-4.0.9/src/redis-server redis-6380.conf && /usr/local/redis-4.0.9/src/redis-server redis-26380.conf

/usr/local/redis-4.0.9/src/redis-server redis-6381.conf && /usr/local/redis-4.0.9/src/redis-server redis-26381.conf

Set up Ruby environment

redis-trib.rb Is a Ruby script used to create, manage and maintain Redis clusters

It provides a command line interface to perform various cluster operations such as adding nodes, deleting nodes, balancing data distribution, etc.

If you want to use  redis-trib.rb this Ruby script to manage Redis Cluster, you need to install the Ruby interpreter

Download the rvm tool first (try several times due to network problems)

#1.下载密钥
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
#2.下载安装包(网络问题多试几次)
curl -L get.rvm.io | bash -s stable

image

# 验证是否安装成功
[root@localhost]# find / -name rvm
/usr/local/rvm
/usr/local/rvm/src/rvm
/usr/local/rvm/src/rvm/bin/rvm
/usr/local/rvm/src/rvm/lib/rvm
/usr/local/rvm/src/rvm/scripts/rvm
/usr/local/rvm/bin/rvm
/usr/local/rvm/lib/rvm
/usr/local/rvm/scripts/rvm

# 启用 RVM 的环境变量
source /etc/profile.d/rvm.sh 
#查看依赖
rvm requirements

#验证 rvm 版本
rvm -v
# 安装 ruby
/usr/local/rvm/bin/rvm install ruby-3.0.0

# 验证 ruby 版本
ruby -v

redis-trib.rb PS: If you find an error after using the tool after installing ruby 

/usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- redis (LoadError)
from /usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from ./redis-trib.rb:25:in `<main>'

According to the prompts, you can know that  redis-trib.rb the script cannot find  redis the module, causing the loading to fail. This is usually because the required Ruby Redis module is missing from the system or the module version problem

Run the following command to install the Redis Gem module:

# 安装 redis 模块,是 redis-trib.rb 脚本所需的依赖
gem install redis

Create a Redis cluster

Create a cluster ( --replicas 1 meaning specifying that each master has a slave)

cd /usr/local/redis-4.0.9/ && ./src/redis-trib.rb create --replicas 1 192.168.149.131:6379 192.168.149.131:26379 192.168.149.131:6380 192.168.149.131:26380 192.168.149.131:6381 192.168.149.131:26381

image


As can be seen from the picture above

master 1: 192.168.149.131:6379 ;slave 1:192.168.149.131:26380
master 2: 192.168.149.131:26381;slave 2:192.168.149.131:6380
master 3: 192.168.149.131:26379;slave 3:192.168.149.131:6381
# 查看集群信息
cd /usr/local/redis-4.0.9/ && ./src/redis-trib.rb info 192.168.149.131:6379

cd /usr/local/redis-4.0.9/ && ./src/redis-trib.rb check 127.0.0.1:6379

verify

After creating the cluster, we simply verify: write a data, you can see that the data is redirected to master 3

[root@localhost /usr/local/redis-4.0.9]# redis-cli -c -p 6379
127.0.0.1:6379> set name Edison
-> Redirected to slot [5798] located at 192.168.149.131:26379
OK

Then we check whether there is this data on slave 3 and whether the synchronization is completed.

[root@localhost /usr/local/redis-4.0.9]# redis-cli -c -p 26379
127.0.0.1:26379> get name
"Edison"

Guess you like

Origin blog.csdn.net/qq_41221596/article/details/132920563