0
点赞
收藏
分享

微信扫一扫

Prometheus实战-从0构建高可用监控平台(一)

minute_5 2023-05-10 阅读 85

当今的互联网应用系统越来越复杂,其中涉及的组件和服务越来越多,需要进行高效、可靠的监控,以保证系统的稳定性和性能。Prometheus是一款功能强大的开源监控系统,可以实时监控多个维度的指标数据,并支持强大的查询语言和告警机制,是目前广泛使用的云原生应用监控系统之一。

本文档合集《Prometheus实战:从0构建高可用监控平台》将从零开始,手把手教您如何构建一套高可用的Prometheus监控平台,涵盖了以下内容:

  1. 1. Prometheus集群搭建:实现高可用和可扩展的监控系统
  2. 2. 动态监控指标:自动发现和注册要监控的目标
  3. 3. 告警机制配置:灵活配置告警规则、分组、过滤、抑制,实时通知异常情况
  4. 4. Grafana可视化展示:直观了解系统运行状态和趋势

本文档合集的目标读者是具有一定Linux系统和网络知识的系统管理员和DevOps工程师。通过本文档合集的学习,您将掌握Prometheus的核心概念和实践技巧,能够快速搭建一套高效、可靠的监控平台,帮助您更好地管理和维护复杂的互联网应用系统。

本文是以下内容是基于Thanos安装一个高可用和可扩展的Prometheus集群。

环境

后面几篇文档都在改环境下生成。

主机系统和软件(服务)

// 主机信息
10.2.0.6  Q-gz-common-prod-thanos-001 node-1
10.2.0.10 Q-gz-common-prod-thanos-002 node-2
10.2.0.41 Q-gz-common-prod-thanos-003 node-3

// 系统版本
CentOS Linux release 7.9.2009 (Core)

// 软件和服务
Consul
ConsulManager
Docker
Prometheus
Alertmanager
PrometheusAlert
MySQL
Thanos
Haproxy
Keepalived
Lsyncd
Grafana
Cos
运维脚本

部署规划

// 各模块规划

node-1 部署的服务: Consul , Grafana, Lsyncd, Keepalived, Haproxy, Thanos, ConsulManager, Docker

node-2 部署的服务: Consul , Prometheus, Alertmanager,PrometheusAlert,  Lsyncd, Keepalived, Haproxy, Thanos

node-3 部署的服务: Consul , Prometheus, Alertmanager, PrometheusAlert, MySQL, Thanos

调用拓扑

部署Consul 集群

Consul是一种分布式服务发现和配置管理工具,可以用于管理各种服务的注册、发现和配置。它提供了一种简单而强大的方法来发现和管理服务,并帮助确保服务实例始终保持可用状态。Consul还提供了健康检查、故障转移和分布式一致性等功能,可以帮助应用程序自动化地管理自己的服务实例。

node1, node2, node3

yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum install consul consul-template -y

mkdir -p /data/consul/{config,data,log} && chown -R consul.consul /data/consul

log_rotate_max_files:指定要保留的旧日志文件存档的最大数量。默认为 0(不会删除任何文件)。设置为 -1 以在创建新日志文件时丢弃旧日志文件。

log_rotate_duration:指定日志在需要轮换之前应写入的最大持续时间。必须是持续时间值,例如 30s。默认为 24 小时。

log_file:将所有 Consul 代理日志消息写入文件。此值用作日志文件名的前缀。当前时间戳附加到文件名。如果值以路径分隔符结尾,consul- 则将附加到该值。如果文件名缺少扩展名,.log 则附加。例如,设置log-file为/var/log/将导致日志文件路径为/var/log/consul-{timestamp}.log. log-file可以-log-rotate-bytes (opens new window)与-log-rotate-duration (opens new window)结合使用 , 以获得细粒度的日志轮换体验。

retry_join:指定将要置入集群的IP列表,如果失败,会自动重试,知道直到成功加入。

node1

[root@Q-gz-common-prod-thanos-001 ~]# consul keygen
xxxxx=

