• Kubernetes Prometheus 监控 Nginx


    1、nginx服务开启with-http_stub_status_module模块(具体开启方式自行百度)
    1.1、nginx配置文件添加如下内容

    server {
        listen 8080;
        server_name  localhost;
        location /stub_status {
           stub_status on;
           access_log off;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1.2、重新加载nginx配置即可

    nginx -t reload 
    
    • 1

    1.3、浏览器测试是否配置成功
    在这里插入图片描述
    2、部署exporter,从而创建一个metrics接口
    2.1、创建一个nginx-exporter服务,获取并格式化metrics接口数据

    [root@master ~]# cat nginx-exporter.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-exporter
      namespace: kuboard
    spec:
      selector:
        matchLabels:
          k8s: nginx-exporter
      template:
        metadata:
          labels:
            k8s: nginx-exporter
        spec:
          containers:
            - name: nginx-exporter
              image: nginx/nginx-prometheus-exporter:0.9.0
              imagePullPolicy: IfNotPresent
              command:
                - "nginx-prometheus-exporter"
                - "-nginx.scrape-uri=http://10.100.209.42:8080/status"
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-exporter
      namespace: kuboard
      labels:
        k8s: nginx-exporter
    spec:
      type: ClusterIP
      selector:
        k8s: nginx-exporter
      ports:
      - name: nginx-api
        port: 9113
        protocol: TCP
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    2.2、应用nginx-exporter.yaml

    kubectl apply -f nginx-exporter.yaml 
    
    • 1

    2.3、查看是否创建成功

    [root@master ~]# kubectl get pod,svc -n kuboard |grep nginx
    pod/nginx-exporter-7f979ddc95-ks89g          1/1     Running   0                24h
    service/nginx-exporter          ClusterIP   10.106.218.51    <none>        9113/TCP                                       24h
    
    • 1
    • 2
    • 3

    2.4、测试 metrics 接口

    [root@master ~]# curl 10.106.218.51:9113/metrics
    # HELP nginx_connections_accepted Accepted client connections
    # TYPE nginx_connections_accepted counter
    nginx_connections_accepted 59
    # HELP nginx_connections_active Active client connections
    # TYPE nginx_connections_active gauge
    nginx_connections_active 1
    # HELP nginx_connections_handled Handled client connections
    # TYPE nginx_connections_handled counter
    nginx_connections_handled 59
    # HELP nginx_connections_reading Connections where NGINX is reading the request header
    # TYPE nginx_connections_reading gauge
    nginx_connections_reading 0
    # HELP nginx_connections_waiting Idle client connections
    # TYPE nginx_connections_waiting gauge
    nginx_connections_waiting 0
    # HELP nginx_connections_writing Connections where NGINX is writing the response back to the client
    # TYPE nginx_connections_writing gauge
    nginx_connections_writing 1
    # HELP nginx_http_requests_total Total http requests
    # TYPE nginx_http_requests_total counter
    nginx_http_requests_total 8728
    # HELP nginx_up Status of the last metric scrape
    # TYPE nginx_up gauge
    nginx_up 1
    # HELP nginxexporter_build_info Exporter build information
    # TYPE nginxexporter_build_info gauge
    nginxexporter_build_info{commit="5f88afbd906baae02edfbab4f5715e06d88538a0",date="2021-03-22T20:16:09Z",version="0.9.0"} 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3、部署EndPrints,链接expertor暴露出来的metrics接口

    [root@master ~]# kubectl get ep -n kuboard |grep nginx
    nginx-exporter          10.244.1.99:9113                                                 24h
    
    • 1
    • 2

    4、创建servicemonitor

    [root@master ~]# cat nginx-servicemonitor.yaml 
    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      labels:
        k8s: nginx-exporter
      name: nginx-exporter
      namespace: kuboard
    spec:
      endpoints:
      - interval: 10s
        port: nginx-api
      selector:
        matchLabels:
          k8s: nginx-exporter
      namespaceSelector:
        matchNames:
        - kuboard
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4.1、检查是否创建成功

    [root@master ~]# kubectl get servicemonitor -n kuboard |grep nginx
    nginx-exporter            24h
    
    • 1
    • 2

    4.2、prometheus 界面查看是否已同步
    在这里插入图片描述
    4.3、grafana 图形化展示
    在这里插入图片描述

  • 相关阅读:
    【排序】十大排序算法
    javaweb个人物品信息管理系统springboot+Ssm
    springboot项目若以集成minio文件服务
    umich cv-1
    Spring Aop原理解析和使用示例
    pyinstaller打包记录|| 打包成功,含xgboost打包遇到的问题
    JAVA社区疫情防控系统毕业设计,社区疫情防控管理系统设计与实现,毕设作品参考
    计算机组成原理
    uniapp配置网络请求
    Java内存模型(JMM)
  • 原文地址:https://blog.csdn.net/ljx1528/article/details/125372057