Prometheus
如何估算 Prometheus 的本地存储和内存消耗
· ☕ 2 分钟
1. 本地存储容量 所需磁盘大小(GB) = 数据保留时长 _ 每秒获取指标数量 _ 指标数据大小 / 1024 / 1024 / 1024 其中 每秒获取指标数量 rate(prometheus_tsdb_head_samples_appended_total[1d]) 一个小时内样本的平均大小 rate(prometheus_tsdb_compaction_chunk_size_bytes_sum[1d])/rate(prometheus_tsdb_compaction_chunk_samples_sum[1d]) 一天(86400 秒)的磁盘消耗,可以在 Prometheus 中直接查询: 86400 * (rate(prometheus_tsdb_head_samples_appended_total[1d]) * (rate(prometheus_tsdb_compaction_chunk_size_bytes_sum[1d]) / rate(prometheus_tsdb_compaction_chunk_samples_sum[1d]))) / 1024 /1024 / 1024 例如,返回 {instance="localhost:9090", job="prometheus"} 4.437

常用的各类资源 Prometheus 告警语句
· ☕ 4 分钟
主机 主机内存使用率超过阈值 1 - node_memory_MemAvailable_bytes{mode!="idle"} / node_memory_MemTotal_bytes 阈值:0.9 主机 CPU 使用率超过阈值 1 - avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by (host_name) 阈值:0.85 主机硬盘使用率超过阈值 1 - avg without (fstype)(node_filesystem_free_bytes{fstype!='rootfs',mountpoint!~'/(run|var|snap).*'} / node_filesystem_size_bytes{fstype!='rootfs',mountpoint!~'/(run|var|snap).*'}) 阈值:0.8 Windows Windows 主机内存使用率超过阈值 1 - 1 * windows_os_physical_memory_free_bytes{job="windows_exporter",mode!="idle"} / windows_cs_physical_memory_bytes 阈值:0.9 Windows 主机 CPU 使用率超过阈值 1 - (avg by (host_ip,host_name) (irate(windows_cpu_time_total{job="windows_exporter",mode="idle"}[1m]))) 阈值:0.85

使用 Blackbox Exporter 测试网络连通性
· ☕ 3 分钟
如果你需要监控两个主机、主机与外部服务之间的网络状况,那么就可以试一试本文提到的 Blackbox Exporter。 1. 安装 Blackbox 1.1 在主机上部署 下载二进制包 1 2 3 4 5 wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.21.0/blackbox_exporter-0.21.0.linux-amd64.tar.gz tar -xzvf blackbox_exporter-0.21.0.linux-amd64.tar.gz mv blackbox_exporter-0.21.0.linux-amd64/blackbox_exporter /usr/bin/ mkdir /etc/prometheus mv blackbox_exporter-0.21.0.linux-amd64/blackbox.yml /etc/prometheus/ 清理安装包 1 rm -rf blackbox_exporter-0.21.0.linux-amd64* 新建 Systemd 服务 1 vim /usr/lib/systemd/system/blackbox_exporter.service 新增如下内容: [Unit] Description=blackbox_exporter After=network.target [Service] Restart=on-failure ExecStart=/usr/bin/blackbox_exporter \ --config.file=/etc/prometheus/blackbox.yml Restart=on-failure [Install] WantedBy=multi-user.target

如何采集 Kubernetes 对象的 labels 和 annotations
· ☕ 2 分钟
1. 为什么需要 kube-status-metrics Kubernetes 的监控主要关注两类指标: 基础性能指标 CPU、内存、磁盘、网络等指标,可以通过 DaemonSet 部署 node-exporter,由 Prometheus 抓取相关指标。 资源对象指标 Deployment 的副本数量、Pod 的运行状态等。这些指标需要 kube-status-metrics 轮询 Kubernetes 的 API 查询,并暴露给 Prometheus 才能够看到

二进制部署 Thanos
· ☕ 1 分钟
1. 下载二进制文件 1 2 3 4 wget https://github.com/thanos-io/thanos/releases/download/v0.26.0/thanos-0.26.0.linux-amd64.tar.gz tar xvf thanos-0.26.0.linux-amd64.tar.gz mv thanos-0.26.0.linux-amd64/thanos /usr/bin/ rm -rf thanos-0.26.0.linux-amd64* 2. 安装 Thanos Query 1 vim /etc/systemd/system/thanos-query.service 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [Unit] Description=Thanos Query After=network-online.target [Service] Restart=on-failure ExecStart=/usr/bin/thanos query \ --log.level=debug \ --query.auto-downsampling \ --grpc-address=0.0.0.0:30091 \ --http-address=0.0.0.0:30092 \ --query.partial-response \ --query.replica-label=prometheus_replica \ --query.replica-label=rule_replica \ --store=1.0.0.0:30091 \ --store=2.0.0.0:30091 [Install] WantedBy=multi-user.target --store 参数添加需要聚合的全部源。 1 systemctl daemon-reload && systemctl enable thanos-query 1 systemctl start thanos-query && systemctl status thanos-query 3. 安装 Thanos Sidecar 1 vim /etc/systemd/system/thanos-sidecar.service 1 2 3 4 5