prometheus监控k8s所有资源的配置文件该怎么写,不使用 Prometheus Operator
时间: 2024-01-21 19:16:52 浏览: 186
在 Kubernetes 中监控所有资源的配置文件可以通过以下步骤实现:
1. 创建一个 `prometheus-configmap.yaml` 文件,定义 Prometheus 的全局配置:
```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
data:
prometheus.yml: |
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'kubernetes-apiservers'
kubernetes_sd_configs:
- role: endpoints
namespaces:
names:
- kube-system
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs:
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: keep
regex: default;kubernetes;https
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_node_name]
target_label: __metrics_path__
replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: kubernetes_pod_name
```
以上配置文件中,定义了三个 job:
- `kubernetes-apiservers`:监控 Kubernetes API 服务器的指标。
- `kubernetes-nodes`:监控 Kubernetes 节点的指标。
- `kubernetes-pods`:监控 Kubernetes Pod 的指标。
2. 创建一个 `prometheus-deployment.yaml` 文件,定义 Prometheus 的 Deployment:
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
labels:
app: prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
containers:
- name: prometheus
image: prom/prometheus:v2.30.0
args:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
ports:
- name: web
containerPort: 9090
protocol: TCP
volumeMounts:
- name: config
mountPath: /etc/prometheus
- name: storage
mountPath: /prometheus
volumes:
- name: config
configMap:
name: prometheus-config
- name: storage
emptyDir: {}
```
以上配置文件中,定义了一个名为 `prometheus` 的 Deployment,使用了 prom/prometheus:v2.30.0 镜像,并挂载了 `prometheus-config` ConfigMap 和 `storage` EmptyDir。
3. 创建一个 `prometheus-service.yaml` 文件,定义 Prometheus 的 Service:
```yaml
apiVersion: v1
kind: Service
metadata:
name: prometheus
spec:
selector:
app: prometheus
ports:
- name: web
port: 9090
targetPort: web
type: ClusterIP
```
以上配置文件中,定义了一个名为 `prometheus` 的 Service,将端口 9090 映射到 Deployment 中的 web 端口。
4. 部署 Prometheus:
```bash
$ kubectl apply -f prometheus-configmap.yaml
$ kubectl apply -f prometheus-deployment.yaml
$ kubectl apply -f prometheus-service.yaml
```
5. 配置 Kubernetes 的 ServiceMonitor,以便 Prometheus 可以监控所有资源:
```yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kubernetes
labels:
k8s-app: prometheus-operator
spec:
selector:
matchLabels:
k8s-app: kube-state-metrics
endpoints:
- port: http-metrics
interval: 30s
path: /metrics
namespaceSelector:
matchNames:
- kube-system
```
以上配置文件中,定义了名为 `kubernetes` 的 ServiceMonitor,监控了 kube-state-metrics 的 http-metrics 端口。
6. 部署 ServiceMonitor:
```bash
$ kubectl apply -f service-monitor.yaml
```
这样,Prometheus 就可以监控 Kubernetes 中的所有资源了。
阅读全文