prometheus监控k8s所有资源的配置文件该怎么写
时间: 2024-01-21 15:16:49 浏览: 99
k8s-prometheus-配置文件.zip
要监控 Kubernetes 中的所有资源,可以使用 Prometheus Operator 来创建和管理 Prometheus 和相关的资源。下面是一个示例 Prometheus Operator 配置文件,用于监控 Kubernetes 中的所有资源:
```yaml
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
spec:
replicas: 2
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
app: kube-prometheus-stack
ruleSelector:
matchLabels:
app: kube-prometheus-stack
alerting:
alertmanagers:
- namespace: monitoring
name: alertmanager
port: web
resources:
requests:
memory: 400Mi
storage:
volumeClaimTemplate:
spec:
storageClassName: standard
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 5Gi
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kube-controller-manager
namespace: monitoring
labels:
app: kube-prometheus-stack
spec:
selector:
matchLabels:
component: kube-controller-manager
endpoints:
- port: https-metrics
scheme: https
tlsConfig:
insecureSkipVerify: true
- port: http-metrics
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kube-scheduler
namespace: monitoring
labels:
app: kube-prometheus-stack
spec:
selector:
matchLabels:
component: kube-scheduler
endpoints:
- port: http-metrics
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kube-etcd
namespace: monitoring
labels:
app: kube-prometheus-stack
spec:
selector:
matchLabels:
component: etcd
endpoints:
- port: metrics
scheme: https
tlsConfig:
insecureSkipVerify: true
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kube-apiserver
namespace: monitoring
labels:
app: kube-prometheus-stack
spec:
selector:
matchLabels:
component: kube-apiserver
endpoints:
- port: https-metrics
scheme: https
tlsConfig:
insecureSkipVerify: true
- port: http-metrics
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kube-state-metrics
namespace: monitoring
labels:
app: kube-prometheus-stack
spec:
selector:
matchLabels:
app.kubernetes.io/name: kube-state-metrics
endpoints:
- port: http-metrics
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: node-exporter
namespace: monitoring
labels:
app: kube-prometheus-stack
spec:
selector:
matchLabels:
app.kubernetes.io/name: node-exporter
endpoints:
- port: metrics
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kubelet
namespace: monitoring
labels:
app: kube-prometheus-stack
spec:
selector:
matchLabels:
component: kubelet
endpoints:
- port: https-metrics
scheme: https
tlsConfig:
insecureSkipVerify: true
- port: cadvisor
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kube-proxy
namespace: monitoring
labels:
app: kube-prometheus-stack
spec:
selector:
matchLabels:
app.kubernetes.io/name: kube-proxy
endpoints:
- port: metrics
```
这个配置文件会创建一个 Prometheus 实例和一些 ServiceMonitor 资源,用于监控 Kubernetes 中的所有组件和节点。你可以使用 kubectl apply 命令来应用这个配置文件:
```
kubectl apply -f prometheus.yaml
```
阅读全文