小编看到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集群环境的服务器操作系统版本如下图所示:
从redis官网下载(http://download.redis.io/releases/redis-5.0.5.tar.gz)最新版本的redis的源码包redis-5.0.5.tar.gz。
安装redis
创建目录/home/test/redis,上传redis包并解压缩
[root@test local]#mkdir -p /home/test/redis
将redis-5.0.5.tar.gz上传到服务器的/home/test/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]# ls
7000 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,7001,7002
port 7000
#默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群
bind 172.30.14.240
#redis后台运行
daemonize yes
#pidfile文件对应7000,7001,7002…
pidfile /var/run/redis_7000.pid
#开启集群,把注释#去掉
cluster-enabled yes
#集群的配置,配置文件首次启动自动生成 7000,7001,7002
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 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
41846:C 24 Oct 10:51:47.070 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=41846, just started
41846:C 24 Oct 10:51:47.070 # Configuration loaded
检查服务
[root@test redis-cluster]# ps -ef | grep redis
root 41396 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7000 [cluster]
root 41601 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7001 [cluster]
root 41645 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7002 [cluster]
root 41779 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7003 [cluster]
root 41818 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7004 [cluster]
root 41826 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7005 [cluster]
root 41834 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7006 [cluster]
root 41839 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7007 [cluster]
root 41847 1 0 10:51 ? 00:00:00 /home/test/redis/redis-5.0.5/src/redis-server 172.30.14.240:7008 [cluster]
监听redis端口
$ netstat -tnlp | grep redis //监听redis端口
[root@test redis-cluster]# netstat -tnlp | grep redis
tcp 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
创建集群
注意:在任意一台上运行 不要在每台机器上都运行,一台就够了
/home/test/redis/redis-5.0.5/src/redis-cli --cluster create 172.30.14.240:7000 172.30.14.240:7001 172.30.14.240:7002 172.30.14.240:7003 172.30.14.240:7004 172.30.14.240:7005 172.30.14.240:7006 172.30.14.240:7007 172.30.14.240:7008 --cluster-replicas 1
需要将其中的IP和端口改为实际安装的的IP地址和端口
注意:此步骤中需要手动敲入yes
[root@test redis-cluster]# /home/test/redis/redis-5.0.5/src/redis-cli --cluster create --replicas 1 172.30.14.240:7000 172.30.14.240:7001 172.30.14.240:7002 172.30.14.240:7003 172.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 slots allocation on 9 nodes…
Using 4 masters:
172.30.14.240:7000
172.30.14.240:7001
172.30.14.240:7002
172.30.14.240:7003
Adding replica 172.30.14.240:7004 to 172.30.14.240:7000
Adding replica 172.30.14.240:7005 to 172.30.14.240:7001
Adding replica 172.30.14.240:7006 to 172.30.14.240:7002
Adding replica 172.30.14.240:7007 to 172.30.14.240:7003
Adding replica 172.30.14.240:7008 to 172.30.14.240:7000
M: 5992c09409c106b566f7876935d39df2ced31d63 172.30.14.240:7000
slots:0-4095 (4096 slots) master
M: 8e842f0cf0cd2569effa8170834618a6fceb144c 172.30.14.240:7001
slots:4096-8191 (4096 slots) master
M: d5d363c4c7d0855e245a0121c098918d9a2d9f54 172.30.14.240:7002
slots:8192-12287 (4096 slots) master
M: c6422e581cface110c30d302db796e88dca49d8d 172.30.14.240:7003
slots:12288-16383 (4096 slots) master
S: 008aab9504f9afac5be41aea92d995d4d1f94c0c 172.30.14.240:7004
replicates 5992c09409c106b566f7876935d39df2ced31d63
S: 7304749ee131927a21d48fb7d4cb360dacf3b7ac 172.30.14.240:7005
replicates 8e842f0cf0cd2569effa8170834618a6fceb144c
S: bf809bd10b59b16daa4169f950891c0839bfa649 172.30.14.240:7006
replicates d5d363c4c7d0855e245a0121c098918d9a2d9f54
S: 0e93a0a1c627cc84572798b78142f9e73407ec72 172.30.14.240:7007
replicates c6422e581cface110c30d302db796e88dca49d8d
S: de666ccd190e9a7b826b558d89ee2cddf75f43e7 172.30.14.240:7008
replicates 5992c09409c106b566f7876935d39df2ced31d63
Can I set the above configuration? (type ‘yes’ to accept): yesNodes 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 172.30.14.240:7000)
M: 5992c09409c106b566f7876935d39df2ced31d63 172.30.14.240:7000
slots:0-4095 (4096 slots) master
2 additional replica(s)
M: c6422e581cface110c30d302db796e88dca49d8d 172.30.14.240:7003
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: de666ccd190e9a7b826b558d89ee2cddf75f43e7 172.30.14.240:7008
slots: (0 slots) slave
replicates 5992c09409c106b566f7876935d39df2ced31d63
S: 7304749ee131927a21d48fb7d4cb360dacf3b7ac 172.30.14.240:7005
slots: (0 slots) slave
replicates 8e842f0cf0cd2569effa8170834618a6fceb144c
S: bf809bd10b59b16daa4169f950891c0839bfa649 172.30.14.240:7006
slots: (0 slots) slave
replicates d5d363c4c7d0855e245a0121c098918d9a2d9f54
M: d5d363c4c7d0855e245a0121c098918d9a2d9f54 172.30.14.240:7002
slots:8192-12287 (4096 slots) master
1 additional replica(s)
M: 8e842f0cf0cd2569effa8170834618a6fceb144c 172.30.14.240:7001
slots:4096-8191 (4096 slots) master
1 additional replica(s)
S: 0e93a0a1c627cc84572798b78142f9e73407ec72 172.30.14.240:7007
slots: (0 slots) slave
replicates c6422e581cface110c30d302db796e88dca49d8d
S: 008aab9504f9afac5be41aea92d995d4d1f94c0c 172.30.14.240:7004
slots: (0 slots) slave
replicates 5992c09409c106b566f7876935d39df2ced31d63
[OK] All nodes agree about slots configuration.Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.
关闭集群
批量关闭
pkill redis
逐个关闭
/home/test/redis/redis-5.0.5/src/redis-cli -c -h 172.30.14.240 -p 7000 shutdown
/home/test/redis/redis-5.0.5/src/redis-cli -c -h 172.30.14.240 -p 7000 shutdown
/home/test/redis/redis-5.0.5/src/redis-cli -c -h 172.30.14.240 -p 7000 shutdown
/home/test/redis/redis-5.0.5/src/redis-cli -c -h 172.30.14.240 -p 7000 shutdown
/home/test/redis/redis-5.0.5/src/redis-cli -c -h 172.30.14.240 -p 7000 shutdown
/home/test/redis/redis-5.0.5/src/redis-cli -c -h 172.30.14.240 -p 7000 shutdown
集群验证
连接集群测试
参数 -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 7000
172.30.14.240:7000> set name test
-> Redirected to slot [5798] located at 172.30.14.240:7001
OK
172.30.14.240:7001> get name
“test”
172.30.14.240:7001>
发现redis set name 之后重定向到172.30.14.240机器 redis 7001 这个节点
检查集群状态
$ /home/test/redis/redis-5.0.5/src/ redis-cli --cluster check 172.30.14.240:7000
[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: 5992c09409c106b566f7876935d39df2ced31d63 172.30.14.240:7000
slots:0-4095 (4096 slots) master
2 additional replica(s)
M: c6422e581cface110c30d302db796e88dca49d8d 172.30.14.240:7003
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: de666ccd190e9a7b826b558d89ee2cddf75f43e7 172.30.14.240:7008
slots: (0 slots) slave
replicates 5992c09409c106b566f7876935d39df2ced31d63
S: 7304749ee131927a21d48fb7d4cb360dacf3b7ac 172.30.14.240:7005
slots: (0 slots) slave
replicates 8e842f0cf0cd2569effa8170834618a6fceb144c
S: bf809bd10b59b16daa4169f950891c0839bfa649 172.30.14.240:7006
slots: (0 slots) slave
replicates d5d363c4c7d0855e245a0121c098918d9a2d9f54
M: d5d363c4c7d0855e245a0121c098918d9a2d9f54 172.30.14.240:7002
slots:8192-12287 (4096 slots) master
1 additional replica(s)
M: 8e842f0cf0cd2569effa8170834618a6fceb144c 172.30.14.240:7001
slots:4096-8191 (4096 slots) master
1 additional replica(s)
S: 0e93a0a1c627cc84572798b78142f9e73407ec72 172.30.14.240:7007
slots: (0 slots) slave
replicates c6422e581cface110c30d302db796e88dca49d8d
S: 008aab9504f9afac5be41aea92d995d4d1f94c0c 172.30.14.240:7004
slots: (0 slots) slave
replicates 5992c09409c106b566f7876935d39df2ced31d63
[OK] All nodes agree about slots configuration.Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.
列出集群节点
列出集群当前已知的所有节点(node),以及这些节点的相关信息
[root@test redis-cluster]# /home/test/redis/redis-5.0.5/src/redis-cli -h 172.30.14.240 -c -p 7000
172.30.14.240:7000> cluster nodes
c6422e581cface110c30d302db796e88dca49d8d 172.30.14.240:7003@17003 master - 0 1508814078128 4 connected 12288-16383
de666ccd190e9a7b826b558d89ee2cddf75f43e7 172.30.14.240:7008@17008 slave 5992c09409c106b566f7876935d39df2ced31d63 0 1508814078000 9 connected
7304749ee131927a21d48fb7d4cb360dacf3b7ac 172.30.14.240:7005@17005 slave 8e842f0cf0cd2569effa8170834618a6fceb144c 0 1508814077125 6 connected
bf809bd10b59b16daa4169f950891c0839bfa649 172.30.14.240:7006@17006 slave d5d363c4c7d0855e245a0121c098918d9a2d9f54 0 1508814077000 7 connected
d5d363c4c7d0855e245a0121c098918d9a2d9f54 172.30.14.240:7002@17002 master - 0 1508814079130 3 connected 8192-12287
8e842f0cf0cd2569effa8170834618a6fceb144c 172.30.14.240:7001@17001 master - 0 1508814078128 2 connected 4096-8191
0e93a0a1c627cc84572798b78142f9e73407ec72 172.30.14.240:7007@17007 slave c6422e581cface110c30d302db796e88dca49d8d 0 1508814080131 8 connected
008aab9504f9afac5be41aea92d995d4d1f94c0c 172.30.14.240:7004@17004 slave 5992c09409c106b566f7876935d39df2ced31d63 0 1508814076000 5 connected
5992c09409c106b566f7876935d39df2ced31d63 172.30.14.240:7000@17000 myself,master - 0 1508814077000 1 connected 0-4095
打印集群信息
172.30.14.240:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:4
cluster_current_epoch:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:523
cluster_stats_messages_pong_sent:546
cluster_stats_messages_sent:1069
cluster_stats_messages_ping_received:538
cluster_stats_messages_pong_received:523
cluster_stats_messages_meet_received:8
cluster_stats_messages_received:1069
集群命令
语法格式
redis-cli -c -p port
集群
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息。
节点
cluster meet :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
cluster forget
cluster replicate
cluster saveconfig :将节点的配置文件保存到硬盘里面。
槽(slot)
cluster addslots [slot …] :将一个或多个槽( slot)指派( assign)给当前节点。
cluster delslots [slot …] :移除一个或多个槽对当前节点的指派。
cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
cluster setslot node
cluster setslot migrating
cluster setslot importing
cluster setslot stable :取消对槽 slot 的导入( import)或者迁移( migrate)。
键
cluster keyslot :计算键 key 应该被放置在哪个槽上。
cluster countkeysinslot :返回槽 slot 目前包含的键值对数量。
cluster getkeysinslot :返回 count 个 slot 槽中的键
以上小编为大家介绍了redis 5.0.5版本的集群环境搭建,比redis 4.*系列版本简化了Ruby的安装过程。在下面一篇文章中,小编将为大家介绍代码中如何继承redis实现数据缓存服务,感兴趣的同学可以关注我们的微信公众号。