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

 

 

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 wzy123
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哦。

我们先在redis根目录下

mkdir redis_cluster

并在redis_cluster目录下分别创建7000、7001、7002、7003、7004、7005,并将redis.conf文件cp到这几个文件夹下

[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 21:43 7000
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
cp redis.conf redis_cluster/7000
cp redis.conf redis_cluster/7001
cp redis.conf redis_cluster/7002
cp redis.conf redis_cluster/7003
cp redis.conf redis_cluster/7004
cp redis.conf redis_cluster/7005

然后修改各个文件夹中的redis.conf

port  7000                                        //端口7000,7002,7003        
bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群,我这里是注释掉了,不知道0.0.0.0行不行
daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志 

然后分别启动这6个节点,也可以写个shell一键搞定~

[root@wzy-cloud redis]# cat startRedis.sh 
redis-server /usr/local/redis/redis_cluster/7000/redis.conf
redis-server /usr/local/redis/redis_cluster/7001/redis.conf
redis-server /usr/local/redis/redis_cluster/7002/redis.conf
redis-server /usr/local/redis/redis_cluster/7003/redis.conf
redis-server /usr/local/redis/redis_cluster/7004/redis.conf
redis-server /usr/local/redis/redis_cluster/7005/redis.conf
[root@wzy-cloud redis]# ./startRedis.sh 
[root@wzy-cloud redis]# ps -ef|grep redis
root      6304     1  0 21:44 ?        00:00:00 redis-server 0.0.0.0:7000 [cluster]
root      6306     1  0 21:44 ?        00:00:00 redis-server 0.0.0.0:7001 [cluster]
root      6308     1  0 21:44 ?        00:00:00 redis-server 0.0.0.0:7002 [cluster]
root      6310     1  0 21:44 ?        00:00:00 redis-server 0.0.0.0:7003 [cluster]
root      6318     1  0 21:44 ?        00:00:00 redis-server 0.0.0.0:7004 [cluster]
root      6320     1  0 21:44 ?        00:00:00 redis-server 0.0.0.0:7005 [cluster]
root      6345  4985  0 21:44 pts/0    00:00:00 grep --color=auto redi

所有节点启动好后,输入以下命令创建集群,注意ip哦,是你的本地ip

注意:如果输入命令后没有反应,那可能是防火墙的关系,关掉防火墙就好了

[root@wzy-cloud redis]# 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参数

[root@wzy-cloud redis]# 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

不然会报以下错误

[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 7000 
127.0.0.1:7000> auth wzy123
OK
127.0.0.1:7000> 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> 

 

  • 11
    点赞
  • 6
    评论
  • 30
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值