Prometheus入门使用(一)

本文详细介绍了如何部署Prometheus服务器和配置监控目标,包括启动Prometheus、安装node_exporter以监控远程主机。接着,通过添加Grafana图形界面,展示了如何安装和启动Grafana,以及如何通过Grafana直观展示Prometheus收集的数据。整个过程旨在建立一套完整的监控解决方案。
摘要由CSDN通过智能技术生成

Prometheus入门使用(一)

Prometheus简介:

部署Prometheus Server:

1.下载Prometheus:
root@dokcer:~# wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz

2.解压Prometheus:
root@dokcer:~# tar -xzvf prometheus-2.37.0.linux-amd64.tar.gz -C /usr/local/

3.建立软连接:
root@dokcer:~# ln -sv /usr/local/prometheus-2.37.0.linux-amd64/ /usr/local/prometheus

4.启动Prometheus,启用默认配置文件:
root@dokcer:~# /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml & 
& 表示命令在后台运行

5.如果出现端口占用情况,先确认9090端口是否被占用:
root@dokcer:~# netstat -tulp | grep 9090   #端口9090已经开启被prometheus占用
tcp6       0      0 [::]:9090               [::]:*                  LISTEN      27047/prometheus

root@dokcer:~# lsof -i:9090
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
prometheu 27047 root    3u  IPv6  58459      0t0  TCP dokcer:9090->192.168.0.5:60420 (ESTABLISHED)
prometheu 27047 root    7u  IPv6  58455      0t0  TCP *:9090 (LISTEN)

root@dokcer:~# ss -natlp | grep 9090
LISTEN   0         128                       *:9090                   *:*        users:(("prometheus",pid=27047,fd=7))

浏览器查看prometheus界面:

访问地址http://:IP:9090

https://s2.loli.net/2022/07/16/RYCdXPWMbeUtOc7.png

查看监控的机器:

http://IP:9090/targets

https://s2.loli.net/2022/07/15/ALwK5xiaXtoEqkd.png

目前只有本地的机器被监控,访问http://IP:9090/metrics,就可以看到被监控机器的各种数据

https://s2.loli.net/2022/07/15/fYp3tSKI1yaBlig.png

输入表达式进行数据的过滤,并可以选择使用图形的形式来查看数据

https://s2.loli.net/2022/07/15/gykuVlabMUWSitm.png

部署被监控机器agent:

被监控的远程主机只需要安装node_exporter组件,就可以通过http://被监控机器IP:9100/metrics,查看到被监控机器的数据指标(cpu,内存,网络)。

1.下载node_exporter:
root@dokcer:**~**# wget  https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz

2.解压到/usr/local/
root@dokcer:~# tar -xzvf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/

3.创建软连接:
root@dokcer:~# ln -sv /usr/local/node_exporter-1.3.1.linux-amd64/node_exporter /usr/local/bin/node_exporter
'/usr/local/bin/node_exporter' -> '/usr/local/node_exporter-1.3.1.linux-amd64/node_exporter'

4.运行node_exporter
root@dokcer:~# nohup node_exporter &  #用nohup后台运行node_exporter
root@dokcer:~# nohup node_exporter &
[1] 27954
root@dokcer:~# jobs -l                #查看当前终端的nohup运行的命令,其他的**supervisord**,**screen**命令也可以
[1]+ 27954 Running                 nohup node_exporter &
root@dokcer:~# nohup: ignoring input and appending output to 'nohup.out'  #可以看到后台运行命令的输出信息

5.查看端口占用状态:
root@dokcer:~# netstat -tulp  | grep 9100                #服务开启成功
tcp6       0      0 [::]:9100               [::]:*                  LISTEN      27954/node_exporter 
root@dokcer:~# lsof -i:9100
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node_expo 27954 root    3u  IPv6  63234      0t0  TCP *:9100 (LISTEN)
root@dokcer:~# ss -natlp | grep 9100
LISTEN   0         128                       *:9100                   *:*        users:(("node_exporter",pid=27954,fd=3))

6.prometheus server配置文件添加被控端:
编辑/usr/local/prometheus/prometheus.yml
文件添加内容:

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "agent"
    static_configs:
      - targets: ["192.168.0.50:9100"]
~

7.重启prometheus server
root@dokcer:~# pkill prometheus  #关闭prometheus server
root@dokcer:~# lsof -i:9090      #查看端口状态
root@dokcer:~# /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml &

打开http://IP:9090 → Status → Targets, 就可以看到被添加上的被监控的主机:

https://s2.loli.net/2022/07/16/KasNSdBYZPW7uXe.png

部署Prometheus图形显示界面Grafana

Grafana可以使用web界面直观显示出Prometheus所监控的机器的数据指标:

1.添加apt源:
root@dokcer:~# wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
OK
root@dokcer:~# echo "deb https://packages.grafana.com/enterprise/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

root@dokcer:~# sudo apt-get update
root@dokcer:~# sudo apt-get install grafana-enterprise

root@dokcer:~# dpkg -l gra*                #查看是否安装成功
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                           Version              Architecture         Description
+++-==============================-====================-====================-=================================================================
un  grafana                        <none>               <none>               (no description available)
ii  grafana-enterprise             9.0.3                amd64                Grafana

2.启动grafana server
root@dokcer:~# systemctl start grafana-server
root@dokcer:~# lsof -i:3000
COMMAND     PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
grafana-s 29257 grafana    8u  IPv6  68427      0t0  TCP *:3000 (LISTEN)

root@dokcer:~# sudo systemctl status grafana-server  #查看grafana server是否已经启动

root@dokcer:~# systemctl enable grafana-server.service  #设置开机自启
Synchronizing state of grafana-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /usr/lib/systemd/system/grafana-server.service.
root@dokcer:~# systemctl is-enabled grafana-server    #服务已经开机自启
enabled

浏览器访问http://IP:3000就可以看到grafana-server界面:

https://s2.loli.net/2022/07/16/CxzgiUhkXdwOHfa.png

https://s2.loli.net/2022/07/16/hqtWoxauVImelsK.png

https://s2.loli.net/2022/07/16/bDYpKXfBk6Qjvcw.png

https://s2.loli.net/2022/07/16/SrhYvlXpOFxmHj6.png

https://s2.loli.net/2022/07/16/1lyFm4sugTH6kin.png

https://s2.loli.net/2022/07/16/dHY1ZBG3C7Xerbu.png

https://s2.loli.net/2022/07/16/S94WONhlVsBwI5Z.png

https://s2.loli.net/2022/07/16/mQMBDR5tqVKb7go.png

https://s2.loli.net/2022/07/16/RaqCAV1TwulLcn5.png

已经可以显示出采集到的数据了:

https://s2.loli.net/2022/07/16/FIPHMLEfX94ASmv.png

可以自定义查询和显示采集到的数据:

https://s2.loli.net/2022/07/16/94KzQVYliAEkaS8.png

带可以选择导入现成的模板文件导入,这样就不用自己定义视图了,方便快捷:

https://s2.loli.net/2022/07/16/dIeZF16VWMUGXKA.png

上传模板json文件:

https://s2.loli.net/2022/07/16/bU1Zawqcg75hyLR.png

显示效果:

https://s2.loli.net/2022/07/16/d3uMBtobz6jPG2E.png

可以Ctrl +/-调整浏览器缩放以达到设置显示面板大小的作用

参考资料:

https://yunlzheng.gitbook.io/prometheus-book/parti-prometheus-ji-chu/quickstart

https://grafana.com/docs/grafana/next/setup-grafana/installation/debian/#install-from-apt-repository

https://grafana.com/orgs/starsliao/dashboards

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值