RedisCluster集群之搭建
一、安装ruby环境
安装redis
二、配置cluster集群
2.1 准备基础配置环境
端口范围7001~7006(共6个,计划master:slave=1:1)
创建6个端口对应的文件夹
# 进入redis根目录
cd /usr/local/project/redis-4.0.8
# 创建cluster-config文件夹,用于存放各个端口对应的信息
mkdir cluster-config
# 进入cluster-config文件夹
cd cluster-config
# 分别创建6个端口的文件夹,可批量创建
mkdir 7001 7002 7003 7004 7005 7006
# 将配置文件拷贝至7001
cp redis.conf cluster-config/7001/
# 先修改7001下的配置文件,修改后再分别复制到其他端口文件夹中
vim cluster-config/7001/redis.conf
redis.conf修改内容如下
# 修改访问ip
bind 127.0.0.1
# 修改访问端口
port 700*
# 以后台方式运行
daemonize yes
# redis实例的pid文件
pidfile="/usr/local/project/redis-4.0.8/cluster-config/700*/redis.pid"
# 日志文件
logfile=/usr/local/project/redis-4.0.8/cluster-config/700*/redis.log
# 数据文件存储位置
dir=/usr/local/project/redis-4.0.8/cluster-config/700*/
# 启动集群
cluster-enabled=yes
# 这里与port对应即可
cluster-config-file nodes-700*.conf
# 超时
cluster-node-timeout=5000
# 开启aof
appendonly=yes
# 设置aof模式为
appendfsync=always
复制配置文件
# 进入7001目录
cd /usr/local/project/redis-4.0.8/cluster-config/7001/
# 复制配置文件至其他端口目录,并分别修改对应的配置文件
cp redis.conf ../7002/
cp redis.conf ../7003/
cp redis.conf ../7004/
cp redis.conf ../7005/
cp redis.conf ../7006/
# 别忘了修改配置文件
小技巧
vim下查找 / ,按N查询下一个 vim下替换 :%s/7001/7002/g ,%是表示全文检索,将7001替换成7002,g表示替换所有
2.2 启动服务
分别启动6个服务
./src/redis-server cluster-config/7001/redis.conf
./src/redis-server cluster-config/7002/redis.conf
./src/redis-server cluster-config/7003/redis.conf
./src/redis-server cluster-config/7004/redis.conf
./src/redis-server cluster-config/7005/redis.conf
./src/redis-server cluster-config/7006/redis.conf
通过ps查看进程状态
ps -ef | grep redis
// 输出如下
root 5407 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7001 [cluster]
root 5415 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7002 [cluster]
root 5423 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7003 [cluster]
root 5431 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7004 [cluster]
root 5439 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7005 [cluster]
root 5447 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7006 [cluster]
2.3 配置集群
利用redis-trib.rb执行cluster集群配置
./src/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 127.0.0.1:7006
提示
>>> 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:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 44c678a77a5181b99f6fe0a5e24a98462d687288 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 0d68dfae22060574a19712f2b6ee2608dbf1b471 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
S: ca9146ea9ce9ef7aa60ac2511f90bd91677285ff 127.0.0.1:7004
replicates 0d68dfae22060574a19712f2b6ee2608dbf1b471
S: bb5f01c7e82348e6c90ce7da209696d02e06546b 127.0.0.1:7005
replicates 44c678a77a5181b99f6fe0a5e24a98462d687288
S: 88a52d61d4c5f5ee00551a546460697bf9f1c475 127.0.0.1:7006
replicates 83e9712ba4520f949119b21a4ebb3de2d66ed2a0
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:7001)
M: 44c678a77a5181b99f6fe0a5e24a98462d687288 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 0d68dfae22060574a19712f2b6ee2608dbf1b471 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: ca9146ea9ce9ef7aa60ac2511f90bd91677285ff 127.0.0.1:7004
slots: (0 slots) slave
replicates 0d68dfae22060574a19712f2b6ee2608dbf1b471
S: bb5f01c7e82348e6c90ce7da209696d02e06546b 127.0.0.1:7005
slots: (0 slots) slave
replicates 44c678a77a5181b99f6fe0a5e24a98462d687288
S: 88a52d61d4c5f5ee00551a546460697bf9f1c475 127.0.0.1:7006
slots: (0 slots) slave
replicates 83e9712ba4520f949119b21a4ebb3de2d66ed2a0
M: 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
最终提示 [OK] All 16384 slots covered.
0~16383 共16384个槽点被包括在内,说明配置集群成功。
温馨提示: 16384个slot是redis内置写死的,固定的,无法修改。
其中我们先分析一下以下内容
M: 44c678a77a5181b99f6fe0a5e24a98462d687288 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 0d68dfae22060574a19712f2b6ee2608dbf1b471 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: ca9146ea9ce9ef7aa60ac2511f90bd91677285ff 127.0.0.1:7004
slots: (0 slots) slave
replicates 0d68dfae22060574a19712f2b6ee2608dbf1b471
S: bb5f01c7e82348e6c90ce7da209696d02e06546b 127.0.0.1:7005
slots: (0 slots) slave
replicates 44c678a77a5181b99f6fe0a5e24a98462d687288
S: 88a52d61d4c5f5ee00551a546460697bf9f1c475 127.0.0.1:7006
slots: (0 slots) slave
replicates 83e9712ba4520f949119b21a4ebb3de2d66ed2a0
M: 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
- 前面的M/S分别表示Master/Slave
- 44c678a77a5181b99f6fe0a5e24a98462d687288 表示当前节点的id
- 127.0.0.1:7001 表示当前节点的ip与端口
- slots:0-5460 (5461 slots) 表示分配了从0到5460共5461个槽点
- 1 additional replica(s) 表示有一个slave节点
- replicates 0d68dfae22060574a19712f2b6ee2608dbf1b471 以此节点为master,自己是slave节点
2.4 验证集群
通过客户端命令查询集群状态(任意节点都可以)
./src/redis-cli -c -h 127.0.0.1 -p 7001
输入 cluster info
127.0.0.1:7001> 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 // 说明有6个已知节点
cluster_size:3 // 有三个集群组
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:846
cluster_stats_messages_pong_sent:860
cluster_stats_messages_sent:1706
cluster_stats_messages_ping_received:855
cluster_stats_messages_pong_received:846
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1706
列出所有节点 cluster nodes
127.0.0.1:7001> cluster nodes
0d68dfae22060574a19712f2b6ee2608dbf1b471 127.0.0.1:7003@17003 master - 0 1537149964380 3 connected 10923-16383
ca9146ea9ce9ef7aa60ac2511f90bd91677285ff 127.0.0.1:7004@17004 slave 0d68dfae22060574a19712f2b6ee2608dbf1b471 0 1537149965086 4 connected
bb5f01c7e82348e6c90ce7da209696d02e06546b 127.0.0.1:7005@17005 slave 44c678a77a5181b99f6fe0a5e24a98462d687288 0 1537149964582 5 connected
88a52d61d4c5f5ee00551a546460697bf9f1c475 127.0.0.1:7006@17006 slave 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 0 1537149965086 6 connected
83e9712ba4520f949119b21a4ebb3de2d66ed2a0 127.0.0.1:7002@17002 master - 0 1537149965388 2 connected 5461-10922
44c678a77a5181b99f6fe0a5e24a98462d687288 127.0.0.1:7001@17001 myself,master - 0 1537149963000 1 connected 0-5460
提示:只有master节点才有槽点信息。
至此,cluster高可用分布式集群搭建完成,下一篇将会介绍如何动态添加与删除集群节点。
博客
开源中国博客地址
https://my.oschina.net/gmarshal
个人博客地址
欢迎关注我的个人微信订阅号:(据说这个头像程序猿专用)