解决k8s集群中Redis Cluster故障

k8s集群中的一个node节点故障,将这个node节点下线后上面的pod迁移到其他节点,但是大量pod都产生报错。经排查,是由于redis集群故障导致。但是查看resdis pod,都是running状态,如下图


redis pod状态

由于这些pod是组成集群使用,既然pod是正常的,应用又报redis链接的错误,所以问题肯定出在Redis Cluster上,查看Redis Cluster状态:


Redis Cluster状态

既然redis集群故障,直接删掉redis,用helm重装。但是结果还是无法组成集群。仔细分析故障原因,得出结论:
k8s架构示意图如下:
示意图

这个示意图我只画出三个node,简单表达一下意思即可。三个node上各运行了一个master和一个slave节点。由于node3节点故障已经移除集群,这个节点上之前运行的其他无状态pod迁移到其他节点可以正常运行,但是master2和slave2在node3上有持久化数据,虽然在node4上重建了,但是由于缺失数据,原来的集群状态被破坏了,所以重新部署也无法恢复,由于是master2和slave2的数据都丢失了,集群无法重建。通过开发了解到,redis上都是缓存数据,丢失影响不大,于是删除本地持久化数据,重新部署redis node,再手动创建集群。

#为了可以手动指定master节点,第一步只用maser创建cluster集群,再手动加入slave节点
./redis-trib.rb create --replicas 0 172.29.11.9:8382 172.29.11.15:8382 172.29.11.20:8382
>>> Creating cluster
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
172.29.11.9:8382
172.29.11.15:8382
172.29.11.20:8382
M: 7f8e4fbd362fd003b1890aa24dd673d06d401500 172.29.11.9:8382
   slots:0-5460 (5461 slots) master
M: 89b3c9925dd167bb7292dcacc715c949506cb022 172.29.11.15:8382
   slots:5461-10922 (5462 slots) master
M: 5160f483410cf0b2fd3fc55d6844f5336f1e1c47 172.29.11.20:8382
   slots:10923-16383 (5461 slots) master
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 172.29.11.9:8382)
M: 7f8e4fbd362fd003b1890aa24dd673d06d401500 172.29.11.9:8382
   slots:0-5460 (5461 slots) master
M: 89b3c9925dd167bb7292dcacc715c949506cb022 172.29.11.15:8382
   slots:5461-10922 (5462 slots) master
M: 5160f483410cf0b2fd3fc55d6844f5336f1e1c47 172.29.11.20:8382
   slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#为了避免一个节点故障导致丢失一组redis数据,添加slave节点时要避免同一组的master和slave运行在一个节点上
#添加slave1节点
./redis-trib.rb add-node --master-id 7f8e4fbd362fd003b1890aa24dd673d06d401500 --slave 172.29.11.15:8383 172.29.11.9:8382
>>> Adding node 172.29.11.15:8383 to cluster 172.29.11.9:8382
>>> Performing Cluster Check (using node 172.29.11.9:8382)
M: 7f8e4fbd362fd003b1890aa24dd673d06d401500 172.29.11.9:8382
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
M: 5160f483410cf0b2fd3fc55d6844f5336f1e1c47 172.29.11.20:8382
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
M: 89b3c9925dd167bb7292dcacc715c949506cb022 172.29.11.15:8382
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.29.11.15:8383 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 172.29.11.9:8382.
[OK] New node added correctly.
./redis-trib.rb add-node --master-id 89b3c9925dd167bb7292dcacc715c949506cb022 --slave 172.29.11.20:8383 172.29.11.15:8382
>>> Adding node 172.29.11.20:8383 to cluster 172.29.11.15:8382
>>> Performing Cluster Check (using node 172.29.11.15:8382)
M: 89b3c9925dd167bb7292dcacc715c949506cb022 172.29.11.15:8382
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
S: 31866adbf9034931f63bb69b62b5169f0d01ab71 172.29.11.15:8383
   slots: (0 slots) slave
   replicates 7f8e4fbd362fd003b1890aa24dd673d06d401500
M: 5160f483410cf0b2fd3fc55d6844f5336f1e1c47 172.29.11.20:8382
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
M: 7f8e4fbd362fd003b1890aa24dd673d06d401500 172.29.11.9:8382
   slots:0-5460 (5461 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.
>>> Send CLUSTER MEET to node 172.29.11.20:8383 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 172.29.11.15:8382.
[OK] New node added correctly.
./redis-trib.rb add-node --master-id 5160f483410cf0b2fd3fc55d6844f5336f1e1c47 --slave 172.29.11.9:8383 172.29.11.20:8382
>>> Adding node 172.29.11.9:8383 to cluster 172.29.11.20:8382
>>> Performing Cluster Check (using node 172.29.11.20:8382)
M: 5160f483410cf0b2fd3fc55d6844f5336f1e1c47 172.29.11.20:8382
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
S: 1546356362eac839986133a43f571fc59cdc4503 172.29.11.20:8383
   slots: (0 slots) slave
   replicates 89b3c9925dd167bb7292dcacc715c949506cb022
M: 89b3c9925dd167bb7292dcacc715c949506cb022 172.29.11.15:8382
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 31866adbf9034931f63bb69b62b5169f0d01ab71 172.29.11.15:8383
   slots: (0 slots) slave
   replicates 7f8e4fbd362fd003b1890aa24dd673d06d401500
M: 7f8e4fbd362fd003b1890aa24dd673d06d401500 172.29.11.9:8382
   slots:0-5460 (5461 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.
>>> Send CLUSTER MEET to node 172.29.11.9:8383 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 172.29.11.20:8382.
[OK] New node added correctly.

三个节点都添加完成,并且没有报错。进入一个master节点查看集群状态:

/data# redis-cli -p 8382
127.0.0.1:8382> 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:3
cluster_my_epoch:1
cluster_stats_messages_sent:12915
cluster_stats_messages_received:12915
127.0.0.1:8382>

集群状态终于恢复正常。重建后的Redis Cluster集群架构示意图如下


重建后架构

总结:对于有状态的应用,redis、mysql等,容器化时一定要考虑周全,避免主从节点运行在一个节点上。对于redis应用,如果读写I/O不是特别高,还是建议直接使用主从复制架构,故障恢复简单且迅速。

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

推荐阅读更多精彩内容