当前位置:   article > 正文

redis集群配置bind ip_CentOS7下搭建 Redis-5.0.5集群环境

centos7 redis bind

最近刚看到redis的最新版本已经更新到了5.0.5,而之前使用的redis是4.0.1版本,所以小编想把使用的redis版本升级到最新的5.0.5。小编在使用redis 4.0.1版本时,是部署的集群环境,所以升级到redis 5.0.5版本时也安装的是集群环境。在安装完redis 5.0.5版本的集群环境后,小编发现相比于redis 4.*系列版本redis 5.0.5版本的安装简化了一个很大的步骤。安装过redis 4.*系列版本集群服务的同学,都知道在创建集群时需要使用redis-trib.rb命令;而这个命令是用Ruby语言编写,所以要使用这个命令还需要安装Ruby环境。虽然使用RVM可以简化Ruby环境的安装,但是依然还是一个很麻烦的安装过程,而且RVM必须连接互联网,对于封闭的局域网环境就更加麻烦了。redis 5.0.5版本使用redis-cli –cluster来替代redis-trib.rb从而我们可以不需要安装Ruby环境,省略了Ruby环境的安装,redis集群环境的搭建就大大简化了。

由于redis集群从设计之初就采用去中心化的思想,所以每个集群节点地位都是相等的。而redis的容错机制要求超过半数认为节点故障才真正认为节点故障,所以组成redis集群的节点最少要求3个,而redis集群的节点一般要求一个主节点具备一个从节点,所以一个redis集群最好要有6个节点;在生产环境中一个节点最好使用一台服务器来安装。而小编手上没有这么多的服务器资源,所以下面就一个一台服务器上安装多个节点的伪集群安装来进行介绍,但其安装过程和实际生产环境安装是一样(只是一台服务器是安装一个还是多个节点的区别),下面我们就来介绍下redis集群环境的搭建。

搭建redis集群环境的服务器操作系统版本如下图所示:

cc98f25f77eb4d4b84236a09278bb582.png

安装redis

