Redis 5.0.0安装部署(伪集群版)
一. 安装单机版redis
二、创建集群节点
1. 创建文件夹
我们计划集群中 Redis 节点的端口号为9001-9006 ,端口号即集群下各redis实例文件夹。数据存放在端口号/data文件夹中。
[root@smsp-dev001-10 redis]# mkdir /usr/local/redis-cluster
[root@smsp-dev001-10 redis]# cd /usr/local/redis-cluster/
[root@smsp-dev001-10 redis-cluster]# mkdir -p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/data
2.复制执行脚本
把安装好的单机版redis bin目录下的执行脚本复制到redis集群文件夹下
[root@smsp-dev001-10 redis-cluster]# mkdir bin
[root@smsp-dev001-10 redis-cluster]# cp -r /usr/local/redis/bin/* bin/
3. 复制配置文件
把解压目录下的redis配置文件redis.conf复制一份到各端口实例文件夹中
[root@smsp-dev001-10 9001]# cp /software/redis-5.0.0/redis.conf /usr/local/redis-cluster/9001/
[root@smsp-dev001-10 9001]# cp /software/redis-5.0.0/redis.conf /usr/local/redis-cluster/9002/
[root@smsp-dev001-10 9001]# cp /software/redis-5.0.0/redis.conf /usr/local/redis-cluster/9003/
[root@smsp-dev001-10 9001]# cp /software/redis-5.0.0/redis.conf /usr/local/redis-cluster/9004/
[root@smsp-dev001-10 9001]# cp /software/redis-5.0.0/redis.conf /usr/local/redis-cluster/9005/
[root@smsp-dev001-10 9001]# cp /software/redis-5.0.0/redis.conf /usr/local/redis-cluster/9006/
4. 修改各端口实例中的配置文件选项
port 9001(指定redis节点绑定的端口号)
daemonize yes(配置后台运行)
# bind 127.0.0.1(指定绑定当前机器IP)
dir /usr/local/redis-cluster/9001/data/(指定数据文件存放位置)
pidfile /var/run/redis_9001.pid(指定pid文件存放位置)
cluster-enabled yes(配置集群模式开启)
cluster-config-file nodes-9001.conf(指定集群节点信息文件,这里900x最好和port对应上)
cluster-node-timeout 15000(指定集群节点响应超时时间,处理网络抖动)
appendonly yes(指定数据持久化方式:AOF)
注:其实可以先修改一个端口实例下的配置文件,完了再复制到其他端口实例文件夹中,通过全局替换成相应端口即可。
比如在vi命令模式下进行如下替换即可:
:%s/9001/9002/g
5. 启动 9001-9006 六个实例节点
[root@smsp-dev001-10 redis-cluster]# cd bin/
[root@smsp-dev001-10 bin]# ./redis-server /usr/local/redis-cluster/9001/redis.conf
[root@smsp-dev001-10 bin]# ./redis-server /usr/local/redis-cluster/9002/redis.conf
[root@smsp-dev001-10 bin]# ./redis-server /usr/local/redis-cluster/9003/redis.conf
[root@smsp-dev001-10 bin]# ./redis-server /usr/local/redis-cluster/9004/redis.conf
[root@smsp-dev001-10 bin]# ./redis-server /usr/local/redis-cluster/9005/redis.conf
[root@smsp-dev001-10 bin]# ./redis-server /usr/local/redis-cluster/9006/redis.conf
6. 查看启动服务
[root@smsp-dev001-10 bin]# ps -ef |grep redis
root 57331 1 0 13:43 ? 00:00:04 ./redis-server 127.0.0.1:6379
root 59539 1 0 14:17 ? 00:00:00 ./redis-server 127.0.0.1:9001 [cluster]
root 59553 1 0 14:17 ? 00:00:00 ./redis-server 127.0.0.1:9002 [cluster]
root 59563 1 0 14:17 ? 00:00:00 ./redis-server 127.0.0.1:9003 [cluster]
root 59573 1 0 14:17 ? 00:00:00 ./redis-server 127.0.0.1:9004 [cluster]
root 59592 1 0 14:17 ? 00:00:00 ./redis-server 127.0.0.1:9005 [cluster]
root 59602 1 0 14:18 ? 00:00:00 ./redis-server 127.0.0.1:9006 [cluster]
三、根据节点创建集群
Redis 5.0开始不再使用ruby搭建集群,而是直接使用客户端命令 redis-cli 来创建。
如果用ruby命令在>5.0.0版本上创建会报如下提示信息:
root@rocketmq-nameserver1 bin]# /usr/local/redis-cluster/bin/redis-trib.rb create --replicas 1 192.168.229.128:9001 192.168.229.128:9002 192.168.229.128:9003 192.168.229.128:9004 192.168.229.128:9005 192.168.229.128:9006
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 192.168.229.128:9001 192.168.229.128:9002 192.168.229.128:9003 192.168.229.128:9004 192.168.229.128:9005 192.168.229.128:9006 --cluster-replicas 1
To get help about all subcommands, type:
redis-cli --cluster help
利用如下客户端命令创建集群:
[root@smsp-dev001-10 bin]# ./redis-cli --cluster create 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9004 127.0.0.1:9005 127.0.0.1:9006 --cluster-replicas 1
创建过程如下:
[root@smsp-dev001-10 bin]# ./redis-cli --cluster create 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9004 127.0.0.1:9005 127.0.0.1:9006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:9004 to 127.0.0.1:9001
Adding replica 127.0.0.1:9005 to 127.0.0.1:9002
Adding replica 127.0.0.1:9006 to 127.0.0.1:9003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: f73a3938f149193f50391b9c19da2df0390ffbd8 127.0.0.1:9001
slots:[0-5460] (5461 slots) master
M: 952ac3f781a738bafa0a7e767a37a7d1aba6cf25 127.0.0.1:9002
slots:[5461-10922] (5462 slots) master
M: af642454e33a93e7c4fb230559a6be40a041daeb 127.0.0.1:9003
slots:[10923-16383] (5461 slots) master
S: 57df2a088b740f6be87c3dc886f0a96f19f656c3 127.0.0.1:9004
replicates af642454e33a93e7c4fb230559a6be40a041daeb
S: d11382cc26d1539db58c8033a0f76bd1e16b1678 127.0.0.1:9005
replicates f73a3938f149193f50391b9c19da2df0390ffbd8
S: 70759209d3b64435f8eafaf41cd8c6b24ae88546 127.0.0.1:9006
replicates 952ac3f781a738bafa0a7e767a37a7d1aba6cf25
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 127.0.0.1:9001)
M: f73a3938f149193f50391b9c19da2df0390ffbd8 127.0.0.1:9001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: af642454e33a93e7c4fb230559a6be40a041daeb 127.0.0.1:9003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: d11382cc26d1539db58c8033a0f76bd1e16b1678 127.0.0.1:9005
slots: (0 slots) slave
replicates f73a3938f149193f50391b9c19da2df0390ffbd8
S: 70759209d3b64435f8eafaf41cd8c6b24ae88546 127.0.0.1:9006
slots: (0 slots) slave
replicates 952ac3f781a738bafa0a7e767a37a7d1aba6cf25
M: 952ac3f781a738bafa0a7e767a37a7d1aba6cf25 127.0.0.1:9002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 57df2a088b740f6be87c3dc886f0a96f19f656c3 127.0.0.1:9004
slots: (0 slots) slave
replicates af642454e33a93e7c4fb230559a6be40a041daeb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
四、连接测试
[root@smsp-dev001-10 bin]# ./redis-cli -c -h 127.0.0.1 -p 9001
127.0.0.1:9001> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:318
cluster_stats_messages_pong_sent:288
cluster_stats_messages_sent:606
cluster_stats_messages_ping_received:283
cluster_stats_messages_pong_received:318
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:606
127.0.0.1:9001> set name Tom
-> Redirected to slot [5798] located at 127.0.0.1:9002
OK
127.0.0.1:9002> get name
"Tom"
127.0.0.1:9002>
[root@smsp-dev001-10 bin]# ./redis-cli -c -h 127.0.0.1 -p 9005
127.0.0.1:9005> get name
-> Redirected to slot [5798] located at 127.0.0.1:9002
"Tom"
127.0.0.1:9002> cluster nodes
70759209d3b64435f8eafaf41cd8c6b24ae88546 127.0.0.1:9006@19006 slave 952ac3f781a738bafa0a7e767a37a7d1aba6cf25 0 1573280954543 6 connected
af642454e33a93e7c4fb230559a6be40a041daeb 127.0.0.1:9003@19003 master - 0 1573280952535 3 connected 10923-16383
57df2a088b740f6be87c3dc886f0a96f19f656c3 127.0.0.1:9004@19004 slave af642454e33a93e7c4fb230559a6be40a041daeb 0 1573280953000 4 connected
d11382cc26d1539db58c8033a0f76bd1e16b1678 127.0.0.1:9005@19005 slave f73a3938f149193f50391b9c19da2df0390ffbd8 0 1573280951000 5 connected
952ac3f781a738bafa0a7e767a37a7d1aba6cf25 127.0.0.1:9002@19002 myself,master - 0 1573280951000 2 connected 5461-10922
f73a3938f149193f50391b9c19da2df0390ffbd8 127.0.0.1:9001@19001 master - 0 1573280953539 1 connected 0-5460
五、查看各节点data目录
[root@smsp-dev001-10 ~]# cd /usr/local/redis-cluster/
[root@smsp-dev001-10 redis-cluster]# ls
9001 9002 9003 9004 9005 9006 bin
[root@smsp-dev001-10 redis-cluster]# cd 9002/
[root@smsp-dev001-10 9002]# ls
data redis.conf
[root@smsp-dev001-10 9002]# cd data/
[root@smsp-dev001-10 data]# ls
appendonly.aof dump.rdb nodes-9002.conf
# 集群启动后会在每一个节点的data目录中自动生成集群节点信息文件(注意:跟配置文件不是一回事!)
# 文件目录dir指定,文件名cluster-config-file指定
[root@smsp-dev001-10 data]# vi nodes-9002.conf
70759209d3b64435f8eafaf41cd8c6b24ae88546 127.0.0.1:9006@19006 slave 952ac3f781a738bafa0a7e767a37a7d1aba6cf25 0 1573280552264 6 connected
af642454e33a93e7c4fb230559a6be40a041daeb 127.0.0.1:9003@19003 master - 0 1573280549255 3 connected 10923-16383
57df2a088b740f6be87c3dc886f0a96f19f656c3 127.0.0.1:9004@19004 slave af642454e33a93e7c4fb230559a6be40a041daeb 0 1573280549000 4 connected
d11382cc26d1539db58c8033a0f76bd1e16b1678 127.0.0.1:9005@19005 slave f73a3938f149193f50391b9c19da2df0390ffbd8 0 1573280551261 5 connected
952ac3f781a738bafa0a7e767a37a7d1aba6cf25 127.0.0.1:9002@19002 myself,master - 0 1573280550000 2 connected 5461-10922
f73a3938f149193f50391b9c19da2df0390ffbd8 127.0.0.1:9001@19001 master - 0 1573280550259 1 connected 0-5460
vars currentEpoch 6 lastVoteEpoch 0
参考
安装高版本ruby:https://blog.csdn.net/gaozongzonggao/article/details/80793864