【网络技术联盟站】centos7.6环境下redis5.0.8集群搭建文档
【摘要】
一、做基础配置
a) 首先创建3个空文件
mkdir -p /server/redis_cluster/7001/data
mkdir -p /server/redis_cluster/700...
一、做基础配置
a) 首先创建3个空文件
mkdir -p /server/redis_cluster/7001/data
mkdir -p /server/redis_cluster/7002/data
mkdir -p /server/redis_cluster/7003/data
- 1
- 2
- 3
- 4
b) 把redis.conf拷贝到/server/redis_cluster/7001
cp /server/redis-5.0.8/redis.conf /server/redis_cluster/7001
- 1
- 2
c) 修改redis.conf(不同节点配置文件端口好不同)
#设置端口号
port 7001
#数据存放位置,必须要指定
dir /server/redis_cluster/7001/data
#启动集群模式
cluster-enabled yes
#集群节点信息文件
cluster-config-file nodes-7001.conf
cluster-node-timeout 5000
#去掉bind绑定访问ip信息
bind 192.168.40.10
#关闭保护模式
protected-mode no
#开启AOF模式,默认的持久化上RDB
appendonly yes
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
d) 实例
port 7001
#端口7001,7002,7003
bind 本机ip
#默认ip为192.168.40.10需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes
#redis后台运行
pidfile /var/run/redis_7001.pid
#pidfile文件对应7001,7002,7003
cluster-enabled yes
#开启集群 把注释#去掉
cluster-config-file nodes_7000.conf
#集群的配置 配置文件首次启动自动生成 7001,7002,7003
cluster-node-timeout 15000
#请求超时 默认15秒,可自行设置
appendonly yes
#aof日志开启 有需要就开启,它会每次写操作都记录一条日志
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
e) 替换redis.conf里的端口号
vim /server/redis_cluster/7002/redis.conf
:%s/7001/7002/g
vim /server/redis_cluster/7003/redis.conf
:%s/7001/7003/g
- 1
- 2
- 3
- 4
- 5
二、 首先创建3个空文件
mkdir -p /server/redis_cluster/7004/data
mkdir -p /server/redis_cluster/7005/data
mkdir -p /server/redis_cluster/7006/data
- 1
- 2
- 3
- 4
三、 把redis.conf拷贝到/server/redis_cluster/7004
cp /server/redis-5.0.8/redis.conf /server/redis_cluster/7004
- 1
- 2
a) 编辑7004/redis.conf
#//设置端口号
port 7004
#数据存放位置,必须要指定
dir /server/redis_cluster/7004/data
#启动集群模式
cluster-enabled yes
#集群节点信息文件
cluster-config-file nodes-7004.conf
cluster-node-timeout 5000
#去掉bind绑定访问ip信息
bind 192.168.40.11
#关闭保护模式
protected-mode no
#开启AOF模式,默认的持久化上RDB
appendonly yes
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
b) 拷贝7004节点的conf文件到其他节点并批量替换节点端口号
vim /server/redis_cluster/7005/redis.conf
:%s/7004/7005/g
vim /server/redis_cluster/7006/redis.conf
:%s/7004/7006/g
- 1
- 2
- 3
- 4
- 5
四、 启动redis服务
redis-server /server/redis_cluster/7001/redis.conf
redis-server /server/redis_cluster/7002/redis.conf
redis-server /server/redis_cluster/7003/redis.conf`
redis-server /server/redis_cluster/7004/redis.conf
redis-server /server/redis_cluster/7005/redis.conf
redis-server /server/redis_cluster/7006/redis.conf
- 1
- 2
- 3
- 4
- 5
- 6
- 7
五、 查看是否有进程
ps -ef | grep redis
- 1
- 2
六、 创建集群
a) 命令格式
redis-cli -a *** --cluster create 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 --cluster-replicas 1
- 1
- 2
-a *** 密码 如果redis.conf中配置了密码创建集群时需要添加
--cluster-replicas 1 从节点个数
- 1
- 2
- 3
b) 使用create-cluster搭建
只创建主节点(不包含slave节点)
redis-cli --cluster create 192.168.40.10:7001 192.168.40.10:7002 192.168.40.10:7003 --cluster-replicas 0
- 1
- 2
c) 查看192.168.40.10:7001节点ID
redis-cli --cluster check 192.168.40.10:7001
- 1
- 2
192.168.40.10:7001 (603f34b6...) -> 0 keys | 5461 slots | 0 slaves.
192.168.40.10:7003 (c28c02bc...) -> 0 keys | 5461 slots | 0 slaves.
192.168.40.10:7002 (9103ce33...) -> 0 keys | 5462 slots | 0 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.40.10:7001)
M: 603f34b6f1bfa681f1e339bda355f225125488b4 192.168.40.10:7001
slots:[0-5460] (5461 slots) master
M: c28c02bca8611298a01717b0591cadec04c77aad 192.168.40.10:7003
slots:[10923-16383] (5461 slots) master
M: 9103ce333b822ac9e0cac4f1d499e72e149197d3 192.168.40.10:7002
slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
d) 手动添加从节点
redis-cli --cluster add-node 192.168.40.11:7004 192.168.40.10:7001 --cluster-slave --cluster-master-id 603f34b6f1bfa681f1e339bda355f225125488b4
- 1
- 2
redis-cli --cluster add-node 192.168.40.11:7005 192.168.40.10:7002 --cluster-slave --cluster-master-id 9103ce333b822ac9e0cac4f1d499e72e149197d3
- 1
- 2
redis-cli --cluster add-node 192.168.40.11:7006 192.168.40.10:7003 --cluster-slave --cluster-master-id c28c02bca8611298a01717b0591cadec04c77aad
- 1
- 2
七、 验证集群
a) 登录集群节点
./redis-cli -h 192.168.40.10 -p 7001
- 1
- 2
b) 查看集群状态
192.168.40.10:7001> cluster info
- 1
- 2
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:3
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1478
cluster_stats_messages_pong_sent:1382
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:2861
cluster_stats_messages_ping_received:1378
cluster_stats_messages_pong_received:1479
cluster_stats_messages_meet_received:4
cluster_stats_messages_received:2861
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
c) 查看集群主从关系(根据slave后面的id确定当前节点的主节点是哪个)
192.168.40.10:7001> cluster nodes
- 1
- 2
c28c02bca8611298a01717b0591cadec04c77aad 192.168.40.10:7003[@17003] master - 0 1598951187016 3 connected 10923-16383
6523aa8a3bf53eae1ef8df25c0f42c820e0af7d6 192.168.40.11:7004@17004 slave 603f34b6f1bfa681f1e339bda355f225125488b4 0 1598951188022 1 connected
32883231bf8908549f1720ab99161738865ec009 192.168.40.11:7005@17005 slave 9103ce333b822ac9e0cac4f1d499e72e149197d3 0 1598951186010 2 connected
d679eba5a795cdd1c6b4722bb5fb33cad60e0c18 192.168.40.11:7006@17006 slave c28c02bca8611298a01717b0591cadec04c77aad 0 1598951187000 3 connected
603f34b6f1bfa681f1e339bda355f225125488b4 192.168.40.10:7001@17001 myself,master - 0 1598951184000 1 connected 0-5460
9103ce333b822ac9e0cac4f1d499e72e149197d3 192.168.40.10:7002@17002 master - 0 1598951185002 2 connected 5461-10922
- 1
- 2
- 3
- 4
- 5
- 6
- 7
文章来源: blog.csdn.net,作者:wljslmz,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_43025343/article/details/109245898
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)