linux学习

Prometheus的安装与使用

相关软件包:

https://xianyijitan.top/wp-blog/typora-user-file/prometheus软件包.rar

Prometheus的官网下载地址:https://prometheus.io/download/

一、Prometheus的安装

  1. 解压安装prometheus
[root@master opt]# ls -lh
总用量 87M
-rw-r--r--. 1 root root 87M 7月  19 17:21 prometheus-2.45.0.linux-amd64.tar.gz
[root@master opt]#

2.进入到 /opt/prometheus 目录启动prometheus

[root@master opt]# cd prometheus
[root@master prometheus]# ./prometheus
ts=2023-07-19T09:32:03.885Z caller=main.go:534 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2023-07-19T09:32:03.886Z caller=main.go:578 level=info msg="Starting Prometheus Server" mode=server version="(version=2.45.0, branch=HEAD, revision=8ef767e396bf8445f009f945b0162fd71827f445)"
ts=2023-07-19T09:32:03.887Z caller=main.go:583 level=info build_context="(go=go1.20.5, platform=linux/amd64, user=root@920118f645b7, date=20230623-1549, tags=netgo,builtinassets,stringlabels)"
ts=2023-07-19T09:32:03.887Z caller=main.go:584 level=info host_details="(Linux 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 2332 UTC 2018 x86_64 master (none))"
ts=2023-07-19T09:32:03.887Z caller=main.go:585 level=info fd_limits="(soft=4096, hard=4096)"
ts=2023-07-19T09:32:03.887Z caller=main.go:586 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2023-07-19T09:32:03.893Z caller=web.go:562 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090
ts=2023-07-19T09:32:03.896Z caller=main.go:1019 level=info msg="Starting TSDB ..."
ts=2023-07-19T09:32:03.901Z caller=tls_config.go:274 level=info component=web msg="Listening on" address=[::]:9090
ts=2023-07-19T09:32:03.901Z caller=tls_config.go:277 level=info component=web msg="TLS is disabled." http2=false address=[::]:9090
ts=2023-07-19T09:32:03.913Z caller=head.go:595 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
ts=2023-07-19T09:32:03.913Z caller=head.go:676 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=15.4µs
ts=2023-07-19T09:32:03.913Z caller=head.go:684 level=info component=tsdb msg="Replaying WAL, this may take a while"
ts=2023-07-19T09:32:03.914Z caller=head.go:755 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
ts=2023-07-19T09:32:03.914Z caller=head.go:792 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=41.9µs wal_replay_duration=1.1234ms wbl_replay_duration=11.2µs total_replay_duration=1.3374ms
ts=2023-07-19T09:32:03.916Z caller=main.go:1040 level=info fs_type=XFS_SUPER_MAGIC
ts=2023-07-19T09:32:03.916Z caller=main.go:1043 level=info msg="TSDB started"
ts=2023-07-19T09:32:03.916Z caller=main.go:1224 level=info msg="Loading configuration file" filename=prometheus.yml
ts=2023-07-19T09:32:03.928Z caller=main.go:1261 level=info msg="Completed loading of configuration file" filename=prometheus.yml totalDuration=11.5929ms db_storage=15.8µs remote_storage=19.2µs web_handler=10.7µs query_engine=14µs scrape=1.0045ms scrape_sd=52.4µs notify=42.7µs notify_sd=29.6µs rules=11.8µs tracing=32.6µs
ts=2023-07-19T09:32:03.928Z caller=main.go:1004 level=info msg="Server is ready to receive web requests."
ts=2023-07-19T09:32:03.928Z caller=manager.go:995 level=info component="rule manager" msg="Starting rule manager..."

3.待启动完成后,用以下地址直接无密码登录

http://192.168.11.235:9090/

如图所示:

img

4.为prometheus服务创建service并设置开机自动启动

vim /usr/lib/systemd/system/prometheus.service

编辑如下内容,根据自己实际的安装目录进行编辑

[Unit]
Description=prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target

使service生效并且设置开机自启动

systemctl daemon-reload
systemctl enable prometheus

5.grafana的安装

yum install -y grafana-enterprise-10.0.2-1.x86_64.rpm

待安装完毕后,启动grafana服务,查看到服务状态,并设置开机自启

systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
systemctl enable grafana-server

6.访问以下地址,并且配置相应的数据源

http://192.168.11.135:3000/login

7.登录界面如下所示,默认用户名及密码分别是admin,admin

img

​ 登录进入grafana系统后,对数据源进行配置,点击,add your first data source,进入添加数据源页面:

img

​ 进入后,点击Prometheus,进入配置页面;

img

​ 编辑HTTP下的URL,这里是主机IP是192.168.20.231,所以填:http://192.68.11.135:9090

然后拉倒最底部,保存

img

img

二、监控主机中安装node_exporter

1.对软件压缩包进行解压,并且将安装包放到自己系统安装目录下,我自己放在/usr/local/目录中

[root@master ruanjianfile]# tar -xzf node_exporter-1.6.1.linux-amd64.tar.gz
[root@master ruanjianfile]# mv node_exporter-1.6.1.linux-amd64 /usr/local/

2.将安装包中的可执行文件node_exporter拷贝到/usr/local/bin目录中

mv /usr/local/node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/

3.为node_exporter服务创建service服务

vim /usr/lib/systemd/system/node_exporter.service

[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target

4.为node_exporter设置自动启动并启动服务

systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter

5.监控主机中进行监控相关参数配置

vi编辑修改prometheus中相关配置文件;

主要在对应该控件软件安装目录下/opt/prometheus/prometheus.yml这个文件中;

在原文件的scrape_configs模块下增加如下配置内容:

  - job_name: 'master_prometheus'
    static_configs:
        - targets: ['192.168.11.135:9100']

修改后/opt/prometheus/prometheus.yml如下所示:

[root@master prometheus]# cat prometheus.yml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=` 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: 'master_prometheus'
    static_configs:
        - targets: ['192.168.11.135:9100']

检查配置是否正确:

[root@master prometheus]# /opt/prometheus/promtool check config /opt/prometheus/prometheus.yml
Checking /opt/prometheus/prometheus.yml
 SUCCESS: /opt/prometheus/prometheus.yml is valid prometheus config file syntax

[root@master prometheus]#

重启prometheus服务进行相关测试

systemctl restart prometheus

打开如下测试地址

http://192.168.11.135:9090/targets

img

6.grafana中添加配置监控模板

如图点击“+”号后,选择import dashboard

img

如下图,搜索系统默认模板8919,然后点击Load

img

点Load后,稍等会,grafana会直接从官方网站导入编号为8919的面板如下图所示:

img

选择数据源Prometheus-1,点击“Import”,显示master和node1监控界面显示如下图所示:

img

留言

您的邮箱地址不会被公开。 必填项已用 * 标注