k8s下kubeprometheus监控ingressnginx

首先需要已经配置好了一个ingress-nginx亦或者使用ACK上的ingress-nginx鉴于对ingress-nginx的状态,或者流量的监控是有一定的必要性,配置监控的指标有助于了解更多细节

通过使用kube-prometheus的项目来监控ingress-nginx,首先需要在nginx-ingress-controller的yaml中配置10254的端口,并且配置一个service,最后加入到ServiceMonitor即可。

start

如果是helm,则需要如下修改

  • helm

    ..
    controller:
    metrics:
    enabled: true
    service:
    annotations:
    prometheus.io/port: "10254"
    prometheus.io/scrape: "true"
    ..

    如果不是 helm,则必须像这样编辑清单:服务清单:

- name: prometheus
port: 10254
targetPort: prometheus

prometheus将会在service中被调用

apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "10254"
..
spec:
ports:
- name: prometheus
port: 10254
targetPort: prometheus
..

deployment

apiVersion: v1
kind: Deployment
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "10254"
..
spec:
ports:
- name: prometheus
containerPort: 10254
..

测试10254的/metrics的url能够被访问到

bash-5.1$ curl 127.0.0.1:10254/metrics
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.9802e-05
go_gc_duration_seconds{quantile="0.25"} 3.015e-05
go_gc_duration_seconds{quantile="0.5"} 4.2054e-05
go_gc_duration_seconds{quantile="0.75"} 9.636e-05
go_gc_duration_seconds{quantile="1"} 0.000383868
go_gc_duration_seconds_sum 0.000972498
go_gc_duration_seconds_count 11
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 92
# HELP go_info Information about the Go environment.

Service And ServiceMonitor

另外需要配置一个ServiceMonitor, 这取决于kube-promentheus的发行版

spec部分字段如下

spec:
endpoints:
- interval: 15s # 15s频率
port: metrics # port的名称
path: /metrics # url路径
namespaceSelector:
matchNames:
- kube-system # ingress-nginx所在的名称空间
selector:
matchLabels:
app: ingress-nginx # ingress-nginx的标签

最终配置如下:

service在ingress-nginx的名称空间下配置,而ServiceMonitor在kube-prometheus的monitoring名称空间下,使用endpoints定义port名称,使用namespaceSelector.matchNames指定了ingress pod的名称空间,selector.matchLabels和标签

apiVersion: v1
kind: Service
metadata:
name: ingress-nginx-metrics
namespace: kube-system
labels:
app: ingress-nginx
annotations:
prometheus.io/port: "10254"
prometheus.io/scrape: "true"
spec:
type: ClusterIP
ports:
- name: metrics
port: 10254
targetPort: 10254
protocol: TCP
selector:
app: ingress-nginx
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: ingress-nginx-metrics
namespace: monitoring
spec:
endpoints:
- interval: 15s
port: metrics
path: /prometheus
namespaceSelector:
matchNames:
- kube-system
selector:
matchLabels:
app: ingress-nginx
  • grafana

在grafana的dashboards中搜索ingress-nginx,得到的和github的官网的模板一样

https://grafana.com/grafana/dashboards/9614?pg=dashboards&plcmt=featured-dashboard-4

image-20220418143443662.png

或者下面这个模板

image-20220418135121369.png

这些在prometheus的targets中被发现

image-20220418135156072.png

参考

ingress-nginx monitoringprometheus and grafana install