Prometheus监控不携带metrics接口的服务
目录
Prometheus监控不携带metrics接口的服务
prometheus监控分为两种:
1、携带metircs接口的服务
2、不携带metrics接口的服务
一、prometheus监控不携带metrics接口的服务的流程
监控不携带metrics接口的服务,需要先创建一个metrics接口,从而进行监控
1、部署expertor,从而创建一个metrics接口
2、部署EndPrints,连接expertor暴漏出来的metrics接口
3、部署Service,给予ServiceMonitor使用
4、创建ServiceMonitor,注入prometheus
5、测试
二、prometheus监控nginx
2.1、创建实验环境:部署一个nginx,加载nginx监控插件
#安装nginx
[root@k8s-node-02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@k8s-node-02 ~]# yum clean all
[root@k8s-node-02 ~]# yum makecache
[root@k8s-node-02 ~]# yum install -y nginx
#修改配置文件,启动nginx监控
[root@k8s-node-02 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location = /status {
stub_status;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
#重启nginx,载入配置文件
[root@k8s-node-02 ~]# systemctl restart nginx
#在其他服务器上访问测试
[root@k8s-master-01 ~]# curl 192.168.15.33/status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
2.2、部署expertor,创造一个metrics接口
- 创建一个服务,获取并格式化metrics接口数据
kind: Deployment
apiVersion: apps/v1
metadata:
name: nginx-prometheus-exporter
spec:
selector:
matchLabels:
k8s: nginx-prometheus
template:
metadata:
labels:
k8s: nginx-prometheus
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.12.90/status"
- 创建一个sercice,给予peometheus集群获取metrics接口提供的格式化的监控数据
kind: Service
apiVersion: v1
metadata:
name: nginx-prometheus-exporter
spec:
ports:
- port: 9113
targetPort: 9113
name: nginx-prometheus-exporter
protocol: TCP
selector:
k8s: nginx-prometheus
- 查看创建结果
[root@k8s-master-01 prometheus-nginx]# kubectl apply -f ./[上述两个文件]
[root@k8s-master-01 prometheus-nginx]# kubectl get svc,pod
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-prometheus-exporter ClusterIP 10.108.25.181 <none> 9113/TCP 45s
NAME READY STATUS RESTARTS AGE
pod/nginx-prometheus-exporter-7cd66d4df-wll78 1/1 Running 0 44s
- 测试
[root@k8s-master-01 prometheus-nginx]# curl 10.108.25.181:9113/metrics
# HELP nginx_connections_accepted Accepted client connections
# TYPE nginx_connections_accepted counter
nginx_connections_accepted 25
# HELP nginx_connections_active Active client connections
# TYPE nginx_connections_active gauge
nginx_connections_active 2
# HELP nginx_connections_handled Handled client connections
# TYPE nginx_connections_handled counter
nginx_connections_handled 25
# 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 1
# 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 16799
# 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.3、创建EndPrints,链接expertor暴漏出来的metrics接口
# 参考上述Service,此处无需重复创建。
2.4、部署Service,给予ServiceMonitor使用
# 参考上述Service,此处无需重复创建。
2.5、创建ServiceMonitor,注入prometheus
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
labels:
k8s: nginx-prometheus
name: nginx-monitor
namespace: monitoring
spec:
endpoints:
- interval: 3s
port: nginx-prometheus-exporter
path: metrics
selector:
matchLabels:
k8s: nginx-prometheus
namespaceSelector:
matchNames:
- "default"
- 查看部署结果
[root@k8s-master-01 prometheus-nginx]# kubectl apply -f servicemonitor.yaml
servicemonitor.monitoring.coreos.com/nginx-monitor created
[root@k8s-master-01 prometheus-nginx]# kubectl get servicemonitors -n monitoring
NAME AGE
nginx-monitor 18s