使用 Prometheus Blackbox 检查 Gremlin 端口

解读

在国内生产环境,OrientDB 常被用作图数据库支撑实时风控、知识图谱等业务,Gremlin Server 默认监听 8182 端口。面试官问“用 Prometheus Blackbox 检查”,核心想验证两点:

  1. 你是否理解 Gremlin 走的是 WebSocket/HTTP 混合协议,而不是纯 TCP;
  2. 你是否能把 Blackbox Exporter 的 HTTP prober、TLS 配置、多租户 Header、JSON 校验 与 OrientDB 的 Gremlin 接口结合起来,给出可落地的告警规则。
    答得太浅(只测 8182 通不通)会被认为“运维思维”,答得太深(自己改源码)又显得“过度设计”。要把 Blackbox 的四大能力(探测、匹配、度量、告警)全部映射到 OrientDB 的 Gremlin 语义,并给出复制粘贴即可用的配置片段,才能拿到高分。

知识点

  1. Gremlin Server 健康 URI:/gremlin?gremlin=100-1 返回 {"result":{"@value":99}},可作为探活探针。
  2. Blackbox Exporter 模块:http_2xx 探针支持 fail_if_body_not_contains_regexp 做正则回包校验,tls_config 可加载国密双证。
  3. Prometheus Relabel:利用 __param_target 与 __address__ 把 OrientDB 多实例(如 orient-1:8182、orient-2:8182)动态化写入 Probe 指标。
  4. SLA 指标:probe_duration_seconds>0.5 视为毫秒级遍历承诺违约;probe_success==0 连续 3 次即触发 GremlinPortDown 告警。
  5. 云原生合规:金融客户要求 mTLS + 国密 SM2,Blackbox 的 tls_config.insecure_skip_verify=false 必须配合 SM2 证书链与 cipher_suites 显式声明,否则会被安全基线扫描扣分。

答案

  1. 安装 Blackbox Exporter(以 0.24 为例)
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.24.0/blackbox_exporter-0.24.0.linux-amd64.tar.gz
tar -xzf blackbox_exporter-0.24.0.linux-amd64.tar.gz
sudo cp blackbox_exporter /usr/local/bin
  1. 创建 /etc/blackbox_exporter/blackbox.yml,重点在 http 探针校验 Gremlin 回包
modules:
  gremlin_orientdb:
    prober: http
    timeout: 5s
    http:
      valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
      method: GET
      headers:
        Host: "orientdb.internal"
        Accept: "application/json"
      fail_if_body_not_contains_regexp:
        - '"@value":99'   # 确保 Gremlin 引擎真算出了结果
      fail_if_ssl_not_last: true
      tls_config:
        ca_file: /etc/ssl/orientdb/ca.crt
        cert_file: /etc/ssl/orientdb/client.crt
        key_file: /etc/ssl/orientdb/client.key
        insecure_skip_verify: false
  1. Prometheus 抓取任务,利用 relabel 把实例地址注入 probe 参数
scrape_configs:
  - job_name: 'gremlin-blackbox'
    metrics_path: /probe
    params:
      module: [gremlin_orientdb]
    static_configs:
      - targets:
          - 'orient-1:8182'
          - 'orient-2:8182'
          - 'orient-3:8182'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: blackbox-exporter:9115
  1. 记录规则与告警(满足金融客户 RPO<30s 要求)
groups:
  - name: gremlin_sla
    interval: 15s
    rules:
      - record: gremlin:probe_success:rate5m
        expr: 1 - avg_over_time(probe_success{job="gremlin-blackbox"}[5m])
      - alert: GremlinPortDown
        expr: gremlin:probe_success:rate5m > 0.1
        for: 30s
        labels:
          severity: critical
          team: dba
        annotations:
          summary: "OrientDB Gremlin 端口探测失败"
          description: "实例 {{ $labels.instance }} 连续 3 次无法返回预期 JSON,可能遍历引擎挂死"
  1. 验证
curl 'http://blackbox-exporter:9115/probe?target=orient-1:8182&module=gremlin_orientdb'

回显中 probe_success 1 且 probe_duration_seconds<0.1 即为通过。

拓展思考

  1. 多模型探针分层:除了 Gremlin,OrientDB 还暴露 HTTP 命令接口(2480) 与 二进制协议(2424)。可再定义 orientdb_sql 模块,发 POST /command/sql 体 {"command":"select count(*) from V"},用 fail_if_body_not_contains_regexp 校验 "@type":"d",实现 图+文档双层探活。
  2. Chaos 验证:利用 Gremlin 遍历语言本身制造慢查询 g.V().repeat(both()).times(10).count(),观察 Blackbox 的 probe_duration_seconds 是否超过 SLO 阈值 500 ms,从而反向证明告警灵敏度。
  3. Operator 集成:在 Kubernetes 环境下,把 Blackbox 的 Probe 定义写进 OrientDB Operator CRD,通过 ServiceMonitor 自动发现新副本,实现 Pod 水平扩容时的零配置接入。
  4. 国密合规演进:若客户要求 SM2 双证 + CRL 校验,Blackbox 官方二进制不支持,可 二次编译引入 gmsm 包,或在 Exporter 前加 nginx-sm2 反向代理,把 TLS 终结层上移,让 Blackbox 只探活 HTTP 200,兼顾合规与可维护性。