从redis官网下载(http://download.redis.io/releases/redis-5.0.5.tar.gz)最新版本的redis的源码包redis-5.0.5.tar.gz。

创建目录/home/test/redis

[root@test local]#mkdir -p /home/test/redis

将redis-5.0.5.tar.gz上传到服务器的/home/test/redis目录下,上传redis包并解压缩。

[root@test local]#cd /home/test/redis[root@test local]#tar -xzf redis-5.0.5.tar.gz

编译安装

执行命令make && make install

[root@test redis]# cd redis-5.0.5[root@test redis]# make && make install

创建集群目录

在/home/test/redis目录下创建集群目录

[root@test redis]# mkdir redis-cluster[root@test redis]# cd redis-cluster/

在集群目录下创建节点目录

小编的示例中是在单台服务器安装9个节点,所以下面创建9个目录

命令:

[root@test redis-cluster]# mkdir 7000 7001 7002 7003 7004 7005 7006 7007 7008[root@test redis-cluster]# ls7000 7001 7002 7003 7004 7005 7006 7007 7008 

集群中的节点配置文件修改

把如下redis.conf 配置内容粘贴进去

[root@test local]#vi 7000/redis.conf[root@test local]#vi 7001/redis.conf[root@test local]#vi 7002/redis.conf[root@test local]#vi 7003/redis.conf[root@test local]#vi 7004/redis.conf[root@test local]#vi 7005/redis.conf[root@test local]#vi 7006/redis.conf[root@test local]#vi 7007/redis.conf[root@test local]#vi 7008/redis.conf

redis.conf 配置(其中7000及bind的ip需要修改为实际的节点的对应配置):

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

集群中的节点启动

集群中节点的启动命令如下:

/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7000/redis.conf/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7001/redis.conf/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7002/redis.conf/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7003/redis.conf/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7004/redis.conf/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7005/redis.conf/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7006/redis.conf/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7007/redis.conf/home/test/redis/redis-5.0.5/src/redis-server /home/test/redis/redis-cluster/7008/redis.conf

出现以下信息,表示启动成功:

41846:C 24 Oct 10:51:47.070 # oO0OoO0OoO0OoRedis is starting oO0OoO0OoO0Oo41846:C 24 Oct 10:51:47.070 # Redisversion=5.0.5, bits=64, commit=00000000, modified=0, pid=41846, just started41846:C 24 Oct 10:51:47.070 # Configuration loaded

检查服务

检查服务命令:ps -ef | grep redis

 [root@test redis-cluster]# ps -ef | grep redisroot 41396 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7000 [cluster]root 41601 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7001 [cluster]root 41645 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7002 [cluster]root 41779 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7003 [cluster]root 41818 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7004 [cluster]root 41826 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7005 [cluster]root 41834 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7006 [cluster]root 41839 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7007 [cluster]root 41847 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server172.30.14.240:7008 [cluster]

监听redis端口命令: netstat -tnlp | grep redis

[root@test redis-cluster]# netstat -tnlp |grep redistcp 0 0 172.30.14.240:17000 0.0.0.0:* LISTEN 41396/redis-server tcp 0 0 172.30.14.240:17001 0.0.0.0:* LISTEN 41601/redis-server tcp 0 0 172.30.14.240:17002 0.0.0.0:* LISTEN 41645/redis-server tcp 0 0 172.30.14.240:17003 0.0.0.0:* LISTEN 41779/redis-server tcp 0 0 172.30.14.240:17004 0.0.0.0:* LISTEN 41818/redis-server tcp 0 0 172.30.14.240:17005 0.0.0.0:* LISTEN 41826/redis-server tcp 0 0 172.30.14.240:17006 0.0.0.0:* LISTEN 41834/redis-server tcp 0 0 172.30.14.240:17007 0.0.0.0:* LISTEN 41839/redis-server tcp 0 0 172.30.14.240:17008 0.0.0.0:* LISTEN 41847/redis-server tcp 0 0 172.30.14.240:7000 0.0.0.0:* LISTEN 41396/redis-server tcp 0 0 172.30.14.240:7001 0.0.0.0:* LISTEN 41601/redis-server tcp 0 0 172.30.14.240:7002 0.0.0.0:* LISTEN 41645/redis-server tcp 0 0 172.30.14.240:7003 0.0.0.0:* LISTEN 41779/redis-server tcp 0 0 172.30.14.240:7004 0.0.0.0:* LISTEN 41818/redis-server tcp 0 0 172.30.14.240:7005 0.0.0.0:* LISTEN 41826/redis-server tcp 0 0 172.30.14.240:7006 0.0.0.0:* LISTEN 41834/redis-server tcp 0 0 172.30.14.240:7007 0.0.0.0:* LISTEN 41839/redis-server tcp 0 0 172.30.14.240:7008 0.0.0.0:* LISTEN 41847/redis-server 

创建集群

注意:在任意一台上运行 不要在每台机器上都运行,一台就够了

redis 5.0.5中使用redis-cli --cluster替代redis-trib.rb,命令如下:

redis-cli --cluster create ip:port ip:port --cluster-replicas 1

需要将其中的IP和端口改为实际安装的的IP地址和端口

注意:此步骤中需要手动敲入yes

[root@test redis-cluster]# /home/test/redis/redis-5.0.5/src/redis-trib.rbcreate --replicas 1 172.30.14.240:7000 172.30.14.240:7001 172.30.14.240:7002 172.30.14.240:7003172.30.14.240:7004 172.30.14.240:7005 172.30.14.240:7006 172.30.14.240:7007 172.30.14.240:7008>>> Creating cluster>>> Performing hash slotsallocation on 9 nodes...Using 4 masters:172.30.14.240:7000172.30.14.240:7001172.30.14.240:7002172.30.14.240:7003Adding replica 172.30.14.240:7004 to 172.30.14.240:7000Adding replica 172.30.14.240:7005 to 172.30.14.240:7001Adding replica 172.30.14.240:7006 to 172.30.14.240:7002Adding replica 172.30.14.240:7007 to 172.30.14.240:7003Adding replica 172.30.14.240:7008 to 172.30.14.240:7000M: 5992c09409c106b566f7876935d39df2ced31d63172.30.14.240:7000 slots:0-4095 (4096 slots) masterM: 8e842f0cf0cd2569effa8170834618a6fceb144c172.30.14.240:7001 slots:4096-8191 (4096 slots) masterM: d5d363c4c7d0855e245a0121c098918d9a2d9f54172.30.14.240:7002 slots:8192-12287 (4096 slots) masterM: c6422e581cface110c30d302db796e88dca49d8d172.30.14.240:7003 slots:12288-16383 (4096 slots) masterS: 008aab9504f9afac5be41aea92d995d4d1f94c0c172.30.14.240:7004 replicates 5992c09409c106b566f7876935d39df2ced31d63S: 7304749ee131927a21d48fb7d4cb360dacf3b7ac172.30.14.240:7005 replicates 8e842f0cf0cd2569effa8170834618a6fceb144cS: bf809bd10b59b16daa4169f950891c0839bfa649172.30.14.240:7006 replicates d5d363c4c7d0855e245a0121c098918d9a2d9f54S: 0e93a0a1c627cc84572798b78142f9e73407ec72172.30.14.240:7007 replicates c6422e581cface110c30d302db796e88dca49d8dS: de666ccd190e9a7b826b558d89ee2cddf75f43e7172.30.14.240:7008 replicates 5992c09409c106b566f7876935d39df2ced31d63Can I set the above configuration? (type'yes' to accept): yes>>> Nodes configuration updated>>> Assign a different configepoch to each node>>> Sending CLUSTER MEET messagesto join the clusterWaiting for the cluster to join.........>>> Performing Cluster Check(using node 172.30.14.240:7000)M: 5992c09409c106b566f7876935d39df2ced31d63172.30.14.240:7000 slots:0-4095 (4096 slots) master 2additional replica(s)M: c6422e581cface110c30d302db796e88dca49d8d172.30.14.240:7003 slots:12288-16383 (4096 slots) master 1additional replica(s)S: de666ccd190e9a7b826b558d89ee2cddf75f43e7172.30.14.240:7008 slots: (0 slots) slave replicates 5992c09409c106b566f7876935d39df2ced31d63S: 7304749ee131927a21d48fb7d4cb360dacf3b7ac172.30.14.240:7005 slots: (0 slots) slave replicates 8e842f0cf0cd2569effa8170834618a6fceb144cS: bf809bd10b59b16daa4169f950891c0839bfa649172.30.14.240:7006 slots: (0 slots) slave replicates d5d363c4c7d0855e245a0121c098918d9a2d9f54M: d5d363c4c7d0855e245a0121c098918d9a2d9f54172.30.14.240:7002 slots:8192-12287 (4096 slots) master 1additional replica(s)M: 8e842f0cf0cd2569effa8170834618a6fceb144c172.30.14.240:7001 slots:4096-8191 (4096 slots) master 1additional replica(s)S: 0e93a0a1c627cc84572798b78142f9e73407ec72172.30.14.240:7007 slots: (0 slots) slave replicates c6422e581cface110c30d302db796e88dca49d8dS: 008aab9504f9afac5be41aea92d995d4d1f94c0c172.30.14.240:7004 slots: (0 slots) slave replicates 5992c09409c106b566f7876935d39df2ced31d63[OK] All nodes agree about slotsconfiguration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered. 

关闭集群

1、批量关闭

pkill redis

2、逐个关闭

 redis-cli -c-h 172.30.14.240 -p 7000 shutdown

集群验证

连接集群测试命令:

redis-cli -h 172.30.14.240 -c -p 7000

参数 -c 可连接到集群,因为 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略,-p 参数为端口号

我们在172.30.14.240机器redis 7000 的节点set 一个key

验证结果如下:

[root@test redis-cluster]#/home/test/redis/redis-5.0.5/src/redis-cli -h 172.30.14.240 -c -p 7000172.30.14.240:7000> set name test-> Redirected to slot [5798] located at 172.30.14.240:7001OK172.30.14.240:7001> get name"test"172.30.14.240:7001>

发现redis在set name *之后重定向到172.30.14.240机器 redis 7001 这个节点。

检查集群状态

检查集群状态命令:

redis-cli--cluster check 172.30.14.240:7000

执行命令大于如下信息,可以看到所有节点的信息,包括节点是主节点master,还是从节点slave:

[root@test redis-cluster]#/home/test/redis/redis-5.0.5/src/redis-cli --cluster check 172.30.14.240:7000>>> Performing Cluster Check(using node 172.30.14.240:7000)M: 5992c09409c106b566f7876935d39df2ced31d63172.30.14.240:7000 slots:0-4095 (4096 slots) master 2additional replica(s)M: c6422e581cface110c30d302db796e88dca49d8d172.30.14.240:7003 slots:12288-16383 (4096 slots) master 1additional replica(s)S: de666ccd190e9a7b826b558d89ee2cddf75f43e7172.30.14.240:7008 slots: (0 slots) slave replicates 5992c09409c106b566f7876935d39df2ced31d63S: 7304749ee131927a21d48fb7d4cb360dacf3b7ac172.30.14.240:7005 slots: (0 slots) slave replicates 8e842f0cf0cd2569effa8170834618a6fceb144cS: bf809bd10b59b16daa4169f950891c0839bfa649172.30.14.240:7006 slots: (0 slots) slave replicates d5d363c4c7d0855e245a0121c098918d9a2d9f54M: d5d363c4c7d0855e245a0121c098918d9a2d9f54172.30.14.240:7002 slots:8192-12287 (4096 slots) master 1additional replica(s)M: 8e842f0cf0cd2569effa8170834618a6fceb144c172.30.14.240:7001 slots:4096-8191 (4096 slots) master 1additional replica(s)S: 0e93a0a1c627cc84572798b78142f9e73407ec72172.30.14.240:7007 slots: (0 slots) slave replicates c6422e581cface110c30d302db796e88dca49d8dS: 008aab9504f9afac5be41aea92d995d4d1f94c0c172.30.14.240:7004 slots: (0 slots) slave replicates 5992c09409c106b566f7876935d39df2ced31d63[OK] All nodes agree about slotsconfiguration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

小结

以上为大家介绍了redis 5.0.5版本的集群环境搭建,比redis 4.*系列版本简化了Ruby的安装过程。在下一篇文章中,将为大家介绍代码中如何继承redis实现数据缓存服务,感兴趣的同学可以关注我们的同名微信公众号“麻辣软硬件”

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/876893
推荐阅读
相关标签
  

闽ICP备14008679号