1、prometheus包部署、配置文件详解、暴露抓取自身指标
  XsWF2uh32RxR 2023年11月13日 30 0
部署Prometheus Server
官方站点:prometheus.io
[root@ubuntu2004 ~]#curl -LO https://github.com/prometheus/prometheus/releases/download/v2.40.2/prometheus-2.40.2.linux-amd64.tar.gz

[root@ubuntu2004 ~]#tar xf prometheus-2.40.2.linux-amd64.tar.gz -C /usr/local
[root@ubuntu2004 ~]#cd /usr/local

[root@ubuntu2004 local]#mv prometheus-2.40.2.linux-amd64 prometheus-2.40.2
[root@ubuntu2004 local]#ln -s /usr/local/prometheus-2.40.2/ /usr/local/prometheus

[root@ubuntu2004 local]#useradd -r -M prometheus

[root@ubuntu2004 local]#mkdir /usr/local/prometheus/data #创建存储时序数据的目录
[root@ubuntu2004 local]#chown -R prometheus.prometheus /usr/local/prometheus/

[root@ubuntu2004 local]#vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Monitoring system and time series database
Documentation=https://prometheus.io/docs/introduction/overview/

[Service]
Restart=always
User=prometheus
EnvironmentFile=-/etc/default/prometheus
ExecStart=/usr/local/prometheus/prometheus \
            --config.file=/usr/local/prometheus/prometheus.yml \
            --storage.tsdb.path=/usr/local/prometheus/data \
            --web.console.libraries=/usr/share/prometheus/console_libraries \
            --web.enable-lifecycle \
            $ARGS
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
SendSIGKILL=no
LimitNOFILE=8192

[Install]
WantedBy=multi-user.target

[root@ubuntu2004 local]#systemctl daemon-reload 
[root@ubuntu2004 local]#systemctl enable --now prometheus.service 
[root@ubuntu2004 local]#systemctl status prometheus.service
[root@ubuntu2004 local]#ss -ntlp
State  Recv-Q  Send-Q   Local Address:Port   Peer Address:Port Process                                    
LISTEN 0       4096     127.0.0.53%lo:53          0.0.0.0:*     users:(("systemd-resolve",pid=736,fd=13)) 
LISTEN 0       128            0.0.0.0:22          0.0.0.0:*     users:(("sshd",pid=769,fd=3))             
LISTEN 0       128               [::]:22             [::]:*     users:(("sshd",pid=769,fd=4))             
LISTEN 0       4096                 *:9090              *:*     users:(("prometheus",pid=1311,fd=7))

使用本节点的9090地址就可以进行访问了
进行解析:10.0.0.106 prometheus.fanchao.com
进行访问:http://prometheus.fanchao.com:9090/
peometheus默认配置文件
global:               #全局配置参数,对所有后续用到该参数的功能都生效
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.     
#抓取时间默认1分中,关键指标抓取频繁,非关键抓取时间可长一点
#如果没有在scrape_configs中定义抓取指标的对象没有指定多长时间抓一次,就在全局这里继承,15s抓一次
evluation_interval: 15s
#在rule_files中定义了很多的规则,会基于已抓取的样本数据做周期性的语句运行,运行结果会保存一个新的时间序列,每多长时间运行一次,如果没有定义他的周期,默认15s运行一次。

alerting:             #Prometheus自己如何生成告警信息,并把信息发给alertmanager

rule_files:           #规则文件,可以把复杂的查询语句写到配置文件中,让prometheus加载后周期性的执行,并把执行结果生成的时间序列保存下来,以后,再需要类似复杂语句生成结果值的时候,不需要实时生成,因为很慢,很影响性能,查询此前生成的时间序列就可以了。

定义抓取任务:
scrape_configs:       #抓取配置的关键所在,被抓取目标都在此定义,所有的抓取配置放在scrape_configs
  - job_name: "prometheus"  #每组相似应用,可定义成一个job,此示例为抓取prometheus自身
    
    # metrics_path defaults to '/metrics' #抓取指标的路径
    # scheme defaults to 'http'.          #抓取指标使用的协议
    如:metrics_path: /metrics
       scheme: http
       
    static_configs:    #静态指定,对应端的地址,也可以动态发现。
      - targets: ["localhost:9090"] #每一个Instance(实例),就是一个target(监控目标),有多少个监控目标,可用 - targets进行定义
其抓取prometheus自身指标的路径是 http://localhost:9090/metrics      
每一个作业有了job_name以后,对应的Instance之上就会自动添加一个对应的标签,'job=<job_name>'

示例:
    static_configs:    #静态指定,对应端的地址,也可以动态发现。
      - targets: ["localhost:9090"] #每一个Instance,就是一个target  
      #使用targets再分组表示定义出多个被监控对象,对于每个targets而言,指明地址和端口,除了地址和端口之外,可能基于某个特定路径暴露指标,默认是/metrics,如果不指,其实就是向"localhost:9090"地址加上一路径个'/metrics'路径来抓指标。
      - targets: ["localhost:9090"]没有指定协议,默认是http协议。协议有两种http和https
      
对于某一个job来言,需要使用特定的路径来抓取指标,需要使用metrics_path指定特定路径,使用scheme指定协议
以上配置文件定义了抓取的任务是抓取prometheus server自身指标
查看prometheus server自身指标暴露路径,可看到暴露的指标有哪些
localhost:9090/metrics
查看prometheus命令
[root@k8s-Master-01 ~]#cd /usr/local/prometheus
[root@k8s-Master-01 prometheus]#./prometheus --help
其中大部分配置选项可以满足我们的需要

--config.file="prometheus.yml"      #指定的配置文件
--web.listen-address="0.0.0.0:9090" #监听地址和端口
--storage.tsdb.path="data/"         #自己本地的持久化数据TSDB存放路径
--storage.tsdb.retention=STORAGE.TSDB.RETENTION #数据保存多长时间


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

XsWF2uh32RxR