cat > /usr/lib/systemd/system/consul.service << EOF
# Consul systemd service unit file
[Unit]
Description=Consul Service Discovery Agent
Documentation=https://www.consul.io/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=consul
Group=consul
ExecStart=/usr/bin/consul agent \
    -node=node-1 \
    -config-dir=/data/consul/config
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=10240
LimitNPROC=10240

[Install]
WantedBy=multi-user.target
EOF


cat > /data/consul/config/config.json <<EOF
{
     "advertise_addr": "10.2.0.6",
     "bind_addr": "10.2.0.6",
     "bootstrap_expect": 3,
     "client_addr": "0.0.0.0",
     "datacenter": "guangzhou",
     "data_dir": "/data/consul/data",
     "domain": "consul",
     "enable_script_checks": true,
     "dns_config": {
         "enable_truncate": true,
         "only_passing": true
     },
     "log_json": true,
     "log_level": "error",
     "log_rotate_max_files": 10,
     "log_rotate_duration": "24h",
     "log_file": "/data/consul/log/",
     "encrypt": "xxxxx=",
     "leave_on_terminate": true,
     "rejoin_after_leave": true,
     "retry_join": [
         "node-1",
         "node-2",
         "node-3"
     ],
     "server": true,
     "start_join": [
         "node-1",
         "node-2",
         "node-3"
     ],
     "acl": {
         "enabled":  true,
         "default_policy": "deny",
         "enable_token_persistence": true
     },
     "ui_config": {
      "enabled": true
     }
}
EOF

systemctl enable consul
systemctl start consul

node-2

cat > /usr/lib/systemd/system/consul.service << EOF
# Consul systemd service unit file
[Unit]
Description=Consul Service Discovery Agent
Documentation=https://www.consul.io/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=consul
Group=consul
ExecStart=/usr/bin/consul agent \
    -node=node-2 \
    -config-dir=/data/consul/config
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=10240
LimitNPROC=10240

[Install]
WantedBy=multi-user.target
EOF

cat > /data/consul/config/config.json <<EOF
{
     "advertise_addr": "10.2.0.10",
     "bind_addr": "10.2.0.10",
     "bootstrap_expect": 3,
     "client_addr": "0.0.0.0",
     "datacenter": "guangzhou",
     "data_dir": "/data/consul/data",
     "domain": "consul",
     "enable_script_checks": true,
     "dns_config": {
         "enable_truncate": true,
         "only_passing": true
     },
     "log_json": true,
     "log_level": "error",
     "log_rotate_max_files": 10,
     "log_rotate_duration": "24h",
     "log_file": "/data/consul/log/",
     "encrypt": "xxxxx=",
     "leave_on_terminate": true,
     "rejoin_after_leave": true,
     "retry_join": [
         "node-1",
         "node-2",
         "node-3"
     ],
     "server": true,
     "start_join": [
         "node-1",
         "node-2",
         "node-3"
     ],
     "acl": {
         "enabled":  true,
         "default_policy": "deny",
         "enable_token_persistence": true
     },
     "ui_config": {
      "enabled": true
     }
}
EOF

systemctl enable consul
systemctl start consul

查看 login token

后面Prometheus做动态发现的时候需要。

[root@Q-gz-common-prod-thanos-001 ~]# consul acl bootstrap
AccessorID:       6708400c-3a06-62af-6bd7-fae9b1fd9235
SecretID:         63961456-9521-xxxx-bfd8-8a386162bb81
Description:      Bootstrap Token (Global Management)
Local:            false
Create Time:      2022-08-14 17:00:52.323737575 +0800 CST
Policies:
   00000000-0000-0000-0000-000000000001 - global-management

ConsulManager

ConsulManager是一个工具,它可以帮助管理Consul集群的配置和状态。ConsulManager可以让你轻松地管理Consul集群的各个方面,包括服务发现、健康检查、KV存储、DNS、ACL等等。

node1 节点

