在Kubernetes中,网络是通过第三方网络插件来提供,这些三方插件本身的实现就比较复杂,以至于在排查网络问题时常常碰壁。那么有没有什么方式来监控集群中所有的网络连接呢?
kubenurse
就是这样一个项目,它通过监视集群中的所有网络连接,并提供监控指标供Prometheus采集。
kubenurse的部署非常简单,使用Daemonset形式部署到集群节点上,Yaml文件在项目的example目录下。
部署成功后,每5秒钟会对/alive发一次检查请求,然后其内部会运行各种方法全方位对集群网络进行检测,为了防止过多的网络流量,会对检查结果缓存3秒。其检测机制如下:
从上图可以看出,kubenurse会对ingress、dns、apiserver、kube-proxy进行网络探测。
所有的检查都会创建公开的指标,这些指标可用于检测:
然后这些数据主要通过两个监控指标来体现:
这些指标都是通过Type
类型进行标识,对应几种不同的检测目标:
然后这些指标分别按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 <none> 8080/TCP 17h
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/kubenurse 5 5 5 5 5 <none> 17h
(7)到prometheus
上查看是否正常获取数据。
查看指标是否正常。
(8)这时候就可以在grafana
上画图,展示监控数据了,如下。
【1】https://github.com/postfinance/kubenurse
【2】https://github.com/postfinance/kubenurse/tree/master/examples