ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Kubernetes节点之间的ping监控

2021-05-09 23:51:45  阅读:230  来源: 互联网

标签:node Kubernetes ping host dimension metrics prom registry 节点


 冯旭松 译 分布式实验室

图片

在诊断Kubernetes集群问题的时候,我们经常注意到集群中某一节点在闪烁*,而这通常是随机的且以奇怪的方式发生。这就是为什么我们一直需要一种工具,它可以测试一个节点与另一个节点之间的可达性,并以Prometheus度量形式呈现结果。有了这个工具,我们还希望在Grafana中创建图表并快速定位发生故障的节点(并在必要时将该节点上所有Pod进行重新调度并进行必要的维护)。
“闪烁”这里我是指某个节点随机变为“NotReady”但之后又恢复正常的某种行为。这会影响例如部分流量可能无法到达相邻节点上的Pod。
为什么会发生这种情况?常见原因之一是数据中心交换机中的连接问题。例如,我们曾经在Hetzner中设置一个vswitch,其中一个节点已无法通过该vswitch端口使用,并且恰好在本地网络上完全不可访问。
我们的最后一个要求是可直接在Kubernetes中运行此服务,因此我们将能够通过Helm图表部署所有内容。(例如在使用Ansible的情况下,我们必须为各种环境中的每个角色定义角色:AWS、GCE、裸机等)。由于我们尚未找到针对此环境的现成解决方案,因此我们决定自己来实现。


脚本和配置

图片


我们解决方案的主要组件是一个脚本,该脚本监视每个节点的.status.addresses值。如果某个节点的该值已更改(例如添加了新节点),则我们的脚本使用Helm value方式将节点列表以ConfigMap的形式传递给Helm图表:image.png

.Values.pingExporter.targets类似以下:image.png