mkdir /usr/local/consulmanager/
cd /usr/local/consulmanager/
wget https://starsl.cn/static/img/docker-compose.yml

编辑:docker-compose.yml,修改3个环境变量:
consul_token:consul的登录token(如何获取?)
consul_url:consul的URL(http开头,/v1要保留)
admin_passwd:登录ConsulManager Web的admin密码

[root@Q-gz-common-prod-thanos-001 consulmanager]# cat docker-compose.yml
version: "3.2"
services:
  flask-consul:
    image: registry.cn-shenzhen.aliyuncs.com/starsl/flask-consul:latest
    container_name: flask-consul
    hostname: flask-consul
    restart: always
    volumes:
      - /usr/share/zoneinfo/PRC:/etc/localtime
    environment:
      consul_token: 63961456-9521-xxxx-bfd8-8a386162bb81
      consul_url: http://10.2.0.6:8500/v1
      admin_passwd: xxxx
  nginx-consul:
    image: registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:latest
    container_name: nginx-consul
    hostname: nginx-consul
    restart: always
    ports:
      - "1026:1026"
    volumes:
      - /usr/share/zoneinfo/PRC:/etc/localtime

启动:docker-compose pull && docker-compose up -d

访问:http://{IP}:1026,使用配置的ConsulManager admin密码登录

部署Thanos

Thanos :可以帮我们简化分布式 Prometheus的部署与管理,并提供了一些的高级特性︰全局视图,长期存储,高可用,该架构使用grpc保持各个组件的通讯,sidecar组件负责连接Prometheus,将其数据提供给Thanos Query查询,并且/或者将其上传到对象存储,以供长期存储。

每个集群都对应了一个唯一的租户 ID,可以通过租户标签区分不同集群的指标数据;如果某个租户创建了新的集群,只需在新集群中部署 Prometheus 并配置远程写入;

由于 Prometheus 远程读取改进,强烈建议使用 Prometheus v2.13+。

// 在三个节点都执行如下的操作

cd /usr/local/src/
wget https://github.com/thanos-io/thanos/releases/download/v0.27.0/thanos-0.27.0.linux-amd64.tar.gz
tar xf thanos-0.27.0.linux-amd64.tar.gz -C /usr/local/
ln -sv /usr/local/thanos-0.27.0.linux-amd64 /usr/local/thanos

echo "PATH=\$PATH:/usr/local/thanos" > /etc/profile.d/thanos.sh 
source    /etc/profile 

groupadd -r thanos
useradd -r -g thanos -s /sbin/nologin -c "thanos Daemons" thanos


mkdir -p /usr/local/thanos/{store,query,receive,compact}
mkdir -p /data/thanos/{store,query,receive,compact}

chown -R thanos. /data/thanos
chown -R thanos. /usr/local/thanos-0.27.0.linux-amd64

各组件介绍和管理

cos桶配置

Thanos 可以使用对象存储服务(如 COS 桶)来存储 Prometheus 数据和 Thanos 组件生成的元数据,以实现高可用和长期存储。 使用 COS 桶作为数据存储,可以实现以下功能:

长期存储:将 Prometheus 数据存储到 COS 桶中,可以实现对数据的长期存储,以便于未来的查询和分析。

可扩展性:COS 桶可以存储大量的数据,并且可以根据需要动态扩展存储容量,以应对不断增长的数据需求。

数据备份:将 Prometheus 数据备份到 COS 桶中,可以保证数据的可靠性和安全性,防止因数据丢失或故障导致的业务损失。

高可用性:使用多个 COS 桶作为 Thanos 存储后端,可以实现数据的多副本备份和冗余存储,以提高数据的可用性和容错能力。

