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 图形化展示