kubenurse就是这样一个项目,它通过监视集群中的所有网络连接,并提供监控指标供Prometheus采集。
Kubenurse
kubenurse的部署非常简单,使用Daemonset形式部署到集群节点上,Yaml文件在项目的example目录下。
部署成功后,每5秒钟会对/alive发一次检查请求,然后其内部会运行各种方法全方位对集群网络进行检测,为了防止过多的网络流量,会对检查结果缓存3秒。其检测机制如下:
从上图可以看出,kubenurse会对ingress、dns、apiserver、kube-proxy进行网络探测。
所有的检查都会创建公开的指标,这些指标可用于检测:
- SDN网络延迟以及错误
- Kubelet之间的网络延迟以及错误
- Pod与apiserver通信问题
- Ingress往返网络延迟以及错误
- Service往返网络延迟以及错误(kube-proxy)
- Kube-apiserver问题
- Kube-dns(CoreDns)错误
- 外部DNS解析错误(ingress url解析)
然后这些数据主要通过两个监控指标来体现:
- kubenurse_errors_total:按错误类型划分的错误计数器
- kubenurse_request_duration:按类型划分的请求时间分布
这些指标都是通过Type类型进行标识,对应几种不同的检测目标:
- api_server_direct:从节点直接检测 API Server
- api_server_dns:从节点通过 DNS 检测 API Server
- me_ingress:通过 Ingress 检测本服务 Service
- me_service:使用 Service 检测本服务 Service
- path_$KUBELET_HOSTNAME:节点之间的互相检测
然后这些指标分别按P50,P90,P99分位数进行划分,就可以根据不同的情况来确认集群网络状况了。
安装部署
这里直接使用官方的部署文件进行部署。不过需要更改几个地方。(1)首先将代码clone到本地
- git clone https://github.com/postfinance/kubenurse.git
(2)进入example目录,修改ingress.yaml配置,主要是添加域名,如下。
- ---
- apiVersion: extensions/v1beta1
- kind: Ingress
- metadata:
- annotations:
- kubernetes.io/ingress.class: nginx
- name: kubenurse
- namespace: kube-system
- spec:
- rules:
- - host: kubenurse-test.coolops.cn
- http:
- paths:
- - backend:
- serviceName: kubenurse
- servicePort: 8080
(2)更新daemonset.yaml配置,主要是更改ingress的入口域名,如下。
- ---
- apiVersion: apps/v1
- kind: DaemonSet
- metadata:
- labels:
- app: kubenurse
- name: kubenurse
- namespace: kube-system
- spec:
- selector:
- matchLabels:
- app: kubenurse
- template:
- metadata:
- labels:
- app: kubenurse
- annotations:
- prometheus.io/path: "/metrics"
- prometheus.io/port: "8080"
- prometheus.io/scheme: "http"
- prometheus.io/scrape: "true"
- spec:
- serviceAccountName: nurse
- containers:
- - name: kubenurse
- env:
- - name: KUBENURSE_INGRESS_URL
- value: kubenurse-test.coolops.cn # 需要更改的地方
- - name: KUBENURSE_SERVICE_URL
- value: http://kubenurse.kube-system.svc.cluster.local:8080
- - name: KUBENURSE_NAMESPACE
- value: kube-system
- - name: KUBENURSE_NEIGHBOUR_FILTER
- value: "app=kubenurse"
- image: "postfinance/kubenurse:v1.2.0"
- ports:
- - containerPort: 8080
- protocol: TCP
- tolerations:
- - effect: NoSchedule
- key: node-role.kubernetes.io/master
- operator: Equal
- - effect: NoSchedule
- key: node-role.kubernetes.io/control-plane
- operator: Equal
(4)新创建一个ServiceMonitor,用于获取指标数据,如下:
- apiVersion: monitoring.coreos.com/v1
- kind: ServiceMonitor
- metadata:
- name: kubenurse
- namespace: monitoring
- labels:
- k8s-app: kubenurse
- spec:
- jobLabel: k8s-app
- endpoints:
- - port: "8080-8080"
- interval: 30s
- scheme: http
- selector:
- matchLabels:
- app: kubenurse
- namespaceSelector:
- matchNames:
- - kube-system
(5)部署应用,在example目录下执行以下命令。
- kubectl apply -f .
(6)等待所有应用变成running,如下。
- # kubectl get all -n kube-system -l app=kubenurse
- NAME READY STATUS RESTARTS AGE
- pod/kubenurse-fznsw 1/1 Running 0 17h
- pod/kubenurse-n52rq 1/1 Running 0 17h
- pod/kubenurse-nwtl4 1/1 Running 0 17h
- pod/kubenurse-xp92p 1/1 Running 0 17h
- pod/kubenurse-z2ksz 1/1 Running 0 17h
-
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
- service/kubenurse ClusterIP 10.96.229.244
8080/TCP 17h -
- NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
- daemonset.apps/kubenurse 5 5 5 5 5
17h
(7)到prometheus上查看是否正常获取数据。
查看指标是否正常。
(8)这时候就可以在grafana上画图,展示监控数据了,如下。
参考文档
【1】https://github.com/postfinance/kubenurse
【2】https://github.com/postfinance/kubenurse/tree/master/examples