下面是Python脚本:

  1. #!/usr/bin/env python3


  2. import subprocess

  3. import prometheus_client

  4. import re

  5. import statistics

  6. import os

  7. import json

  8. import glob

  9. import better_exchook

  10. import datetime


  11. better_exchook.install()


  12. FPING_CMDLINE = "/usr/sbin/fping -p 1000 -C 30 -B 1 -q -r 1".split(" ")

  13. FPING_REGEX = re.compile(r"^(\S*)\s*: (.*)$", re.MULTILINE)

  14. CONFIG_PATH = "/config/targets.json"


  15. registry = prometheus_client.CollectorRegistry()


  16. prometheus_exceptions_counter = \

  17.    prometheus_client.Counter('kube_node_ping_exceptions', 'Total number of exceptions', [], registry=registry)


  18. prom_metrics_cluster = {"sent": prometheus_client.Counter('kube_node_ping_packets_sent_total',

  19. 'ICMP packets sent',

  20. ['destination_node', 'destination_node_ip_address'],

  21.                                                  registry=registry),

  22. "received": prometheus_client.Counter('kube_node_ping_packets_received_total',

  23. 'ICMP packets received',

  24. ['destination_node', 'destination_node_ip_address'],

  25.                                                     registry=registry),

  26. "rtt": prometheus_client.Counter('kube_node_ping_rtt_milliseconds_total',

  27. 'round-trip time',

  28. ['destination_node', 'destination_node_ip_address'],

  29.                                                registry=registry),

  30. "min": prometheus_client.Gauge('kube_node_ping_rtt_min', 'minimum round-trip time',

  31. ['destination_node', 'destination_node_ip_address'],

  32.                                               registry=registry),

  33. "max": prometheus_client.Gauge('kube_node_ping_rtt_max', 'maximum round-trip time',

  34. ['destination_node', 'destination_node_ip_address'],

  35.                                               registry=registry),

  36. "mdev": prometheus_client.Gauge('kube_node_ping_rtt_mdev',

  37. 'mean deviation of round-trip times',

  38. ['destination_node', 'destination_node_ip_address'],

  39.                                                registry=registry)}



  40. prom_metrics_external = {"sent": prometheus_client.Counter('external_ping_packets_sent_total',

  41. 'ICMP packets sent',

  42. ['destination_name', 'destination_host'],

  43.                                                  registry=registry),

  44. "received": prometheus_client.Counter('external_ping_packets_received_total',

  45. 'ICMP packets received',

  46. ['destination_name', 'destination_host'],

  47.                                                     registry=registry),

  48. "rtt": prometheus_client.Counter('external_ping_rtt_milliseconds_total',

  49. 'round-trip time',

  50. ['destination_name', 'destination_host'],

  51.                                                registry=registry),

  52. "min": prometheus_client.Gauge('external_ping_rtt_min', 'minimum round-trip time',

  53. ['destination_name', 'destination_host'],

  54.                                               registry=registry),

  55. "max": prometheus_client.Gauge('external_ping_rtt_max', 'maximum round-trip time',

  56. ['destination_name', 'destination_host'],

  57.                                               registry=registry),

  58. "mdev": prometheus_client.Gauge('external_ping_rtt_mdev',

  59. 'mean deviation of round-trip times',

  60. ['destination_name', 'destination_host'],

  61.                                                registry=registry)}


  62. def validate_envs():

  63.    envs = {"MY_NODE_NAME": os.getenv("MY_NODE_NAME"), "PROMETHEUS_TEXTFILE_DIR": os.getenv("PROMETHEUS_TEXTFILE_DIR"),

  64. "PROMETHEUS_TEXTFILE_PREFIX": os.getenv("PROMETHEUS_TEXTFILE_PREFIX")}


  65. for k, v in envs.items():

  66. if not v:

  67. raise ValueError("{} environment variable is empty".format(k))


  68. return envs



  69. @prometheus_exceptions_counter.count_exceptions()

  70. def compute_results(results):

  71.    computed = {}


  72.    matches = FPING_REGEX.finditer(results)

  73. for match in matches:

  74.        host = match.group(1)

  75.        ping_results = match.group(2)

  76. if "duplicate" in ping_results:

  77. continue

  78.        splitted = ping_results.split(" ")

  79. if len(splitted) != 30:

  80. raise ValueError("ping returned wrong number of results: \"{}\"".format(splitted))


  81.        positive_results = [float(x) for x in splitted if x != "-"]

  82. if len(positive_results) > 0:

  83.            computed[host] = {"sent": 30, "received": len(positive_results),

  84. "rtt": sum(positive_results),

  85. "max": max(positive_results), "min": min(positive_results),

  86. "mdev": statistics.pstdev(positive_results)}

  87. else:

  88.            computed[host] = {"sent": 30, "received": len(positive_results), "rtt": 0,

  89. "max": 0, "min": 0, "mdev": 0}

  90. if not len(computed):

  91. raise ValueError("regex match\"{}\" found nothing in fping output \"{}\"".format(FPING_REGEX, results))

  92. return computed



  93. @prometheus_exceptions_counter.count_exceptions()

  94. def call_fping(ips):

  95.    cmdline = FPING_CMDLINE + ips

  96.    process = subprocess.run(cmdline, stdout=subprocess.PIPE,

  97.                             stderr=subprocess.STDOUT, universal_newlines=True)

  98. if process.returncode == 3:

  99. raise ValueError("invalid arguments: {}".format(cmdline))

  100. if process.returncode == 4:

  101. raise OSError("fping reported syscall error: {}".format(process.stderr))


  102. return process.stdout



  103. envs = validate_envs()


  104. files = glob.glob(envs["PROMETHEUS_TEXTFILE_DIR"] + "*")

  105. for f in files:

  106.    os.remove(f)


  107. labeled_prom_metrics = {"cluster_targets": [], "external_targets": []}


  108. while True:

  109. with open(CONFIG_PATH, "r") as f:

  110.        config = json.loads(f.read())

  111.        config["external_targets"] = [] if config["external_targets"] is None else config["external_targets"]

  112. for target in config["external_targets"]:

  113.            target["name"] = target["host"] if "name" not in target.keys() else target["name"]


  114. if labeled_prom_metrics["cluster_targets"]:

  115. for metric in labeled_prom_metrics["cluster_targets"]:

  116. if (metric["node_name"], metric["ip"]) not in [(node["name"], node["ipAddress"]) for node in config['cluster_targets']]:

  117. for k, v in prom_metrics_cluster.items():

  118.                    v.remove(metric["node_name"], metric["ip"])


  119. if labeled_prom_metrics["external_targets"]:

  120. for metric in labeled_prom_metrics["external_targets"]:

  121. if (metric["target_name"], metric["host"]) not in [(target["name"], target["host"]) for target in config['external_targets']]:

  122. for k, v in prom_metrics_external.items():

  123.                    v.remove(metric["target_name"], metric["host"])



  124.    labeled_prom_metrics = {"cluster_targets": [], "external_targets": []}


  125. for node in config["cluster_targets"]:

  126.        metrics = {"node_name": node["name"], "ip": node["ipAddress"], "prom_metrics": {}}


  127. for k, v in prom_metrics_cluster.items():

  128.            metrics["prom_metrics"][k] = v.labels(node["name"], node["ipAddress"])


  129.        labeled_prom_metrics["cluster_targets"].append(metrics)


  130. for target in config["external_targets"]:

  131.        metrics = {"target_name": target["name"], "host": target["host"], "prom_metrics": {}}


  132. for k, v in prom_metrics_external.items():

  133.            metrics["prom_metrics"][k] = v.labels(target["name"], target["host"])


  134.        labeled_prom_metrics["external_targets"].append(metrics)


  135. out = call_fping([prom_metric["ip"] for prom_metric in labeled_prom_metrics["cluster_targets"]] + \

  136. [prom_metric["host"] for prom_metric in labeled_prom_metrics["external_targets"]])

  137.    computed = compute_results(out)


  138. for dimension in labeled_prom_metrics["cluster_targets"]:

  139.        result = computed[dimension["ip"]]

  140.        dimension["prom_metrics"]["sent"].inc(computed[dimension["ip"]]["sent"])

  141.        dimension["prom_metrics"]["received"].inc(computed[dimension["ip"]]["received"])

  142.        dimension["prom_metrics"]["rtt"].inc(computed[dimension["ip"]]["rtt"])

  143.        dimension["prom_metrics"]["min"].set(computed[dimension["ip"]]["min"])

  144.        dimension["prom_metrics"]["max"].set(computed[dimension["ip"]]["max"])

  145.        dimension["prom_metrics"]["mdev"].set(computed[dimension["ip"]]["mdev"])


  146. for dimension in labeled_prom_metrics["external_targets"]:

  147.        result = computed[dimension["host"]]

  148.        dimension["prom_metrics"]["sent"].inc(computed[dimension["host"]]["sent"])

  149.        dimension["prom_metrics"]["received"].inc(computed[dimension["host"]]["received"])

  150.        dimension["prom_metrics"]["rtt"].inc(computed[dimension["host"]]["rtt"])

  151.        dimension["prom_metrics"]["min"].set(computed[dimension["host"]]["min"])

  152.        dimension["prom_metrics"]["max"].set(computed[dimension["host"]]["max"])

  153.        dimension["prom_metrics"]["mdev"].set(computed[dimension["host"]]["mdev"])


  154.    prometheus_client.write_to_textfile(

  155.        envs["PROMETHEUS_TEXTFILE_DIR"] + envs["PROMETHEUS_TEXTFILE_PREFIX"] + envs["MY_NODE_NAME"] + ".prom", registry)


