侧边栏壁纸
博主头像
毕发胖博主等级

沉默会被理解为赞同

  • 累计撰写 8 篇文章
  • 累计创建 0 个标签
  • 累计收到 0 条评论

Redis集群搭建

毕发胖
2022-03-20 / 0 评论 / 0 点赞 / 523 阅读 / 4,309 字
温馨提示:
本文最后更新于 2022-03-20,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Redis集群搭建

第一步:安装Redis
(下次写)

第二步:创建集群目录

[root@localhost redis]# mkdir redis-cluster

第三步:在集群目录下创建节点目录
集群视图.png
搭建集群最少需要三台主节点(主机),如果每台主机需要配置从机的话,就最少需要六台
那么我们的设计如下:创建6 个redis实例,需要端口号7001~7006

[root@localhost myapps]# cp redis/ redis-cluster/7001 -r 

[root@localhost myapps]# cd redis-cluster/7001

[root@localhost 7001]# ll
drwxr-xr-x. 2 root root 4096 3月 1 10:22 bin
-rw-r--r--. 1 root root 3446 3月 1 10:22 dump.rdb
-rw-r--r--. 1 root root 41404 3月 1 10:22 redis.conf

通过复制redis节点,我们查看到,7001存在持久化文件,这里我们删除它

第四步:删除持久化文件

[root@localhost 7001]# rm -rf appendonly.aof dump.rdb

第五步:修改redis.conf配置文件,打开cluster-enabled yes

这里说明一下:cluster-enabled 代表是否支持集群
image20220319234007499.png
第六步:修改端口号,打开redis.conf
image20220319234616105.png
第七步:复制7002-7006机器

[root@localhost redis-cluster]# cp 7001/ 7002 -r
[root@localhost redis-cluster]# cp 7001/ 7003 -r
[root@localhost redis-cluster]# cp 7001/ 7004 -r
[root@localhost redis-cluster]# cp 7001/ 7005 -r
[root@localhost redis-cluster]# cp 7001/ 7006 -r
[root@localhost redis-cluster]# ll
total 28
drwxr-xr-x. 3 root root 4096 Jun 2 00:02 7001
drwxr-xr-x. 3 root root 4096 Jun 2 00:02 7002
drwxr-xr-x. 3 root root 4096 Jun 2 00:02 7003
drwxr-xr-x. 3 root root 4096 Jun 2 00:03 7004
drwxr-xr-x. 3 root root 4096 Jun 2 00:03 7005
drwxr-xr-x. 3 root root 4096 Jun 2 00:03 7006

第八步:修改7002-7006的端口号(重复第六步操作)

第九步:启动7001-7006这六台机器的服务,写一个启动脚本:自定义shell脚本

[root@localhost redis-cluster]# vi start-all.sh
cd 7001
./bin/redis-server ./redis.conf
cd ..
cd 7002
./bin/redis-server ./redis.conf
cd ..
cd 7003
./bin/redis-server ./redis.conf
cd ..
cd 7004
./bin/redis-server ./redis.conf
cd ..
cd 7005
./bin/redis-server ./redis.conf
cd ..
cd 7006
./bin/redis-server ./redis.conf
cd ..

第十步:修改start-all.sh 文件权限

[root@localhost redis-cluster]# chmod u+x start-all.sh

第十一步:启动所有的实例

[root@localhost redis-cluster]# ./start-all.sh

第十二步:创建集群(关闭防火墙)

redis 5.0.5中使用redis-cli --cluster 替代 redis-trib.rb(我这里是 redis5.0.5)

命令如下:

#关闭防火墙
查看防火墙状态: systemctl status firewalld.service
执行关闭命令: systemctl stop firewalld.service
执行开机禁用防火墙自启命令  : systemctl disable firewalld.service
[root@localhost redis_cluster]# cd /home/admin/myapps/redis-cluster/7001/bin
[root@localhost bin]# ./redis-cli --cluster create 192.168.138.161:7001 192.168.138.161:7002 192.168.138.161:7003 192.168.138.161:7004 192.168.138.161:7005 192.168.138.161:7006 --cluster-replicas 1
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: fe3a08119ce85a8383b9f409ee1d669afbdb0757 192.168.138.161:7001	#M表示主节点,slots[表示占用的槽点]
   slots:[0-5460] (5461 slots) master
M: e0113f0d93041cdb2bc0de17099dd91d1b1ddc32 192.168.138.161:7002
   slots:[5461-10922] (5462 slots) master
M: b5ddab2f251f4efdfaef257c3db230e50edd7196 192.168.138.161:7003
   slots:[10923-16383] (5461 slots) master
S: 7da9a5ed9b061a2d1c440b4986d5fda5fda9ce5f 192.168.138.161:7004
   replicates e0113f0d93041cdb2bc0de17099dd91d1b1ddc32
S: cfbad1cdf9e0048c0ef010f1b832ae454a2a13f2 192.168.138.161:7005
   replicates b5ddab2f251f4efdfaef257c3db230e50edd7196
S: 42f0cab8f8352152edf9538ce5100ca8b73d69d0 192.168.138.161:7006
   replicates fe3a08119ce85a8383b9f409ee1d669afbdb0757
Can I set the above configuration? (type 'yes' to accept): 	yes		#这里询问你可以使用上面的配置吗
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
......
>>> Performing Cluster Check (using node 192.168.138.161:7001)
M: fe3a08119ce85a8383b9f409ee1d669afbdb0757 192.168.138.161:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: b5ddab2f251f4efdfaef257c3db230e50edd7196 192.168.138.161:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: cfbad1cdf9e0048c0ef010f1b832ae454a2a13f2 192.168.138.161:7005
   slots: (0 slots) slave
   replicates b5ddab2f251f4efdfaef257c3db230e50edd7196
S: 42f0cab8f8352152edf9538ce5100ca8b73d69d0 192.168.138.161:7006
   slots: (0 slots) slave
   replicates fe3a08119ce85a8383b9f409ee1d669afbdb0757
M: e0113f0d93041cdb2bc0de17099dd91d1b1ddc32 192.168.138.161:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 7da9a5ed9b061a2d1c440b4986d5fda5fda9ce5f 192.168.138.161:7004
   slots: (0 slots) slave
   replicates e0113f0d93041cdb2bc0de17099dd91d1b1ddc32
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.		#集群搭建成功

第十三步:连接集群

命令:

[root@localhost 7001]# ./bin/redis-cli -h 127.0.0.1 -p 7001 -c 	#-c表示指定集群连接,这里的IP换成自己节点的 

测试是否成功:

192.168.138.161:7001> set username bwc		#这里存个值bwc
-> Redirected to slot [14315] located at 192.168.138.161:7003	
#它表示这个值存在了14315的这个槽点中,所在的端口是7003
OK
192.168.138.161:7003> get username	#这个时候发现端口变成了7003 ,我们可以get它,发现可以得到值
"bwc"

#到这里集群就搭建完成啦
0

评论区