searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

prometheus生产环境落地实践一:部署prometheus

2024-07-01 03:27:02
0
0

1. 监控软件选型

市面上有不少监控软件,关于prometheus与其它监控软件的区别网上有很多资料,这里不再赘述,只简单说一下当时为什么选择了prometheus:

  • prometheus是开源软件,免费使用。
  • prometheus社区生态比较好,在国内应用广泛,网上有很多资料。
  • prometheus拥有众多exporter,基本覆盖公司内使用的数据库与中间件产品。
  • 公司有把业务系统迁移到k8s上的打算,prometheus天然适配k8s。

2. prometheus主机资源选择

prometheus需要的主机规格与较多因素有关,如采集样本数,采集频率、数据保留时间等,要根据实际需求进行评估。磁盘建议选择高IO的SSD盘,我们初期选用了IO较低的机械盘,结果导致监控数据查询很慢,grafana上的图要很久才能刷新出来。

3. prometheus安装包下载

  1. 官网:https://prometheus.io/download/
  2. github:https://github.com/prometheus/prometheus

4. 部署prometheus

  1. 解压安装包:

    tar -xvf prometheus-2.xx.x.linux-amd64.tar.gz
    mv prometheus-2.xx.x.linux-amd64 /app/prometheus
    
  2. prometheus支持的启动参数可以通过 ./prometheus --help获取,我们主要关注以下几个启动参数:

    # 指定prometheus配置文件:
    --config.file
    
    # tsdb数据存储路径:
    --storage.tsdb.path
    
    # tsdb数据存储时间:
    --storage.tsdb.retention.time
    
    # tsdb数据存储大小:
    --storage.tsdb.retention.size
    
    # prometheus的web地址
    --web.listen-address
    
    # 管理控制操作启用API端点
    --web.enable-admin-api
    
    # 启用是否通过HTTP请求重新加载
    --web.enable-lifecycle
    
    # 日志等级
    --log.level=info
    
  3. promtool提供检查配置文件的功能

    ./promtool check config prometheus.yml
    
  4. 另外prometheus还提供了reload接口,使我们在更改配置文件后不必重启prometheus就可将更改内容应用。

    curl -X POST http://127.0.0.1:9090/-/reload
    
  5. 基于以上知识点,我们创建以下prometheus.service文件

    vi /usr/lib/systemd/system/prometheus.service
    
    
    [Unit]
    Description=Prometheus
    After=network.target
    
    [Service]
    Type=simple
    User=root
    ExecStartPre=/app/prometheus/promtool check config /app/prometheus/prometheus.yml
    ExecStart=/app/prometheus/prometheus \
    --config.file=/app/prometheus/prometheus.yml \
    --storage.tsdb.path=/app/prometheus/data \
    --storage.tsdb.retention.time=30d \
    --web.listen-address=0.0.0.0:9090 \
    --web.enable-admin-api \
    --web.enable-lifecycle \
    --log.level=info
    ExecReload=/bin/curl -X POST http://127.0.0.1:9090/-/reload
    TimeoutStopSec=20s
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    
  6. 如果公司禁止使用root用户启动应用,那这里要把User改成其它用户,如prometheus普通用户,并且要将相关目录授权给prometheus普通用户。

    chown -R prometheus:prometheus /app/prometheus/
    
  7. 启动prometheus服务

    systemctl daemon-reload
    systemctl enable --now prometheus.service
    systemctl status prometheus.service
    
  8. 如果prometheus服务启动失败,可以使用以下命令检查失败原因:

    journalctl -u prometheus.service
    
  9. prometheus服务正常启动后,我们就可以通过浏览器访问prometheus了。

    http://IP:9090/
    
0条评论
0 / 1000
范****强
1文章数
0粉丝数
范****强
1 文章 | 0 粉丝