Redis5单机多节点集群和多机多节点集群以及出现的问题

第一步:安装Redis

wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar xzf redis-5.0.0.tar.gz
cd redis-5.0.0
make

第二步:修改配置,创建节点
我们现在要搞六个节点,三主三从,

端口规定分别是7001,7002,7003,7004,7005,7006

我们先在root目录下新建一个redis_cluster目录,然后该目录下再创建6个目录,

分别是7001,7002,7003,7004,7005,7006,用来存在redis配置文件;

这里我们要使用redis集群,要先修改redis的配置文件redis.conf

mkdir redis_cluster 新建目录

[root@localhost ~]# cd redis_cluster/

[root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006

先复制一份配置文件到7001目录下

[root@localhost redis_cluster]# cd

[root@localhost ~]# cp redis-5.0.0/redis.conf redis_cluster/7001/

我们修改下这个配置文件

vi redis_cluster/7001/redis.conf

修改一下几个

port 7001 //六个节点配置文件分别是7001-7006

daemonize yes //redis后台运行

pidfile /var/run/redis_7001.pid //pidfile文件对应7001-7006

cluster-enabled yes //开启集群

cluster-config-file nodes_7001.conf //保存节点配置,自动创建,自动更新对应7001-7006

cluster-node-timeout 5000 //集群超时时间,节点超过这个时间没反应就断定是宕机

appendonly yes //存储方式,aof,将写操作记录保存到日志中。
//下面可以不写
#若设置密码,master和slave需同时配置下面两个参数:
masterauth “sfsxxx” #连接master的密码
requirepass “jadsfx” #自己的密码 
大体配置例如

port 7001 #端口
cluster-enabled yes #启用集群模式
cluster-config-file nodes_7001.conf #集群的配置 配置文件首次启动自动生成
cluster-node-timeout 5000 #超时时间 5秒
appendonly yes #aof日志开启 它会每次写操作都记录一条日志
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile  /var/run/redis_7001.pid

7001下的修改完后,我们把7001下的配置分别复制到7002-7006 然后对应的再修改下配置即可;

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/

[root@localhost ~]# vi redis_cluster/7002/redis.conf 

[root@localhost ~]# vi redis_cluster/7003/redis.conf 

[root@localhost ~]# vi redis_cluster/7004/redis.conf 

[root@localhost ~]# vi redis_cluster/7005/redis.conf 

[root@localhost ~]# vi redis_cluster/7006/redis.conf 

编辑后面5个配置文件,把 port ,pidfile,cluster-config-file 分别修改下即可;

第三步:启动六个节点的redis

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 

启动六个节点

[root@localhost ~]# ps -ef | grep redis

查找下redis进程

第四步:创建集群

redis官方提供了redis-trib.rb工具,第一步里已经房到里bin下 ;

但是在使用之前 需要安装ruby,以及redis和ruby连接

yum -y install ruby ruby-devel rubygems rpm-build
gem install redis

redis-trib.rb create --replicas 1  127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 (redis3.0版本用这个)
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005  --cluster-replicas 1(redis5.0版本)

效果如下:

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:7001

127.0.0.1:7002

127.0.0.1:7003

Adding replica 127.0.0.1:7004 to 127.0.0.1:7001

Adding replica 127.0.0.1:7005 to 127.0.0.1:7002

Adding replica 127.0.0.1:7006 to 127.0.0.1:7003

M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004

   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c

S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005

   replicates d61e66e49e669b99d801f22f6461172696fdd1c9

S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006

   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18

Can I set the above configuration? (type 'yes' to accept): 

从运行结果看 主节点就是7001 7002 7003 从节点分别是7004 7005 7006

7001分配到的哈希槽是 0-5460

7002分配到的哈希槽是 5461-10922

7003分配到的哈希槽是 10923-16383

最后问我们是否接受上面的设置,输入yes 就表示接受,我们输入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 127.0.0.1:7001)

M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006

   slots: (0 slots) slave

   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18

M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005

   slots: (0 slots) slave

   replicates d61e66e49e669b99d801f22f6461172696fdd1c9

M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004

   slots: (0 slots) slave

   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

显示配置哈希槽,以及集群创建成功,可以用了;
若出现如下错误:
[ERR] Node 127.0.0.1:7001 is not empty. Either the nodealready knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决办法如下:
1)、将需要新增的节点下aof、rdb等本地备份文件删除;

2)、同时将新Node的集群配置文件删除,即:删除你redis.conf里面cluster-config-file所在的文件;

3)、再次添加新节点如果还是报错,则登录新Node,./redis-cli–h x –p对数据库进行清除:

127.0.0.1:7001> flushdb #清空当前数据库
若出现[ERR] Not all 16384 slots are covered by nodes.的错误
解决办法如下:
redis-trib.rb fix 127.0.0.1:7001(3.0版本)
redis-trib.rb check 127.0.0.1:7000(3.0版本)
redis-cli --cluster fix 127.0.0.1:7001(5.0版本)
redis-cli --cluster check 127.0.0.1:7001(5.0版本)
只要输入任意集群中节点即可,会自动检查所有相关节点。可以查看相应的输出看下是否是每个Master都有了slots,如果分布不均匀那可以使用下面的方式重新分配。