该脚本在每个Kubernetes节点上运行,并且每秒两次发送ICMP数据包到Kubernetes集群的所有实例。收集的结果会存储在文本文件中。
该脚本会包含在Docker镜像中:

image.png


另外,我们还创建了一个ServiceAccount和一个具有唯一权限的对应角色用于获取节点列表(这样我们就可以知道它们的IP地址):

image.png


最后,我们需要DaemonSet来运行在集群中的所有实例:
image.png
image.png

该解决方案的最后操作细节是:


  • Python脚本执行时,其结果(即存储在主机上/var/run/node-exporter-textfile目录中的文本文件)将传递到DaemonSet类型的node-exporter。

  • node-exporter使用--collector.textfile.directory /host/textfile参数启动,这里的/host/textfile是hostPath目录/var/run/node-exporter-textfile。(你可以点击这里[1]了解关于node-exporter中文本文件收集器的更多信息。)

  • 最后node-exporter读取这些文件,然后Prometheus从node-exporter实例上收集所有数据。


那么结果如何?

图片


现在该来享受期待已久的结果了。指标创建之后,我们可以使用它们,当然也可以对其进行可视化。以下可以看到它们是怎样的。
首先,有一个通用选择器可让我们在其中选择节点以检查其“源”和“目标”连接。你可以获得一个汇总表,用于在Grafana仪表板中指定的时间段内ping选定节点的结果:

图片


以下是包含有关选定节点的组合统计信息的图形:

图片


另外,我们有一个记录列表,其中每个记录都链接到在“源”节点中选择的每个特定节点的图:

图片


如果将记录展开,你将看到从当前节点到目标节点中已选择的所有其他节点的详细ping统计信息:

图片


下面是相关的图形:

图片


节点之间的ping出现问题的图看起来如何?

图片


如果你在现实生活中观察到类似情况,那就该进行故障排查了!
最后,这是我们对外部主机执行ping操作的可视化效果:

图片


我们可以检查所有节点的总体视图,也可以仅检查任何特定节点的图形:

图片


当你观察到仅影响某些特定节点的连接问题时,这可能会有所帮助。
相关链接:
  1. https://github.com/prometheus/node_exporter#textfile-collector


原文链接:https://medium.com/flant-com/ping-monitoring-between-kubernetes-nodes-11e815f4eff1


标签:node,Kubernetes,ping,host,dimension,metrics,prom,registry,节点
来源: https://blog.51cto.com/u_15127630/2764305

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有