码迷,mamicode.com
首页 > 其他好文 > 详细

Redis集群搭建

时间:2020-01-09 19:03:39      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:over   not   comm   cal   creat   span   src   aof   自己   

参考文章:https://blog.csdn.net/aloneno/article/details/96370167

Redis简单集群搭建及(error)NOAUTH Authentication required等问题的解决方法

一、redisz主从集群最少需要6个节点
首先我们既然要搭建集群,那么master节点至少要3个,slave节点也是3个,为什么呢?这是因为一个redis集群如果要对外提供可用的服务,那么集群中必须要有过半的master节点正常工作。基于这个特性,如果想搭建一个能够允许 n 个master节点挂掉的集群,那么就要搭建2n+1个master节点的集群。(感觉和Zookeeper的vote机制差不多)

如:

2个master节点,挂掉1个,则1不过半,则集群down掉,无法使用,容错率为0

3个master节点,挂掉1个,2>1,还可以正常运行,容错率为1

4个master节点,挂掉1个,3>1,还可以正常运行,但是当挂掉2个时,2=2,不过半,容错率依然为1

如果创建集群时设置slave为1个

--cluster-replicas 1

则当总节点少于6个时会有如下报错:

[root@wzy-cloud 7000]# redis-cli --cluster create 0.0.0.0:7000 0.0.0.0:7001 0.0.0.0:7002 --cluster-replicas 1 -a redis
Warning: Using a password with -a or -u option on the command line interface may not be safe.
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 1 replicas per node.
*** At least 6 nodes are required.

所以集群搭建至少需要6个节点哦~

 

二、redis集群开始搭建

首先尼,由于条件限制,我只有一台服务器,所以节点ip都一致,只是端口不同,有条件的朋友可以多ip哦(现在有docker,可以很方便的实现)。

我们先在redis根目录下

mkdir redis_cluster

并在redis_cluster目录下分别创建7001、7002、7003、7004、7005、7006,并将redis目录下的 redis.conf 文件cp到这几个文件夹下(注意redis.conf中包含了登录密码)

[root@wzy-cloud redis_cluster]# pwd
/usr/local/redis/redis_cluster
[root@wzy-cloud redis_cluster]# ll
total 24
drwxr-xr-x 2 root root 4096 Jul 17 16:36 7001
drwxr-xr-x 2 root root 4096 Jul 17 16:36 7002
drwxrwxr-x 2 root root 4096 Jul 17 16:37 7003
drwxrwxr-x 2 root root 4096 Jul 17 16:37 7004
drwxrwxr-x 2 root root 4096 Jul 17 16:38 7005

drwxrwxr-x 2 root root 4096 Jul 17 16:38 7006
 

这里我们已经创建好了 

然后复制 redis.conf

cp redis.conf redis_cluster/7001

这里我们先只复制一个到 7001目录下,因为我们需要对reids.conf做一些配置

#注释掉下面,不绑定任何的服务器ip地址
#bind xxxxxxxxxxx

#暴露出去的端口号,如果在一台服务器上搭建集群,则需要每一个集群节点的端口号都不同
port 7001

#允许后台运行
daemonize yes

#这里的路径自己通过pwd获取
pid  /redis-7001目录的路径/redis-7001.pid

#开启集群模式
cluster-enable yes

#配置每个节点的配置文件,同样以端口号为名称
cluster-config-file nodes-7001.conf

#配置集群节点的超时时间,可配可不配
cluster-node-timeout 15000

#启动aof增量持久化策略。不建议使用
appendonly yes

#发生改变就记录日志
appendfsync always

注意:其中的logfil和dir可以给值,也可以不给

 

到这里我们已经将7001的redis.conf配置好了。只需将7001的redis.conf复制到其他节点的目录下,并且更改 7001为对应的的即可

 

启动redis

技术图片

 

 

 进入到redis的根目录下

技术图片

 

 

 依次启动六个redis节点

.src/redis-server ./redis_cluster/7001/redis.conf
.src/redis-server ./redis_cluster/7002/redis.conf
.src/redis-server ./redis_cluster/7003/redis.conf
.src/redis-server ./redis_cluster/7004/redis.conf
.src/redis-server ./redis_cluster/7005/redis.conf
.src/redis-server ./redis_cluster/7006/redis.conf

如果加了密码,那么创建集群时就需要加-a password参数

 

[root@wzy-cloud redis]# redis-cli --cluster create   114.116.35.252:7001  114.116.35.252:7002  114.116.35.252:7003  114.116.35.252:7004  114.116.35.252:7005
114.116.35.252:7006  --cluster-replicas 1 -a redis

不然会报以下错误

[ERR] Node  114.116.35.252:7000 NOAUTH Authentication required.

输入创建集群的命令后会出现以下提示,注意Can I set the above configuration? (type ‘yes‘ to accept): yes,该处请输入yes,不然好像分配不了哈希槽(别人说的,没试)

[root@wzy-cloud redis_cluster]# redis-cli --cluster create  114.116.35.252:7000  114.116.35.252:7001  114.116.35.252:7002  114.116.35.252:7003  114.116.35.252:7004  114.116.35.252:7005  --cluster-replicas 1 -a wzy123
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  114.116.35.252:7003 to  114.116.35.252:7000
Adding replica  114.116.35.252:7004 to  114.116.35.252:7001
Adding replica  114.116.35.252:7005 to  114.116.35.252:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4e571c020d1f2cca020132a9adfdea2a367da21d  114.116.35.252:7000
   slots:[0-5460] (5461 slots) master