多节点集群搭建
一、环境搭建
同一局域网下创建两台虚拟机或者其他机器在局域网下。

第二步:修改配置,创建节点

首先我们在192.168.0.5虚拟机里创建三个节点,端口分别是7001,7002,7003

我们先在root目录下新建一个redis_cluster目录,然后该目录下再创建3个目录,

分别是7001,7002,7003,用来存redis配置文件;

这里我们要使用redis集群,要先修改redis的配置文件redis.conf

mkdir redis_cluster 新建目录

[root@localhost ~]# cd redis_cluster/

[root@localhost redis_cluster]# mkdir 7001 7002 7003

先复制一份配置文件到7001目录下

[root@localhost redis_cluster]# cd

[root@localhost ~]# cp redis-5.5.0/redis.conf redis_cluster/7001/

我们修改下这个配置文件

vi redis_cluster/7001/redis.conf

修改一下几个

port 7001 //六个节点配置文件分别是7001-7003

bind 192.168.0.5 //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访,和单机集群有区别

daemonize yes //redis后台运行

pidfile /var/run/redis_7001.pid //pidfile文件对应7001-7003

cluster-enabled yes //开启集群

cluster-config-file nodes_7001.conf //保存节点配置,自动创建,自动更新对应7001-7003

cluster-node-timeout 5000 //集群超时时间,节点超过这个时间没反应就断定是宕机

appendonly yes //存储方式,aof,将写操作记录保存到日志中

7001下的修改完后,我们把7001下的配置分别复制到7002-7003 然后对应的再修改下配置即可;

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/

[root@localhost ~]# vi redis_cluster/7002/redis.conf 

[root@localhost ~]# vi redis_cluster/7003/redis.conf 

[root@localhost ~]# vi redis_cluster/7004/redis.conf 

编辑后面5个配置文件,把 port ,pidfile,cluster-config-file 分别修改下即可;
同理 192.168.0.6机器,也搞一个redis_cluster目录,然后再新建7004,7005,7006目录,
第三步:启动两台机器的六个节点
启动之前先检查appendonly.aof和dump.rdp以及node-7001-7006的conf文件是否删除,避免出现问题
192.168.0.5机器

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[root@localhost ~]# ps -ef | grep redis  

root       2242      1  0 19:55 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7001 [cluster]

root       2252      1  0 19:59 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7002 [cluster]

root       2256      1  0 19:59 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.5:7003 [cluster]

root       2260   2214  0 19:59 pts/0    00:00:00 grep --color=auto redis

192.168.0.6机器

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 

[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 

[root@localhost ~]# ps -ef | grep redis  

root       2347      1  0 20:31 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7004 [cluster]

root       2351      1  0 20:31 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7005 [cluster]

root       2355      1  0 20:31 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.6:7006 [cluster]

root       2363   2270  0 20:32 pts/0    00:00:00 grep --color=auto redis

说明都启动OK
第四步:设置防火墙,开放集群端口

两台机器的防火墙我们直接关掉

systemctl stop firewalld.service

第五步:创建集群

192.168.0.5机器作为集群控制端

redis官方提供了redis-trib.rb工具,第一步里已经房到里bin下 ;

但是在使用之前 需要安装ruby,以及redis和ruby连接

yum -y install ruby ruby-devel rubygems rpm-build

gem install redis

开始在05机器创建

redis-cli --cluster create  192.168.0.5:7001 192.168.0.5:7002 192.168.0.5:7003 192.168.0.6:7004 192.168.0.6:7005 192.168.0.6:7006 --cluster-replicas 1

在这里插入图片描述
从运行结果看 主节点就是7001 7004 7002 从节点分别是7005 7003 7006

7001分配到的哈希槽是 0-5460

7004分配到的哈希槽是 5461-10922

7002分配到的哈希槽是 10923-16383

最后问我们是否接受上面的设置,输入yes 就表示接受,我们输入yes
然后显示:
在这里插入图片描述显示配置哈希槽,以及集群创建成功,可以用了;

第六步:集群数据测试

我们先连接任意一个节点,然后添加一个key:

redis-cli是redis默认的客户端工具,启动时加上`-c`参数,-p指定端口,就可以连接到集群。

这里还得加-h 指定机器IP

连接任意一个节点端口:

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.0.5 -c -p 7002

192.168.0.5:7002>

连接到7002节点

192.168.1.5:7002> set xxx 'fadfa'

-> Redirected to slot [4038] located at 192.168.0.5:7001

OK

前面说过Redis Cluster值分配规则,所以分配key的时候,它会使用CRC16(‘my_name’)%16384算法,来计算,将这个key 放到哪个节点,这里分配到了4038slot 就分配到了7001(0-5460)这个节点上。所以有:

Redirected to slot [4038] located at 192.168.0.5:7001

我们从其他集群节点 ,都可以获取到数据

[root@localhost ~]# /usr/local/redis/bin/redis-cli -h 192.168.0.6 -c -p 7005

192.168.0.6:7005>

192.168.0.6:7005> get xxx

-> Redirected to slot [4038] located at 192.168.0.5:7001

“fadfa”

192.168.0.5:7001>

发布了8 篇原创文章 · 获赞 1 · 访问量 4740

猜你喜欢

转载自blog.csdn.net/kiritolr/article/details/104081970
今日推荐