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、重新加载nginx配置即可

nginx -t reload 

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

2.2、应用nginx-exporter.yaml

kubectl apply -f nginx-exporter.yaml 

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

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

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

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

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

4.1、检查是否创建成功

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

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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维那些事~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值