Centos7 Redis5.0分片集群搭建

 

搭建之后发现,Redis5.0比Redis4.0搭建主从集群方便多了,推荐使用Redis5.0搭建集群

有几个要注意的地方首需要注意:

1、Redis5.0要求在3个主节点的主从集群中,--cluster-replicas 2设置每个主节点的从节点个数,这里每个主节点的从节点个数为2,每台机器平均下来布三个节点

2、另一个注意的问题是防火墙,有可能启动并没有任何问题,但是在使用以下命令创建集群时会失败

redis-cli --cluster create ......

原因有可能是:

1、Redis端口未在防火墙中配置,请把Redis坚挺的端口加入防火墙白名单中。参考:https://blog.csdn.net/XIANZHIXIANZHIXIAN/article/details/78918772

2、Redis集群总线端口未在防火墙中配置,请把Redis集群总线端口加入防火墙白名单中。参考:https://blog.csdn.net/XIANZHIXIANZHIXIAN/article/details/82392172

正式开始安装步骤

1、从官网下载Redis5.0稳定版的压缩包并解压

wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar -xvf redis-5.0.5.tar.gz

2、执行源码编译安装,PREFIX表示安装的目标路径,本例中是/usr/local/redis+监听端口(这里可以偷懒一下只安装一个,然后复制安装完毕后的文件就行,目标路径文件夹不存在的话需要先创建)。如图所示:

cd redis-5.0.5/src
make
make install PREFIX=/usr/local/redis6379

Centos7 Redis5.0分片集群搭建_第1张图片

3、进入配置环节,复制源码包中配置文件到安装路径下,需要配置9个节点的文件,端口和IP按配置类推

cp redis-5.0.5/redis.conf redis6379/
vim redis6379/redis.conf

需要修改配置文件中的内容为下框所示,其中需要注意:各个节点密码都必须一致,否则Redirected就会失败,关于masterauth和requirepass的不同在文末有解答

daemonize yes                       #让Redis可以后台运行
pidfile /var/run/redis_6379.pid     #每个节点的pid文件建议修改为resid_{port}.pid的形式
port 6379                           #Redis监听的端口
bind 192.168.56.103                 #绑定Redis节点所在机器的IP
masterauth passwd1234               #设置密码
requirepass passwd1234              #设置密码
cluster-enabled yes                 #支持集群
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000           #超时时间,单位ms
appendonly yes                      #开启AOF日志

4、启动每台机器上所有的Redis,启动结果如图所示

Centos7 Redis5.0分片集群搭建_第2张图片

5、激动人心的一刻,创建Redis集群命令

./redis-cli --cluster create 192.168.56.103:6379 192.168.56.103:6380 192.168.56.103:6381 192.168.56.102:6382 192.168.56.102:6383 192.168.56.102:6384 192.168.56.104:6385 192.168.56.104:6386 192.168.56.104:6387 --cluster-replicas 2 -a passwd1234

结果成功有图为证,这样一套Redis5.0主从集群就搭建完毕啦

>>> Performing hash slots allocation on 9 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.56.102:6383 to 192.168.56.103:6379
Adding replica 192.168.56.104:6386 to 192.168.56.103:6379
Adding replica 192.168.56.103:6381 to 192.168.56.102:6382
Adding replica 192.168.56.104:6387 to 192.168.56.102:6382
Adding replica 192.168.56.102:6384 to 192.168.56.104:6385
Adding replica 192.168.56.103:6380 to 192.168.56.104:6385
M: 22076b539128c24a17525e342724a019097c1a43 192.168.56.103:6379
   slots:[0-5460] (5461 slots) master
S: b2721b37476c3d882603449b5127602f897b3d77 192.168.56.103:6380
   replicates 5c8e4776df0a606b45a428e5b700cf32375bdfa1
S: 2446a79701b970c6f57cd3ae8fc4b276d49c0480 192.168.56.103:6381
   replicates f37d30f23b5d1c4f6f4ff43cf6d3f8309eb50aaa
M: f37d30f23b5d1c4f6f4ff43cf6d3f8309eb50aaa 192.168.56.102:6382
   slots:[5461-10922] (5462 slots) master
