目前公司在用的Prometheus是安装在k8s集群中,安装方式类似如下如下链接:
https://blog.csdn.net/zhangshaohuas/article/details/107681209
安装于k8s集群中的Prometheus如果要监控k8s集群外的服务或者服务器,那么需要用到k8s的endpoint资源,将外部服务映射为集群的内部服务,然后进行访问通信。
Kubernetes-服务连接和暴露(endpoints)(二十) (juejin.cn)
创建好能连接外部资源的endponit 后,再创建service 和serviceMonitor资源 就能监控到一个外部服务,如下是范例yaml文件:
监控外部linux主机的serviceMonitor\service\endpoint的yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: linux-metrics-sm
namespace: monitoring
labels:
prometheus: k8s
spec:
namespaceSelector:
matchNames:
- monitoring
selector:
matchLabels:
k8s-app: linux-metrics
endpoints:
- port: metrics
interval: 10s
---
apiVersion: v1
kind: Service
metadata:
name: linux-metrics
namespace: monitoring
labels:
k8s-app: linux-metrics
spec:
type: ExternalName
externalName: 192.168.0.20
clusterIP: ""
ports:
- name: metrics
port: 9100
protocol: TCP
targetPort: 9100
---
apiVersion: v1
kind: Endpoints
metadata:
name: linux-metrics #name与service的name一致
namespace: monitoring
labels:
k8s-app: linux-metrics
subsets:
- addresses:
- ip: 192.168.0.20
ports:
- name: metrics
port: 9100
protocol: TCP