M: 838382153a78260e274c1d2d11a105dd3986a223  114.116.35.252:7001
   slots:[5461-10922] (5462 slots) master
M: e86d01e92214015304461a104a9f14e3cedc7829  114.116.35.252:7002
   slots:[10923-16383] (5461 slots) master
S: 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594  114.116.35.252:7003
   replicates 838382153a78260e274c1d2d11a105dd3986a223
S: 5248474e122d745b7e929a2705da210d3d150b4c  114.116.35.252:7004
   replicates e86d01e92214015304461a104a9f14e3cedc7829
S: b3d20a419df22b4c9f4fe14c1fda22c2920c5c11  114.116.35.252:7005
   replicates 4e571c020d1f2cca020132a9adfdea2a367da21d
Can I set the above configuration? (type yes to accept): yes

输完yes后,会出现如下提示,[OK] All 16384 slots covered.说明成功啦

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 0.0.0.0:7000)
M: 4e571c020d1f2cca020132a9adfdea2a367da21d  114.116.35.252:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: b3d20a419df22b4c9f4fe14c1fda22c2920c5c11  114.116.35.252:7005
   slots: (0 slots) slave
   replicates 4e571c020d1f2cca020132a9adfdea2a367da21d
M: e86d01e92214015304461a104a9f14e3cedc7829  114.116.35.252:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 22b1f3d83f068973c6e8a5d0b9e87c0c1b950594  114.116.35.252:7003
   slots: (0 slots) slave
   replicates 838382153a78260e274c1d2d11a105dd3986a223
M: 838382153a78260e274c1d2d11a105dd3986a223  114.116.35.252:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 5248474e122d745b7e929a2705da210d3d150b4c  114.116.35.252:7004
   slots: (0 slots) slave
   replicates e86d01e92214015304461a104a9f14e3cedc7829
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

进行验证

[root@wzy-cloud redis_cluster]# redis-cli -c -p 7001 
127.0.0.1:7001> auth redis
OK
127.0.0.1:7001> cluster nodes
b3d20a419df22b4c9f4fe14c1fda22c2920c5c11  114.116.35.252:7005@17005 slave 4e571c020d1f2cca020132a9adfdea2a367da21d 0 1563378113000 6 connected
e86d01e92214015304461a104a9f14e3cedc7829  114.116.35.252:7002@17002 master - 0 1563378115000 3 connected 10923-16383
22b1f3d83f068973c6e8a5d0b9e87c0c1b950594  114.116.35.252:7003@17003 slave 838382153a78260e274c1d2d11a105dd3986a223 0 1563378116490 4 connected
838382153a78260e274c1d2d11a105dd3986a223  114.116.35.252:7001@17001 master - 0 1563378114486 2 connected 5461-10922
5248474e122d745b7e929a2705da210d3d150b4c  114.116.35.252:7004@17004 slave e86d01e92214015304461a104a9f14e3cedc7829 0 1563378115488 5 connected
4e571c020d1f2cca020132a9adfdea2a367da21d  114.116.35.252:7000@17000 myself,master - 0 1563378114000 1 connected 0-5460
 114.116.35.252:7000> set qwe 111
OK
 114.116.35.252:7000>exit
 
 
[root@wzy-cloud 7000]# redis-cli -c -p 7003 
 114.116.35.252:7003> auth wzy123
OK
 114.116.35.252:7003> get qwe
-> Redirected to slot [757] located at 127.0.0.1:7000
(error) NOAUTH Authentication required.
 114.116.35.252:7000> auth wzy123
OK
127.0.0.1:7000> get qwe
"111"
127.0.0.1:7000> 

 

 

设置redis集群管理工具:

vi /usr/local/soft/redis.5.0.6/utils/create-cluster/create-cluster

技术图片

 

 

 端口PROT设置为7000,NODES为6,工具会自动累加1 生成 7001-7006 六个节点 用于操作。

 

修改后,执行如下

命令关闭集群:

/usr/loca/soft/redis-5.0.6/utils/create-cluster/create-cluster stop

启动集群:

/usr/loca/soft/redis-5.0.6/utils/create-cluster/create-cluster start

或者仍然使用老方法启动集群:

 


先分别启动六个节点:

/usr/loca/soft/redis-5.0.6/src/redis-server //usr/local/soft/redis-5.0.6/redis_cluster/7001/redis.conf
/usr/loca/soft/redis-5.0.6/src/redis-server //usr/local/soft/redis-5.0.6/redis_cluster/7002/redis.conf
/usr/loca/soft/redis-5.0.6/src/redis-server //usr/local/soft/redis-5.0.6/redis_cluster/7003/redis.conf
/usr/loca/soft/redis-5.0.6/src/redis-server //usr/local/soft/redis-5.0.6/redis_cluster/7004/redis.conf
/usr/loca/soft/redis-5.0.6/src/redis-server //usr/local/soft/redis-5.0.6/redis_cluster/7005/redis.conf
/usr/loca/soft/redis-5.0.6/src/redis-server //usr/local/soft/redis-5.0.6/redis_cluster/7006/redis.conf

然后

/usr/loca/soft/redis-5.0.6/src/redis-cli --cluster create 192.168.2.40:7001 192.168.2.40:7002 192.168.2.40:7003 192.168.2.40:7004 192.168.2.40:7005 192.168.2.40:7006 --cluster-replicas 1 -a password

Redis集群搭建

标签:over   not   comm   cal   creat   span   src   aof   自己   

原文地址:https://www.cnblogs.com/zzh-blog/p/12172895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!