Prometheus 有多种部署模式,适用于不同的场景和需求。以下是几种常见的部署模式:
这是最简单的部署模式,适用于小型环境或测试环境。
prometheus.yml
。bash
复制
./prometheus --config.file=prometheus.yml
为了提高系统的可用性和容错能力,可以使用高可用部署模式。
prometheus.yml
。yaml
复制
# prometheus.yml 示例
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
联邦部署模式适用于大规模分布式系统,通过分层采集和汇总数据。
yaml
复制
# 中心 Prometheus 配置示例
scrape_configs:
- job_name: 'federate'
scrape_interval: 15s
honor_labels: true
metrics_path: '/federate'
params:
'match[]':
- '{job="prometheus"}'
- '{__name__=~"job:.*"}'
static_configs:
- targets:
- 'source-prometheus-1:9090'
- 'source-prometheus-2:9090'
分片部署模式通过将数据采集任务分散到多个 Prometheus 实例,适用于超大规模环境。
prometheus.yml
。yaml
复制
# 分片 Prometheus 配置示例
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'sharded_job'
static_configs:
- targets: ['target1:9100', 'target2:9100']
relabel_configs:
- source_labels: [__address__]
modulus: 2
target_label: __tmp_hash
action: hashmod
- source_labels: [__tmp_hash]
regex: '0'
action: keep
Thanos 和 Cortex 是 Prometheus 的扩展项目,提供了分布式存储和查询能力。
prometheus.yml
。yaml
复制
# Thanos Sidecar 配置示例
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
spec:
replicas: 1
template:
spec:
containers:
- name: prometheus
image: prom/prometheus:latest
args:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--storage.tsdb.path=/prometheus"
- "--web.enable-lifecycle"
- name: thanos-sidecar
image: thanosio/thanos:latest
args:
- "sidecar"
- "--prometheus.url=http://localhost:9090"
- "--tsdb.path=/prometheus"
Prometheus Operator 是 Kubernetes 上的一个扩展,简化了 Prometheus 的部署和管理。
bash
复制
# 安装 Prometheus Operator
kubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/main/bundle.yaml
# 创建 Prometheus 实例
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: prometheus
spec:
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
team: frontend
resources:
requests:
memory: 400Mi
不同的部署模式适用于不同的场景和需求。单节点部署适合小型环境,高可用部署适合生产环境,联邦部署和分片部署适合大规模分布式系统,而使用 Thanos 或 Cortex 的分布式部署适合需要长期存储和全局视图的场景。在 Kubernetes 环境中,使用 Prometheus Operator 可以简化部署和管理。