cat > /usr/local/thanos/cos_bucket.yaml <<EOF
type: COS
config:
  bucket: "prod-thanos-1001060"
  region: "ap-guangzhou"
  app_id: ""
  endpoint: "https://prod-thanos-1001060.cos.ap-guangzhou.myqcloud.com"
  secret_key: "xxxxxkey"
  secret_id: "xxxxxscr"
  http_config:
    idle_conn_timeout: 1m30s
    response_header_timeout: 2m
    insecure_skip_verify: false
    tls_handshake_timeout: 10s
    expect_continue_timeout: 1s
    max_idle_conns: 100
    max_idle_conns_per_host: 100
    max_conns_per_host: 0
    tls_config:
      ca_file: ""
      cert_file: ""
      key_file: ""
      server_name: ""
      insecure_skip_verify: false
    disable_compression: false
prefix: ""
EOF

node-3

cat > /usr/lib/systemd/system/consul.service << EOF
# Consul systemd service unit file
[Unit]
Description=Consul Service Discovery Agent
Documentation=https://www.consul.io/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=consul
Group=consul
ExecStart=/usr/bin/consul agent \
    -node=node-3 \
    -config-dir=/data/consul/config
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=10240
LimitNPROC=10240

[Install]
WantedBy=multi-user.target
EOF

cat > /data/consul/config/config.json <<EOF
{
     "advertise_addr": "10.2.0.41",
     "bind_addr": "10.2.0.41",
     "bootstrap_expect": 3,
     "client_addr": "0.0.0.0",
     "datacenter": "guangzhou",
     "data_dir": "/data/consul/data",
     "domain": "consul",
     "enable_script_checks": true,
     "dns_config": {
         "enable_truncate": true,
         "only_passing": true
     },
     "log_json": true,
     "log_level": "error",
     "log_rotate_max_files": 10,
     "log_rotate_duration": "24h",
     "log_file": "/data/consul/log/",
     "encrypt": "xxxxx=",
     "leave_on_terminate": true,
     "rejoin_after_leave": true,
     "retry_join": [
         "node-1",
         "node-2",
         "node-3"
     ],
     "server": true,
     "start_join": [
         "node-1",
         "node-2",
         "node-3"
     ],
     "acl": {
         "enabled":  true,
         "default_policy": "deny",
         "enable_token_persistence": true
     },
     "ui_config": {
      "enabled": true
     }
}
EOF

systemctl enable consul
systemctl start consul

Store配置

Store:是一个 Thanos 的代理,通过 gRPC 连接到 Prometheus 服务,并提供了长期存储和全局查询的能力。

node1 和 node2

cat > /etc/systemd/system/thanos-store.service <<EOF
[Unit]
Description=thanos-store
Documentation=https://thanos.io/
After=network.target
[Service]
Type=simple
User=thanos
ExecStart=/usr/local/thanos/thanos store \
          --data-dir=/data/thanos/store \
          --objstore.config-file=/usr/local/thanos/cos_bucket.yaml \
          --http-address         0.0.0.0:10906 \
          --grpc-address         0.0.0.0:10905
ExecReload=/bin/kill -HUP
TimeoutStopSec=20s
Restart=on-failure
LimitNOFILE=10240
LimitNPROC=10240
LimitCORE=infinity
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start thanos-store
systemctl enable thanos-store

Receive配置

Receive:接收来自 Prometheus 服务器的数据,并将其写入长期存储中。

node1 , node2 , node3

mkdir /usr/local/thanos/receive/fb
cat > /usr/local/thanos/receive/fb/hashring.json <<EOF
[
  {
    "hashring": "fanbook",
    "endpoints": [
      "10.2.0.6:10907",
      "10.2.0.10:10907",
      "10.2.0.41:10907"
    ],
    "tenants": ["fb"]
  }
]
EOF

cat > /etc/systemd/system/thanos-receive.service <<EOF
[Unit]
Description=thanos-receive
Documentation=https://thanos.io/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/thanos/thanos receive \
          --tsdb.path=/data/thanos/receiver \
          --tsdb.retention=7d \
          --grpc-address 0.0.0.0:10907 \
          --remote-write.address 0.0.0.0:10908 \
          --http-address 0.0.0.0:10909 \
          --receive.local-endpoint 10.2.0.6:10907 \
          --receive.replication-factor 3 \
          --receive.hashrings-file=/usr/local/thanos/receive/fb/hashring.json \
          --objstore.config-file=/usr/local/thanos/cos_bucket.yaml \
          --label=replica="fb"  
