Redis三主三从Cluster集群搭建

测试环境
系统版本:

Distributor ID: CentOS
Description:    CentOS Linux release 7.3.1611 (Core) 
Release:    7.3.1611

redis版本:redis-4.0.10

1、安装redis
2、修改对应的redis节点配置,修改项:

daemonize yes
pidfile /var/run/redis_6379.pid
cluster-config-file nodes-6379.conf
port 6379
cluster-enabled yes

3、启动所有节点

[root@localhost redis-6379]# ./bin/redis-server ect/redis.conf 
5669:C 25 Jun 03:39:23.641 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5669:C 25 Jun 03:39:23.641 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5669, just started
5669:C 25 Jun 03:39:23.641 # Configuration loaded
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6380.conf 
5690:C 25 Jun 03:45:58.093 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5690:C 25 Jun 03:45:58.093 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5690, just started
5690:C 25 Jun 03:45:58.093 # Configuration loaded
[root@localhost redis-6379]# 
[root@localhost redis-6379]# 
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6381.conf 
5695:C 25 Jun 03:46:04.122 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5695:C 25 Jun 03:46:04.122 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5695, just started
5695:C 25 Jun 03:46:04.123 # Configuration loaded
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6382.conf 
5700:C 25 Jun 03:46:07.380 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5700:C 25 Jun 03:46:07.380 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5700, just started
5700:C 25 Jun 03:46:07.380 # Configuration loaded
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6383.conf 
5705:C 25 Jun 03:46:10.661 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5705:C 25 Jun 03:46:10.661 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5705, just started
5705:C 25 Jun 03:46:10.661 # Configuration loaded
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6384.conf 
5710:C 25 Jun 03:46:13.807 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5710:C 25 Jun 03:46:13.808 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5710, just started
5710:C 25 Jun 03:46:13.808 # Configuration loaded

4、查看节点启动情况,日志有无报错,进程是否存在

[root@localhost redis-6379]# ps -ef | grep redis
root       5670      1  0 03:39 ?        00:00:01 ./bin/redis-server 127.0.0.1:6379 [cluster]
root       5691      1  0 03:45 ?        00:00:00 ./bin/redis-server 127.0.0.1:6380 [cluster]
root       5696      1  0 03:46 ?        00:00:00 ./bin/redis-server 127.0.0.1:6381 [cluster]
root       5701      1  0 03:46 ?        00:00:00 ./bin/redis-server 127.0.0.1:6382 [cluster]
root       5706      1  0 03:46 ?        00:00:00 ./bin/redis-server 127.0.0.1:6383 [cluster]
root       5711      1  0 03:46 ?        00:00:00 ./bin/redis-server 127.0.0.1:6384 [cluster]

5、连接其中一个节点

[root@localhost redis-6379]# ./bin/redis-cli 
127.0.0.1:6379> CLUSTER NODES
9768ddf51689eaf6a4143fd946e5138bde292e1e :6379@16379 myself,master - 0 0 0 connected

看到集群节点数只有自己一个节点,这是因为各个单独的子节点间没有进行通讯和关联
6、各个节点间建立关联

127.0.0.1:6379> cluster meet 127.0.0.1 6380
OK
127.0.0.1:6379> CLUSTER NODES
9768ddf51689eaf6a4143fd946e5138bde292e1e 127.0.0.1:6379@16379 myself,master - 0 0 1 connected
9ca580d9bfd309125cacb3f3d84014adce3f559b 127.0.0.1:6380@16380 master - 0 1529912974096 0 connected


127.0.0.1:6379> cluster meet 127.0.0.1 6381
OK
127.0.0.1:6379> cluster meet 127.0.0.1 6382
OK
127.0.0.1:6379> cluster meet 127.0.0.1 6383
OK
127.0.0.1:6379> cluster meet 127.0.0.1 6384
OK
127.0.0.1:6379> CLUSTER NODES
9768ddf51689eaf6a4143fd946e5138bde292e1e 127.0.0.1:6379@16379 myself,master - 0 1529913014000 1 connected
c87c6c9d81e854380533eb6eda1f93bc0d54d5c5 127.0.0.1:6382@16382 master - 0 1529913015462 0 connected
9ca580d9bfd309125cacb3f3d84014adce3f559b 127.0.0.1:6380@16380 master - 0 1529913014000 2 connected
d5543d31a40230c58854986eab3ec43aa4e9b0b2 127.0.0.1:6384@16384 master - 0 1529913014000 0 connected
8a813463c796cdb515837d4d4a0e9ae5a76cb912 127.0.0.1:6383@16383 master - 0 1529913014454 4 connected
a24733a1f2e080b6f638086fa74c6904c2468eec 127.0.0.1:6381@16381 master - 0 1529913014000 3 connected

