Redis 6.2.4 cluster deployment

Preface

Environmental preparation

20.0.0.21   8101   8102
20.0.0.22   8101   8102
20.0.0.23   8101   8102

mkdir /u-data
cd /u-data
Insert picture description here

Yum source replacement, if any, can be omitted

##yum源(1)
cd /etc/yum.repos.d/
rm -rf local.repo
mv backup/CentOS-Base.repo /etc/yum.repos.d/
yum clean all
yum makecache
cd -

##yum源(2)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

Installation dependent environment

redis6版本要求安装gcc要5.3以上的:

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils

echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
source /etc/profile

Install redis

tar zxvf redis-6.2.4.tar.gz
cd redis-6.2.4
make -j3
make install
cd utils/


##注释掉因为systemd不能执行脚本的问题
vim install_server.sh
 76 #bail if this system is managed by systemd
 77 #_pid_1_exe="$(readlink -f /proc/1/exe)"
 78 #if [ "${_pid_1_exe##*/}" = systemd ]
 79 #then
 80 #       echo "This systems seems to use systemd."
 81 #       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
 82 #       exit 1
 83 #fi
 
 mkdir -p /u-data/redis/8101

Execute the installation script

./install_server.sh
输入:
8101                                 ##端口号
/u-data/redis/8101/8101.conf         ##配置文件路径
/u-data/redis/8101/redis_8101.log    ##日志存放路径
/u-data/redis/8101                   ##存放数据目录
回车
回车

直到出现
Copied /tmp/8101.conf => /etc/init.d/redis_8101
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

Edit the first 8101 of the cluster configuration file

这里以20.0.0.21为列:
vim /u-data/redis/8101/8101.conf

70    bind 127.0.0.1 20.0.0.21   ##绑定IP,可直接注释,注释表示绑定本机所有网卡,适用于有公网IP
89    protected-mode no                                  ##关闭保护模式
93    port 8101                                          ##端口号
226   daemonize yes                                      ###独立进程启动
248   pidfile /u-data/redis/8101/redis_8101.pid          ##PID文件
261   logfile /u-data/redis/8101/redis_8101.log          ##日志文件
366   dir /u-data/redis/8101                             ##存储目录
396   masterauth <123456>      ##当master服务设置了密码保护时,slav服务连接master的密码
790   requirepass 123456 
1089  appendonly yes            ##开启aof持久化,目的为了同步持久化,开启后会在/u-data/redis/8101下
1222  cluster-enabled yes       ####开启群集功能
1230  cluster-config-file nodes-8101.conf    ##群集名称文件设置,开启后会在/u-data/redis/8101下
1236  cluster-node-timeout 15000             ##群集超时时间设置

Edit the first 8102 of the cluster configuration file

mkdir /u-data/redis/8102
cp /u-data/redis/8101/8101.conf /u-data/redis/8102/8102.conf
vim /u-data/redis/8102/8102.conf

70    bind 127.0.0.1 20.0.0.21      ##绑定IP,可直接注释,注释表示绑定本机所有网卡
89    protected-mode no                                  ##关闭保护模式
93    port 8102                                          ##端口号
226   daemonize yes                                      ###独立进程启动
248   pidfile /u-data/redis/8102/redis_8102.pid          ##PID文件
261   logfile /u-data/redis/8102/redis_8102.log          ##日志文件
366   dir /u-data/redis/8102                             ##存储目录
396   masterauth <123456>       ##当master服务设置了密码保护时,slav服务连接master的密码
790   requirepass 123456        ##设置Redis连接密码
1089  appendonly yes            ##开启aof持久化,目的为了同步持久化,开启后会在/u-data/redis/8102下
1222  cluster-enabled yes       ####开启群集功能
1230  cluster-config-file nodes-8102.conf    ##群集名称文件设置,开启后会在/u-data/redis/8102下
1236  cluster-node-timeout 15000             ##群集超时时间设置

可直接使用末行模式:
:% s/8101/8102/g
7 次替换,共 5 行

Start redis

[[email protected] utils]# redis-server /u-data/redis/8101/8101.conf 
[[email protected] utils]# redis-server /u-data/redis/8102/8102.conf 

