运维

prometheus监控nginx(无metric接口)

2021-04-29  本文已影响0人  慕知

https://github.com/nginxinc/nginx-prometheus-exporter

监控不携带metrics接口的服务步骤:

1、部署exporter,从而创建一个metrics接口

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

3、部署Service,基于ServiceMonitor使用

4、创建ServiceMonitor,注入promethues

5、测试

6、加入grafana,做大屏展示

准备好环境

image.png

1、部署exporter,从而创建一个metrics接口

1) 创建一个服务,获取并格式化metrics接口数据
[root@\ k8s-m-01~]# mkdir exporter_nginx
[root@\ k8s-m-01~/exporter_nginx]# vim deployment_nginx.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-prometheus-exporter
spec:
  selector:
    matchLabels:
      k8s: nginx-prometheus-exporter-servicemonitor
  template:
    metadata:
      labels:
        k8s: nginx-prometheus-exporter-servicemonitor
    spec:
      containers:
        - name: nginx-exporter
          image: nginx/nginx-prometheus-exporter:0.9.0
          imagePullPolicy: IfNotPresent
          command:
            - "nginx-prometheus-exporter"
            - "-nginx.scrape-uri=http://192.168.15.109/status"


[root@\ k8s-m-01~/exporter_nginx]# kubectl apply -f deployment_nginx.yaml 
deployment.apps/nginx-prometheus-exporter created

[root@\ k8s-m-01~/exporter_nginx]# kubectl get deployments.apps 
NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
nginx-prometheus-exporter   1/1     1            1           21s

2) 创建一个Service,给予prometheus集群获取格式化好了的metrics接口服务
[root@\ k8s-m-01~/exporter_nginx]# kubectl apply -f 
kind: Service
apiVersion: v1
metadata:
  name: nginx-prometheus-exporter
  labels:
    k8s: nginx-prometheus-exporter-servicemonitor
spec:
  ports:
    - port: 9113
      targetPort: 9113
      name: nginx-prometheus-exporter
      protocol: TCP
  selector:
    k8s: nginx-prometheus-exporter-servicemonitor





[root@\ k8s-m-01~/exporter_nginx]# kubectl apply -f service_nginx.yaml 

[root@\ k8s-m-01~/exporter_nginx]# kubectl get svc
NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
nginx-prometheus-exporter   ClusterIP   10.106.255.69   <none>        9113/TCP   4s

[root@\ k8s-m-01~/exporter_nginx]# kubectl get pods
NAME                                         READY   STATUS    RESTARTS   AGE
nginx-prometheus-exporter-579949c67b-z9zdt   1/1     Running   0          3m11s

3) 测试
[root@\ k8s-m-01~/exporter_nginx]# curl 10.106.255.69:9113/metrics

# HELP nginx_connections_accepted Accepted client connections
# TYPE nginx_connections_accepted counter
nginx_connections_accepted 4
# 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 4
# 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 5
# 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


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

参考上述service
[root@\ k8s-m-01~/exporter_nginx]# kubectl get endpoints
NAME                        ENDPOINTS            AGE
nginx-prometheus-exporter   10.244.0.11:9113     3m35s

3、部署Service,基于ServiceMonitor使用

4、创建ServiceMonitor,注入promethues

[root@\ k8s-m-01~/exporter_nginx]# vim servicemonitor_nginx.yaml
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
  labels:
    k8s: nginx-prometheus-exporter-servicemonitor
    deploy: nginx-exporter-servicemonitor
  name: nginx-monitor
  namespace: monitoring
spec:
  endpoints:
    - interval: 3s
      port: nginx-prometheus-exporter
  selector:
    matchLabels:
      k8s: nginx-prometheus-exporter-servicemonitor
  namespaceSelector:
    matchNames:
      - "default"



[root@\ k8s-m-01~/exporter_nginx]# kubectl apply -f servicemonitor_nginx.yaml 


[root@\ k8s-m-01~/exporter_nginx]# kubectl get servicemonitor -n monitoring 
NAME                      AGE
nginx-monitor             39s



5、测试

image.png image.png

6、加入grafana,做大屏展示

grafana找nginx
上一篇下一篇

猜你喜欢

热点阅读