6、solt划分。这时候集群节点已经可以相互感知,但是现在的集群状态还是失败的,因为还没有进行solt的划分

127.0.0.1:6379> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:74
cluster_stats_messages_pong_sent:86
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:165
cluster_stats_messages_ping_received:86
cluster_stats_messages_pong_received:79
cluster_stats_messages_received:165

solt划分命令:

[root@localhost redis-6379]# ./bin/redis-cli -h 127.0.0.1 -p 6379 cluster addslots {0..5461}
OK
[root@localhost redis-6379]# ./bin/redis-cli -h 127.0.0.1 -p 6380 cluster addslots {5462..10922}
OK
[root@localhost redis-6379]# ./bin/redis-cli -h 127.0.0.1 -p 6381 cluster addslots {10922..16383}
(error) ERR Slot 10922 is already busy
[root@localhost redis-6379]# ./bin/redis-cli -h 127.0.0.1 -p 6381 cluster addslots {10923..16383}
OK

这时候再查看集群状态

127.0.0.1:6379> 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:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:242
cluster_stats_messages_pong_sent:263
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:510
cluster_stats_messages_ping_received:263
cluster_stats_messages_pong_received:247
cluster_stats_messages_received:510

7、主从节点配置。现在看集群各个节点的状态会发现显示都是master节点。

127.0.0.1:6379> cluster nodes
9768ddf51689eaf6a4143fd946e5138bde292e1e 127.0.0.1:6379@16379 myself,master - 0 1529913448000 1 connected 0-5461
c87c6c9d81e854380533eb6eda1f93bc0d54d5c5 127.0.0.1:6382@16382 master - 0 1529913450386 5 connected
9ca580d9bfd309125cacb3f3d84014adce3f559b 127.0.0.1:6380@16380 master - 0 1529913447361 2 connected 5462-10922
d5543d31a40230c58854986eab3ec43aa4e9b0b2 127.0.0.1:6384@16384 master - 0 1529913446000 0 connected
8a813463c796cdb515837d4d4a0e9ae5a76cb912 127.0.0.1:6383@16383 master - 0 1529913449377 4 connected
a24733a1f2e080b6f638086fa74c6904c2468eec 127.0.0.1:6381@16381 master - 0 1529913448371 3 connected 10923-16383

接下来进行从节点的指定:

[root@localhost bin]# ./redis-cli -h 127.0.0.1 -p 6382 cluster replicate 9768ddf51689eaf6a4143fd946e5138bde292e1e
OK
[root@localhost bin]# ./redis-cli -h 127.0.0.1 -p 6383 cluster replicate 9ca580d9bfd309125cacb3f3d84014adce3f559b
OK
[root@localhost bin]# ./redis-cli -h 127.0.0.1 -p 6384 cluster replicate a24733a1f2e080b6f638086fa74c6904c2468eec
OK

然后再查看集群各节点状态

127.0.0.1:6379> cluster nodes
9768ddf51689eaf6a4143fd946e5138bde292e1e 127.0.0.1:6379@16379 myself,master - 0 1529913983000 1 connected 0-5461
c87c6c9d81e854380533eb6eda1f93bc0d54d5c5 127.0.0.1:6382@16382 slave 9768ddf51689eaf6a4143fd946e5138bde292e1e 0 1529913984000 5 connected
9ca580d9bfd309125cacb3f3d84014adce3f559b 127.0.0.1:6380@16380 master - 0 1529913984693 2 connected 5462-10922
d5543d31a40230c58854986eab3ec43aa4e9b0b2 127.0.0.1:6384@16384 slave a24733a1f2e080b6f638086fa74c6904c2468eec 0 1529913981000 3 connected
8a813463c796cdb515837d4d4a0e9ae5a76cb912 127.0.0.1:6383@16383 slave 9ca580d9bfd309125cacb3f3d84014adce3f559b 0 1529913984000 4 connected
a24733a1f2e080b6f638086fa74c6904c2468eec 127.0.0.1:6381@16381 master - 0 1529913982000 3 connected 10923-16383

发现已经是三主三从的状态。
至此三主三从集群已经搭建完成,可以进行连接测试了。
连接方式:redis_cli -c -h 127.0.0.1 -p 6379
测试方法:登录其中一个主节点写信息,然后登录另一个主节点查询

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容