Prometheus 是一个开源的监控和 alerting 工具,适用于各种系统和应用程序。在 Kubernetes 环境中部署 Prometheus,可以方便地监控集群状态和应用性能。以下是一份简单的 Kubernetes 部署 Prometheus 的教程。

安装 Prometheus

  1. 首先,在 Kubernetes 集群中创建一个名为 prometheus.yaml 的配置文件,内容如下:
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: prometheus
  namespace: monitoring
rules:
- apiGroups: ["monitoring.coreos.com"]
  resources: ["prometheuses"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["monitoring.coreos.com"]
  resources: ["prometheusrule"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["apiextensions.k8s.io"]
  resources: ["customresourcedefinitions"]
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: prometheus
  namespace: monitoring
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: monitoring
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      serviceAccountName: prometheus
      containers:
      - name: prometheus
        image: prom/prometheus:v2.33.0
        ports:
        - containerPort: 9090
        volumeMounts:
        - name: config
          mountPath: /etc/prometheus
      volumes:
      - name: config
        configMap:
          name: prometheus-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: monitoring
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
    scrape_configs:
    - job_name: 'kubernetes-pods'
      kubernetes_sd_configs:
      - role: pod
  1. 使用 kubectl apply -f prometheus.yaml 命令创建 Prometheus。

配置 Prometheus

  1. 创建一个名为 prometheus-config.yaml 的配置文件,内容如下:
global:
  scrape_interval: 15s
scrape_configs:
- job_name: 'kubernetes-pods'
  kubernetes_sd_configs:
  - role: pod
  1. 使用 kubectl create configmap prometheus-config --from-file=prometheus-config.yaml 命令创建 ConfigMap。

访问 Prometheus

  1. 使用 kubectl get svc -n monitoring 命令获取 Prometheus 服务的 IP 地址。

  2. 在浏览器中输入 http://<IP 地址>:9090 访问 Prometheus。

Prometheus Dashboard

更多关于 Prometheus 的教程,请访问本站 Prometheus 教程