node-problem-detector介绍node-problem-detector是一个收集节点异常的组件,目标是使上层控制面对节点异 常可见。在kubernetes集群中当前控制层面对节点异常的感知还比较薄弱,对于一些节点异常情况不能感知到,而这些异常问题往往会影 响pod的运行。如当某节点pod网络异常时,kubernetes任然可以把相关pod调度到此节点,此时新创建的pod往往不可用。所 以社区引入node-problem-detector来补充这一不足。node-problem-detector在每个节点上运行检测 节点问题并将其报告,使各种节点问题对集群中的上层可见。原理介绍采集上报KubernetesApiserverStackdrive rMonitorSystemLogMonitorCustomPluginMonitorSystemStatsMonitorPro metheusProblemMetricsManagerPrometheusexporterKubernetesexport erStackdriverexporterHealthcheckernode-problem-detector主要包括“Moni tor”和“Exporter”两大类型功能组件。“Monitor”类组件负责异常问题检测或指标数据采集,“Exporter”类组件 负责向上对接控制层面进行数据上报。其中SystemLogMonitor和CustomPluginMonitor检测到的异常或指标可 通过Kubernetesexporter或Stackdriverexporter分别上报到kubernetesapiserv er或StackdriverMonitor。而SystemStatsMonitor和ProblemMetricsManager 的监控指标数据通过Prometheusexporter暴露,进而可以通过Prometheus采集到相关数据。主要模块介绍Moni tornode-problem-detector包括SystemLogMonitor、SystemStatsMonitor和Cus tomPluginMonitor3种monitor来分别检测不同类型的问题,详细类型参考:Monitor类型说明SystemLo gMonitor根据与设定的规则监控日志来检测问题,目前可以配置的日志有系统日志、kubelet日志、docker日志、conta inerd日志。SystemStatsMonitor监控和节点健康相关的监控指标。CustomPluginMonitor根据用户自 定义检测脚本来检测。ExporterExporter的作用是把检测到的问题或数据上报给相应后端。node-problem-dete ctor目前提供三种Exporter:Exporter类型说明Kubernetesexporter上报问题到Kubernetes APIserver:临时问题以Events形式上报;永久问题以NodeConditions形式上报。Prometheus exporter上报节点的监控指标到Prometheus。Stackdriverexporter上报问题和监控指标到Stackd riverMonitoringAPI.HealthcheckerHealthchecker负责node-problem-det ector运行节点的kubelet和容器运行时的健康检查功能,并在检查出异常后进行重启造作。ProblemMetricsManag ernode-problem-detector检测问题的指标数据,如某个类型问题发生的次数,某个异常问题是否影响了节点。部署(1) 部署node-problem-detector添加node-problem-detectorrepo:[root@master ]#helmrepoadddeliveryherohttps://charts.deliveryhero.io/安装n ode-problem-detector[root@master]#helminstall--repohttps://c harts.deliveryhero.io/npdnode-problem-detectorNAME:npdLASTDEP LOYED:MonJun2713:39:192022NAMESPACE:defaultSTATUS:deployed REVISION:1TESTSUITE:NoneNOTES:Toverifythatthenode-problem- detectorpodshavestarted,run:kubectl--namespace=defaultget pods-l"app.kubernetes.io/name=node-problem-detector,app.kuberne tes.io/instance=npd"[root@master]#查看node-problem-detector已运行于各个节 点:模拟内核出现异常,在日志中写入内核异常的日志:[root@node1]#sh-c"echo''kernel:BUG: unabletohandlekernelNULLpointerdereferenceatTESTING''>> /dev/kmsg"查看事件,可以看到node-problem-detector已检测到kernelBUG。自定义检测扩展实践n ode-problem-detector当前支持基于日志匹配规则的异常检测;kubelet运行异常检测;容器运行时异常检测;ntp 服务未启动检测和conntrack表超过90%的检测等问题检查项。虽然node-problem-detector已内置了一些问题检 测,但实际使用时候往往还不能满足用户的需求,需要自定义检测进行扩展。以下以“NTPProblem”为例介绍自定义检测是如何集成到n ode-problem-detector中的。(1)配置添加通过前文可知node-problem-detector提供了Custo mPluginMonitor支持用户添加自定义脚本来检测相关的问题。启动时需要在启动参数中添加--config.custom-p lugin-monitor配置:node-problem-detector---config.custom-plugin-mo nitor=/config/custom-plugin-monitor.json.....custom-plugin-moni tor.json内如如下:{"plugin":"custom","pluginConfig":{"invoke_inte rval":"30s","timeout":"5s","max_output_length":80,"concurre ncy":3,"enable_message_change_based_condition_update":false}, "source":"ntp-custom-plugin-monitor","metricsReporting":true, "conditions":[{"type":"NTPProblem","reason":"NTPIsUp","me ssage":"ntpserviceisup"}],"rules":[{"type":"temporary" ,"reason":"NTPIsDown","path":"./config/plugin/check_ntp.sh", "timeout":"3s"},{"type":"permanent","condition":"NTPProble m","reason":"NTPIsDown","path":"./config/plugin/check_ntp.sh" ,"timeout":"3s"}]}从配置文件中可以看出实际检测时node-problem-detector调用的脚本是“ check_ntp.sh”,目录在镜像的/config/plugin/目录中,内如如下:#!/bin/bash#Thisplu ginchecksifthentpserviceisrunningundersystemd.#NOTE:Th isisonlyanexampleforsystemdservices.readonlyOK=0readonly NONOK=1readonlyUNKNOWN=2readonlySERVICE=''ntpd.service''#Checks ystemdcmdpresentif!command-vsystemctl>/dev/null;thenecho "Couldnotfind''systemctl''-requiresystemd"exit$UNKNOWNfi# Returnsuccessifserviceactive(i.e.running)ifsystemctl-qis -active"$SERVICE";thenecho"$SERVICEisrunning"exit$OKelse #Doesnotdifferenciatestopped/failedservicefromnon-existent echo"$SERVICEisnotrunning"exit$NONOKfi因为脚本中直接使用了systemctl命 令,因此当已pod形式运行在节点中时node-problem-detector镜像中需要安装好此命令否则检测脚本将不可用,同时为了 能够和宿主机的systemd通信,需要把宿主机上的“/var/run/dbus”和“/run/systemd”目录挂载进node- problem-detector容器中。(2)结果验证如宿主机中ntp异常即可在kubernetes中看到上报信息:在node.status.conditions中上报信息如下:event形式上报信息如下:节点开启ntp服务后,node.status.conditions上报信息如下:总结在kubernetes集群中节点异常对上层的可见性是非常必要的,这种感知能力在原生的集群中还比较薄弱。node-problem-detector对这一能力进行了加强,同时基于用户自定义扩展机制使得node-problem-detector可检测的节点问题很丰富。 |
|