S: 74f0f0e1643e42080d833711bbc21621e2096d92 192.168.56.102:6383
   replicates 22076b539128c24a17525e342724a019097c1a43
S: 0d337229379bf4affdd7b34588d478c1a4e5e569 192.168.56.102:6384
   replicates 5c8e4776df0a606b45a428e5b700cf32375bdfa1
M: 5c8e4776df0a606b45a428e5b700cf32375bdfa1 192.168.56.104:6385
   slots:[10923-16383] (5461 slots) master
S: ba4dfb00796ca00c0e723b7ed7e4a30627a2d787 192.168.56.104:6386
   replicates 22076b539128c24a17525e342724a019097c1a43
S: 10ab954c7d757abb4f9464c00eb04e1336e1bb07 192.168.56.104:6387
   replicates f37d30f23b5d1c4f6f4ff43cf6d3f8309eb50aaa
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.56.103:6379)
M: 22076b539128c24a17525e342724a019097c1a43 192.168.56.103:6379
   slots:[0-5460] (5461 slots) master
   2 additional replica(s)
M: f37d30f23b5d1c4f6f4ff43cf6d3f8309eb50aaa 192.168.56.102:6382
   slots:[5461-10922] (5462 slots) master
   2 additional replica(s)
S: 2446a79701b970c6f57cd3ae8fc4b276d49c0480 192.168.56.103:6381
   slots: (0 slots) slave
   replicates f37d30f23b5d1c4f6f4ff43cf6d3f8309eb50aaa
M: 5c8e4776df0a606b45a428e5b700cf32375bdfa1 192.168.56.104:6385
   slots:[10923-16383] (5461 slots) master
   2 additional replica(s)
S: 0d337229379bf4affdd7b34588d478c1a4e5e569 192.168.56.102:6384
   slots: (0 slots) slave
   replicates 5c8e4776df0a606b45a428e5b700cf32375bdfa1
S: 10ab954c7d757abb4f9464c00eb04e1336e1bb07 192.168.56.104:6387
   slots: (0 slots) slave
   replicates f37d30f23b5d1c4f6f4ff43cf6d3f8309eb50aaa
S: b2721b37476c3d882603449b5127602f897b3d77 192.168.56.103:6380
   slots: (0 slots) slave
   replicates 5c8e4776df0a606b45a428e5b700cf32375bdfa1
S: 74f0f0e1643e42080d833711bbc21621e2096d92 192.168.56.102:6383
   slots: (0 slots) slave
   replicates 22076b539128c24a17525e342724a019097c1a43
S: ba4dfb00796ca00c0e723b7ed7e4a30627a2d787 192.168.56.104:6386
   slots: (0 slots) slave
   replicates 22076b539128c24a17525e342724a019097c1a43
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Centos7 Redis5.0分片集群搭建_第3张图片

6、验证一下,搭建成功

./redis-cli --cluster check 192.168.56.103:6381 -a passwd1234

Centos7 Redis5.0分片集群搭建_第4张图片

关于masterauth和requirepass的不同,如下文所示:

REDIS启用密码认证的疑问
首先,Redis主从和clsuter是支持密码认证的。

其次,关于启用redis密码认证的涉及的几个问题:

1、是否只设置requirepass就可以?masterauth是否需要同步设置?

答案:redis启用密码认证一定要requirepass和masterauth同时设置。

如果主节点设置了requirepass登录验证,在主从切换,slave在和master做数据同步的时候首先需要发送一个ping的消息给主节点判断主节点是否存活,再监听主节点的端口是否联通,发送数据同步等都会用到master的登录密码,否则无法登录,log会出现响应的报错。也就是说slave的masterauth和master的requirepass是对应的,所以建议redis启用密码时将各个节点的masterauth和requirepass设置为相同的密码,降低运维成本。当然设置为不同也是可以的,注意slave节点masterauth和master节点requirepass的对应关系就行。

2、requreipass和master的作用?

masterauth作用:主要是针对master对应的slave节点设置的,在slave节点数据同步的时候用到。

requirepass作用:对登录权限做限制,redis每个节点的requirepass可以是独立、不同的。

 

你可能感兴趣的:(linux)