Prometheus

Prometheus 基础

什么是 Prometheus #

Prometheus 是一个开源的 monitoring 和 alerting 系统。

Prometheus 将 指标 (metrics) 收集并存储为 时序(time series) 数据,指标信息与记录它的 时间 和可选的被称为 标签 (labels)的键值对一块存储。

什么是 metrics #

时序

通过问题理解 Prometheus #

Prometheus 是如何收集指标数据的 #

Prometheus 是如何存储指标数据的 #

Prometheus 是如何存储一个指标的时间序列的 #

参考 #

kube-prometheus-stack 安装

kube-prometheus-stack 是 prometheus 的官方 helm charts,包含 prometheus-operatorprometheusgrafanaalertmanagernode-exporter 等组件。

安装 kube-prometheus-stack #

使用 helm charts 安装 kube-prometheus-stack

mkdir -p ~/charts/kube-prometheus-stack
cd ~/charts/kube-prometheus-stack
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
# values.yaml 用来查看默认值
helm show values prometheus-community/kube-prometheus-stack > values.yaml
cat <<EOF > custom-values.yaml
prometheus:
  prometheusSpec:
    additionalScrapeConfigs: []
    podMonitorSelectorNilUsesHelmValues: false
    ruleSelectorNilUsesHelmValues: false
    probeSelectorNilUsesHelmValues: false
    scrapeConfigSelectorNilUsesHelmValues: false
    serviceMonitorSelectorNilUsesHelmValues: false
    storageSpec:
      volumeClaimTemplate:
        spec:
          storageClassName: "ceph-block"
          accessModes: ["ReadWriteOnce"]
          resources:
            requests:
              storage: 50Gi
  service:
    type: NodePort
  ingress:
    enabled: true
    ingressClassName: nginx
    hosts: ['prometheus.lan']
  replicas: 1
  retention: 10d

prometheusOperator:
  enabled: true

# grafana service
grafana:
  service:
    type: NodePort
  ingress:
    enabled: true
    ingressClassName: nginx
    hosts: ['grafana.lan']
  persistence:
    enabled: true
    type: sts
    storageClassName: "ceph-block"
    accessModes:
      - ReadWriteOnce
    size: 20Gi

alertmanager:
  enabled: true

nodeExporter:
  enabled: true
EOF
helm upgrade --install --create-namespace --namespace monitoring kube-prometheus-stack prometheus-community/kube-prometheus-stack -f custom-values.yaml

配置说明 #

有一个相关 issue 讨论:servicemonitor not being discovered

...

node-exporter 安装

node-exporter 安装 #

生成账号密码的 bcrypt hash #

apt install apache2-utils -y

生成一个账号密码的 bcrypt hash

  • -B 强制使用 bcrypt 算法
  • -C 10 指定 bcrypt 的 cost 值为 10, golang bcrypt 默认 cost 值也为 10

注意修改下面的 usernamepassword 为你要设置的账号密码

# htpasswd -nbBC 10 username password
username:$2y$10$poDYDLemE3r95gcQ.h8FdODudFaFZhwZCSX1RTwpI2s8V4Mwm0.lO

关于 bcrypt #

格式为 $2<a/b/x/y>$[cost]$[22 character salt][31 character hash]

例如

$2y$10$poDYDLemE3r95gcQ.h8FdODudFaFZhwZCSX1RTwpI2s8V4Mwm0.lO
\__/\/ \____________________/\_____________________________/
Alg Cost      Salt                        Hash

运行 node-exporter #

  1. 创建 prometheus 配置文件 /etc/prometheus/web.yml
  2. 创建 docker-compose.yml 文件
  3. 运行 docker compose
PASS='$2y$10$poDYDLemE3r95gcQ.h8FdODudFaFZhwZCSX1RTwpI2s8V4Mwm0.lO'
mkdir -p /etc/prometheus

cat <<EOF> /etc/prometheus/web.yml
basic_auth_users:
  # username: password
  prometheus: ${PASS}
EOF

cat <<EOF> /etc/prometheus/docker-compose.yml
services:
  node-exporter:
    image: quay.io/prometheus/node-exporter:latest
    container_name: node-exporter
    command: 
      - "--path.rootfs=/host"
      - "--web.config.file=/etc/prometheus/web.yml"
    network_mode: "host"
    pid: host
    restart: always
    volumes:
      - '/:/host:ro,rslave'
      - /etc/prometheus/web.yml:/etc/prometheus/web.yml
EOF

docker compose -f /etc/prometheus/docker-compose.yml up -d

在 prometheus 中配置指标收集 #

方式一:直接修改 prometheus 配置 #

修改 kube-prometheus-stack chart 配置并更新,或者直接修改保存配置的 configmap 中的 job 配置, 记得修改 {EDIT_HERE} 为实际值

...