Linux 部署Redis Cluster 集群,最新教程,细到极致

14 篇文章 1 订阅
6 篇文章 0 订阅

  大家上午好呀,几日不见,甚是想念!距离上一次更新已经是一个星期过去了,上个星期,老哥在准备软考考试,所以一直没有时间更新文章。今天考完了,舒服,非常的舒服,为了更加地舒服,今天咱们来更新点东西。
   给各位小伙伴带来的是,最新版的Redis集群,云服务器环境下搭建。希望给小伙伴们一点点小小的帮助。
   话不多说,上车
1、首先,准备Redis安装包,虽然现在最新版Redis7出来了,但是为了避免翻车,老哥选择稳一手,这次集群还是用Redis6,Redis官网下载

在这里插入图片描述
2、上传安装包

[root@VM-4-2-centos redis]# pwd
/opt/redis
[root@VM-4-2-centos redis]# ll
total 2432
-rw-r--r-- 1 root root 2487287 May 28 18:45 redis-6.2.7.tar.gz

3、安装C环境,Redis用C写的

[root@VM-4-2-centos redis]# yum -y install gcc tcl autoconf automake

3A、另外两台服务器创建Redis文件夹,然后分别安装Redis的依赖

[root@VM-12-13-centos ~]# cd /opt/ && mkdir redis && cd redis
[root@VM-4-2-centos redis]# yum -y install gcc tcl autoconf automake

5、切换到第一台服务器,解压Redis安装包

[root@VM-4-2-centos redis]# tar -zxvf redis-6.2.7.tar.gz

6、解压之后,分发到另外两台服务器,不会分发的,可以看我之前mysql集群搭建教程Linux 部署Mysql 8.0集群+脚本分发

[root@VM-4-2-centos redis]# /opt/mysql/xsync_bin/xsync redis-6.2.7

7、在/usr/local 文件夹下创建一个Redis文件夹,方便后续启动,然后另外两台也需要

[root@VM-4-2-centos redis]# cd redis-6.2.7/          
[root@VM-4-2-centos redis-6.2.7]# mkdir -p /usr/local/redis

8、编译,另外两台服务器也需要,出现以下提示,说明正常编译

[root@VM-4-2-centos redis-6.2.7]# make PREFIX=/usr/local/redis install

在这里插入图片描述

9、进入我们编译好的文件夹,然后创建一个conf文件夹,make命令默认不会生成conf文件,需要我们手动创建conf文件夹

[root@VM-4-2-centos redis]# cd /usr/local/redis/
[root@VM-4-2-centos redis]# mkdir conf
[root@VM-4-2-centos redis]# ll
total 8
drwxr-xr-x 2 root root 4096 May 29 09:41 bin
drwxr-xr-x 2 root root 4096 May 29 09:45 conf

10、复制解压的 redis.conf 配置,到/usr/local/redis/conf下
,然后分发一下配置

[root@VM-4-2-centos redis]# cp /opt/redis/redis-6.2.7/redis.conf /usr/local/redis/conf/
[root@VM-4-2-centos redis]# /opt/mysql/xsync_bin/xsync /usr/local/redis/

11、修改配置,然后修改几个地方,
bind是开放任意ip访问,
daemonize 启动后台进程,
requirepass 123456 是密码
然后的话,增加一条命令 replica-announce-ip,暴露你的公网ip地址,方便后面的集群启动

[root@VM-4-2-centos redis]# vi conf/redis.conf
#bind 127.0.0.1

bind 0.0.0.0


# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes


# requirepass foobared
  
 requirepass 123456
 
replica-announce-ip 你的公网ip地址

12、分发配置,其他的不修改,只需要修改一下,replica-announce-ip 你的公网ip地址

[root@VM-4-2-centos redis]# /opt/mysql/xsync_bin/xsync /usr/local/redis/conf/

13、分别启动3台服务器的Redis

[root@VM-4-2-centos redis]# ./bin/redis-server ./conf/redis.conf

14、查看进程,然后我们尝试连接一下,输入ping命令,没有问题

[root@VM-12-13-centos redis]# ps -ef |grep redis 
root     2022796       1  0 10:14 ?        00:00:00 ./bin/redis-server 0.0.0.0:6379
root     2022825 2012940  0 10:14 pts/0    00:00:00 grep --color=auto redis
[root@VM-4-2-centos redis]# ./bin/redis-cli -p 6379 -a 123456
127.0.0.1:6379> ping
PONG

15、开放云服务端口6379,然后使用Desktop manager连接上去,没有任何问题
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

16、现在三台服务器的Redis都启动了,但是还没有形成集群,切换到第二台服务器,然后修改conf配置,增加两条命令,然后这里解释一下,命令是当前节点 依附于master节点,然后指明ip和端口,有两种选择,replicaof 和 slaveof

注意:
replicaof 是新版本的命令
slaveof 是旧版本(redis5.0以前)的命令

[root@VM-4-12-centos redis]# vi conf/redis.conf
# replicaof <masterip> <masterport>

replicaof 101.35.245.191 6379

#主节点的密码
# masterauth <master-password>

masterauth 123456

17、第三台服务器参照上一步操作,然后分别重启第二台、第三台

[root@VM-12-13-centos redis]# ./bin/redis-cli -p 6379 -a 123456 shutdown
[root@VM-12-13-centos redis]# ./bin/redis-server ./conf/redis.conf

18、然后切换到第一台服务器,我们需要设置为master节点启动,修改配置文件,主节点需要开启集群模式,其他节点不需要,不要搞出问题了

[root@VM-4-2-centos redis]# vi conf/redis.conf
#cluster-enabled yes

cluster-enabled yes

19、重启,再次启动主节点

[root@VM-4-2-centos redis]# ./bin/redis-cli -p 6379 -a 123456 shutdown
[root@VM-4-2-centos redis]#  ./bin/redis-server ./conf/redis.conf

20、然后我们需要为Redis集群分配Hash槽,如果不分配,会报错 (error) CLUSTERDOWN Hash slot not served

[root@VM-4-2-centos redis]# ./bin/redis-cli -a 123456 --cluster fix 101.35.245.191:6379

21、然后连接上去Redis集群,查看相关信息

[root@VM-4-2-centos redis]# ./bin/redis-cli -p 6379 -a 123456 
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=101.34.7.236,port=6379,state=online,offset=350,lag=0
slave1:ip=124.222.204.94,port=6379,state=online,offset=350,lag=0
master_failover_state:no-failover
master_replid:417c5ef99bd055e0d080cd84038951a6aa76f485
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:350
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:350
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:1
cluster_current_epoch:1
cluster_my_epoch:1
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

22、然后我们测试主节点设值,从节点读取

127.0.0.1:6379> set aaa bbb
OK

切换到第二台服务器:

[root@VM-4-12-centos redis]# ./bin/redis-cli -p 6379 -a 123456   
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get aaa
"bbb"

切换到第三台服务器:

[root@VM-12-13-centos redis]# ./bin/redis-cli -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get aaa
"bbb"

23、然后我们测试从节点赋值,正常来讲,从节点是只读状态,不可以直接赋值

127.0.0.1:6379> set ccc ddd
(error) READONLY You can't write against a read only replica.

24、至此,整个Redis集群搭建完毕!最后,老铁们,别忘了点赞、关注、收藏三连。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jesscia ^_^

您的打赏将是我努力的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值