Redis 集群单机安装和多服务器安装过程(一)

2022/1/17 19:03:37

本文主要是介绍Redis 集群单机安装和多服务器安装过程(一),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

      本次安装的是编辑版的redis,分别从单机安装和多服务器安装全过程,不论是单机还是多机安装都需要进行如下操作,安装redis,下载地址:Redis

1.创建安装目录

cd /app/cmw
mkdir redis
cd redis

2.解压安装包

tar -xvzf redis-6.2.6.tar.gz
cd redis-6.2.6

3.授予src目录下文件执行权限

chmod +x -R src

4.安装redis

make MALLOC=libc
make install

2.单机安装三主三从

1.创建集群文件夹

cd /app/cmw/redis
mkdir cluster
cd cluster

2.创建配置、数据、日志目录

mkdir -p data conf logs scripts

3.创建节点7001配置文件

cd conf
vi redis-7001.conf
在该配置文件中输入如下信息
port 7001
daemonize yes
dir "/app/cmw/redis/cluster/data"
logfile "/app/cmw/redis/cluster/logs/7001.log"
#dbfilename不能配置为路径
dbfilename "dump-7001.rdb"
cluster-enabled yes
cluster-config-file nodes-7001.conf
#是否需要每个节点都可用,集群才算可用,关闭
cluster-require-full-coverage no

4.复制节点7001到7002、7003、7004、7005、7006

sed "s/7001/7002/g" redis-7001.conf >redis-7002.conf
sed "s/7001/7003/g" redis-7001.conf >redis-7003.conf
sed "s/7001/7004/g" redis-7001.conf >redis-7004.conf
sed "s/7001/7005/g" redis-7001.conf >redis-7005.conf
sed "s/7001/7006/g" redis-7001.conf >redis-7006.conf

5.启动redis服务

redis-server  redis-7001.conf
redis-server  redis-7002.conf
redis-server  redis-7003.conf
redis-server  redis-7004.conf
redis-server  redis-7005.conf
redis-server  redis-7006.conf
后续启动可以写一个批量启动的脚本,放在文档末尾


6.构建集群

redis-cli -p 7001 cluster meet 192.168.245.135 7002
redis-cli -p 7001 cluster meet 192.168.245.135 7003
redis-cli -p 7001 cluster meet 192.168.245.135 7004
redis-cli -p 7001 cluster meet 192.168.245.135 7005
redis-cli -p 7001 cluster meet 192.168.245.135 7006
查看节点以及结果
redis-cli -p 7001 cluster nodes
4228b57d5a303fabcf992bf2a11acd5ba644d289 192.168.245.134:7001@17001 master,myself  0 1641293323000 12 connected
0945ef9ecaeb9ab46e8622eb68d4457f94f9c58d 192.168.245.134:7003@17003 master - 0 1641293327642 9 connected 
fe9d495d0c1d4164897d919478b3532ea3e4617b 192.168.245.134:7002@17002 master - 0 1641293324000 13 connected 
7d23f4957d9f290b8197586674e9ea9706be65c1 192.168.245.134:7005@17005 master  0 1641293325610 13 connected
f90701eb18229fa672d9846980e3e04f65cd02bb 192.168.245.134:7006@17006 master  0 1641293326614 9 connected
8f363f5a2db3448658b694255a8a11f1afdd248d 192.168.245.134:7004@17004 master - 0 1641293325000 12 connected 

7.设置主从关系将7001:7004、7002:7005、7003:7006做为主:从关系节点

redis-cli -p 7004 cluster replicate 4228b57d5a303fabcf992bf2a11acd5ba644d289
redis-cli -p 7005 cluster replicate fe9d495d0c1d4164897d919478b3532ea3e4617b
redis-cli -p 7006 cluster replicate 0945ef9ecaeb9ab46e8622eb68d4457f94f9c58d
查看节点信息 redis-cli -p 7001 cluster nodes
4228b57d5a303fabcf992bf2a11acd5ba644d289 192.168.245.134:7001@17001 myself,master - 0 1641293323000 12 connected
0945ef9ecaeb9ab46e8622eb68d4457f94f9c58d 192.168.245.134:7003@17003 master - 0 1641293327642 9 connected
fe9d495d0c1d4164897d919478b3532ea3e4617b 192.168.245.134:7002@17002 master - 0 1641293324000 13 connected
7d23f4957d9f290b8197586674e9ea9706be65c1 192.168.245.134:7005@17005 slave fe9d495d0c1d4164897d919478b3532ea3e4617b 0 1641293325610 13 connected
f90701eb18229fa672d9846980e3e04f65cd02bb 192.168.245.134:7006@17006 slave 0945ef9ecaeb9ab46e8622eb68d4457f94f9c58d 0 1641293326614 9 connected
8f363f5a2db3448658b694255a8a11f1afdd248d 192.168.245.134:7004@17004 slave 4228b57d5a303fabcf992bf2a11acd5ba644d289 0 1641293325000 12 connected

8.编写脚本分配slots(槽点)
cd /app/cmw/redis/cluster/scripts
vi addslots.sh 

start=$1
end=$2
port=$3

for slot in `seq ${start} ${end}`
do 
	redis-cli -p ${port} cluster addslots ${slot}
done

echo ${port}  slots ${start} - ${end} done

执行脚本
sh addslots.sh 0 5460 7001
sh addslots.sh 5461 10922  7002
sh addslots.sh 10923 16383  7003

9.验证槽点分配情况

redis-cli -p 7001 cluster slots

#显示如下,证明成功
0
5460
192.168.245.135
7001
4228b57d5a303fabcf992bf2a11acd5ba644d289
192.168.245.135
7004
8f363f5a2db3448658b694255a8a11f1afdd248d
10923
16383
192.168.245.135
7003
0945ef9ecaeb9ab46e8622eb68d4457f94f9c58d
192.168.245.135
7006
f90701eb18229fa672d9846980e3e04f65cd02bb
5461
10922
192.168.245.135
7002
fe9d495d0c1d4164897d919478b3532ea3e4617b
192.168.245.135
7005
7d23f4957d9f290b8197586674e9ea9706be65c1

10.验证集群是否成功
cluster_state:fail
cluster_slots_assigned:16384
cluster_slots_ok:0
cluster_slots_pfail:16384
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:13
cluster_my_epoch:12
cluster_stats_messages_ping_sent:405385
cluster_stats_messages_pong_sent:385152
cluster_stats_messages_fail_sent:20
cluster_stats_messages_auth-req_sent:5
cluster_stats_messages_auth-ack_sent:2
cluster_stats_messages_update_sent:3
cluster_stats_messages_sent:790567
cluster_stats_messages_ping_received:385147
cluster_stats_messages_pong_received:405332
cluster_stats_messages_fail_received:13
cluster_stats_messages_auth-req_received:5
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_update_received:2
cluster_stats_messages_received:790501

11.设置密码

redis-cli -p 7001 -c 
config set masterauth 123456
config set requirepass 123456









这篇关于Redis 集群单机安装和多服务器安装过程(一)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程