ExecReload=/bin/kill -HUP
TimeoutStopSec=20s
Restart=always
KillMode=process
KillSignal=SIGTERM
LimitNOFILE=655350
LimitNPROC=655350
LimitCORE=infinity
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start thanos-receive
systemctl enable thanos-receive

Query配置

Query:提供了一个聚合多个 Prometheus 数据源的高可用查询界面。用户可以在单个查询中跨多个集群或实例执行查询,可以使用标准的 Prometheus PromQL 或 Thanos 扩展的 PromQL。

node1

为了方便Query节点动态添加,我们这边使用了consul的service, 并用模版文件生成动态发现文件。具体如下。

[root@Q-gz-common-prod-thanos-001 thanos]# pwd
/usr/local/thanos
[root@Q-gz-common-prod-thanos-001 thanos]# ls
compact  consul_token.yaml  cos_bucket.yaml  query  receive  sd.sh  store  thanos  thanos-sd-file.tpl  thanos-sd-file.yaml
[root@Q-gz-common-prod-thanos-001 thanos]# cat thanos-sd-file.tpl
- targets:
  {{ range service "thanos-query" -}}
  - {{ .Address }}:{{ .Port }}
  {{ end -}}

然后用 consul-template从consul的service读取并生成文件。

consul-template -consul-token-file=consul_token.yaml -consul-addr 127.0.0.1:8500 -template "/usr/local/thanos/thanos-sd-file.tpl:/usr/local/thanos/thanos-sd-file.yaml:echo ok" -once

cat > /etc/systemd/system/thanos-query.service <<EOF
[Unit]
Description=thanos-query
Documentation=https://thanos.io/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/thanos/thanos query \
          --http-address 0.0.0.0:10903 \
          --grpc-address 0.0.0.0:10904 \
          --store.sd-files=/usr/local/thanos/thanos-sd-file.yaml \
          --query.replica-label=replica \
          --log.level=info \
          --query.timeout=15m 
ExecReload=/bin/kill -HUP 
TimeoutStopSec=20s
Restart=always
LimitNOFILE=10240
LimitNPROC=10240
LimitCORE=infinity
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable thanos-query
systemctl start thanos-query

Compact配置

Compact:负责在长期存储中执行压缩和降采样操作,以减少存储空间和查询时间。

注意:压缩器必须作为单例运行,并且不能在手动修改存储桶中的数据时运行 --wait 让 Compact 一直运行,轮询新数据来做压缩和降采样。 --retention.resolution-raw 指定原始数据存放时长,--retention.resolution-5m 指定降采样到数据点 5 分钟间隔的数据存放时长,--retention.resolution-1h 指定降采样到数据点 1 小时间隔的数据存放时长,它们的数据精细程度递减,占用的存储空间也是递减,通常建议它们的存放时间递增配置 (一般只有比较新的数据才会放大看,久远的数据通常只会使用大时间范围查询来看个大致,所以建议将精细程度低的数据存放更长时间)

node3

cat > /etc/systemd/system/thanos-compact.service <<EOF
[Unit]
Description=thanos-compact
Documentation=https://thanos.io/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/thanos/thanos compact \
          --data-dir=/data/thanos/compact \
          --objstore.config-file=/usr/local/thanos/cos_bucket.yaml \
          --http-address 0.0.0.0:19193 \
          --debug.accept-malformed-index \
          --retention.resolution-raw=90d \
          --retention.resolution-5m=180d \
          --retention.resolution-1h=360d \
          --log.level=info \
          --wait
ExecReload=/bin/kill -HUP 
TimeoutStopSec=20s
Restart=always
LimitNOFILE=10240
LimitNPROC=10240
LimitCORE=infinity
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start thanos-compact
systemctl enable thanos-compact


举报

相关推荐

0 条评论