[[email protected] utils]# netstat -anutp | grep 8101
tcp        0      0 0.0.0.0:18101           0.0.0.0:*               LISTEN      10184/redis-server
tcp        0      0 0.0.0.0:8101            0.0.0.0:*               LISTEN      10184/redis-server
tcp6       0      0 :::18101                :::*                    LISTEN      10184/redis-server
tcp6       0      0 :::8101                 :::*                    LISTEN      10184/redis-server
[[email protected] utils]# netstat -anutp | grep 8102
tcp        0      0 0.0.0.0:18102           0.0.0.0:*               LISTEN      10191/redis-server
tcp        0      0 0.0.0.0:8102            0.0.0.0:*               LISTEN      10191/redis-server
tcp6       0      0 :::18102                :::*                    LISTEN      10191/redis-server
tcp6       0      0 :::8102                 :::*                    LISTEN      10191/redis-server

注意:
如果是多网卡,如果不是0.0.0.0:8101
kill -9 redis的PID号,重新启动端口号即可

Create a cluster

在20.0.0.21上执行(这里由于是实验,所以没有区分主和主备):
redis-cli -a 123456 --cluster create 20.0.0.21:8101 20.0.0.21:8102 20.0.0.22:8101 20.0.0.22:8102 20.0.0.23:8101 20.0.0.23:8102 --cluster-replicas 1

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 20.0.0.22:8102 to 20.0.0.21:8101
Adding replica 20.0.0.23:8102 to 20.0.0.22:8101
Adding replica 20.0.0.21:8102 to 20.0.0.23:8101
.....
Can I set the above configuration? (type 'yes' to accept): yes      ##输入yes
........
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Verify cluster function

20.0.0.21上操作:
[[email protected] 8101]# redis-cli -c -h 20.0.0.21 -p 8101 -a 123456
20.0.0.21:8101> CLUSTER INFO         查看集群状态
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:729
cluster_stats_messages_pong_sent:719
cluster_stats_messages_sent:1448
cluster_stats_messages_ping_received:714
cluster_stats_messages_pong_received:729
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1448

20.0.0.21:8101> cluster nodes                      ##查看集群的节点信息
b18b19c58b3dfe2b13c93dda376dc8dac2e4da03 20.0.0.23:[email protected] master - 0 1622876402000 5 connected 10923-16383
f66c56fda21573504ddd2473897313b1fa9f69ae 20.0.0.22:[email protected] master - 0 1622876403695 3 connected 5461-10922
764209ecb51ca4b95aa728d1188a8c5a4e45dd1c 20.0.0.21:[email protected] myself,master - 0 1622876401000 1 connected 0-5460
169524b6bb0508a335643300809fdca36f2ad3c5 20.0.0.23:[email protected] slave f66c56fda21573504ddd2473897313b1fa9f69ae 0 1622876402686 3 connected
d3285fbb9c98fb437d00db6fd003a7c1cebed8a1 127.0.0.1:[email protected] slave b18b19c58b3dfe2b13c93dda376dc8dac2e4da03 0 1622876399660 5 connected
872ae1f9d5efbf96919fab45977f786eb87932a9 20.0.0.22:[email protected] slave 764209ecb51ca4b95aa728d1188a8c5a4e45dd1c 0 1622876404701 1 connected

Write data verification

20.0.0.21:8101> set key111 aaa
-> Redirected to slot [13680] located at 20.0.0.23:8101     ##数据存放20.0.0.23上,哈希槽位13680
OK
20.0.0.23:8101> set key222 bbb
-> Redirected to slot [2320] located at 20.0.0.21:8101      ##数据存放20.0.0.21上,哈希槽位2320
OK
20.0.0.21:8101> set key333 ccc
-> Redirected to slot [7472] located at 20.0.0.22:8101      ##数据存放20.0.0.22上,哈希槽位7472
OK

20.0.0.22:8101> get key111
-> Redirected to slot [13680] located at 20.0.0.23:8101
"aaa"
20.0.0.23:8101> get key222
-> Redirected to slot [2320] located at 20.0.0.21:8101
"bbb"
20.0.0.21:8101> get key333
-> Redirected to slot [7472] located at 20.0.0.22:8101
"ccc"

可以看出redis cluster集群是去中心化的,每个节点都是平等的,连接哪个节点都可以获取和设置数据。
当然,平等指的是master节点,因为slave节点根本不提供服务,只是作为对应master节点的一个备份。