Joe Blog

Ingress 是从Kubernetes集群外部访问集群内部服务的入口。你可以在Ingress配置中提供外部可访问的URL、负载均衡、SSL、基于名称的虚拟主机等。

这次我们主要以Nginx Ingress为例子,来介绍如何将现有的的Nginx Ingress ControllerMetrics指标添加到Prometheus中以及以Grafana Dashboard的形式展现。

Nginx IngressKubernetes Ingress 的一种实现,它通过 watch Kubernetes 集群的 Ingress 资源,将 Ingress 规则转换成 Nginx 的配置,然后让 Nginx 来进行 7 `层的流量转发:

nginx-ingress-on-tke-1.jpg

deployment

针对现有的Nginx Ingress Controller的deployment 需要添加2个新的annotations

暴露Metrics端口10254

...
        - containerPort: 10254
          name: metrics
          protocol: TCP
...

Service

...
  - name: metrics
    port: 10254
    protocol: TCP
...

ServiceMonitor

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: nginx-ingress-controller-metrics
  namespace: <Your Namespce> #根据自己Prometheus的namespace来修改
  labels:
    app: nginx-ingress
    release: prom #根据自己的Prometheus ServiceMonitorSelector的标签修改
spec:
  endpoints:
  - interval: 30s
    port: metrics
  selector:
    matchLabels:
      app: nginx-ingress
      release: nginx-ingress
  namespaceSelector:
    matchNames:
    - kube-system #根据nginx ingress controller的namespace来匹配

Grafana

Grafana的管理界面导入官方Dashboard ID 9614

NGINX Ingress controller

结果

# kubectl get ep -l app=nginx-ingress -l component=controller
NAME                       ENDPOINTS                                             AGE
nginx-ingress-controller   192.168.2.12:80,192.168.2.12:10254,192.168.2.12:443   133d

nginx-ingress-controller-target.png

nginx-ingress-controller-Dashboard.png