Prometheus 学习笔记1 - 安装 Prometheus 和 Linux Exporter教程

2021-07-08 07:51:32

Prometheus是一个很强大的监控工具,尤其适合于基于时间流的各种metric的日志,可以用来监控Linux,Windows节点,各种应用程序,比如apache,docker,kubernetes等容器编排工具等等。 他的安装和配置相对来说还是比较直观容易的。下面看一个简单的例子,安装一个Prometheus服务器,并且监控一个Linux的节点服务器性能

安装Prometheus

首先新建一个用户

<pre class="language-c">```
sudo useradd -M -r -s /bin/false prometheus

新建一个目录

<pre class="language-c">```
sudo mkdir /etc/prometheus /var/lib/prometheus

下载二进制文件并解压

<pre class="language-c">```
wget https://github.com/prometheus/prometheus/releases/download/v2.16.0/prometheus-2.16.0.linux-amd64.tar.gz


tar xzf prometheus-2.16.0.linux-amd64.tar.gz prometheus-2.16.0.linux-amd64/

拷贝二进制文件到对应的目录并修改权限

<pre class="language-c">```
sudo cp prometheus-2.16.0.linux-amd64/{prometheus,promtool} /usr/local/bin/
sudo chown prometheus:prometheus /usr/local/bin/{prometheus,promtool}
sudo cp -r prometheus-2.16.0.linux-amd64/{consoles,console_libraries} /etc/prometheus/
sudo cp prometheus-2.16.0.linux-amd64/prometheus.yml /etc/prometheus/prometheus.yml
sudo chown -R prometheus:prometheus /etc/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus

测试一下是否成功

<pre class="language-c">```
prometheus --config.file=/etc/prometheus/prometheus.yml

如果成功,我们可以看见 "Server is ready to receive web requests." 按住Ctrl+C 退出。 配置一下systemd的daemon 服务进程,这样可以控制开机自动后台启动

<pre class="language-c">```
sudo vi /etc/systemd/system/prometheus.service

输入下面的内容

<pre class="language-c">```
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target

保存退出 然后重新加载systemd,启动服务

<pre class="language-c">```
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
sudo systemctl status prometheus

查看是否成功

<pre class="language-c">```
curl localhost:9090

结果应该显示 Found. 浏览器里输入 http://<IP address>:9090 就可以看见我们的web界面了

安装Node Exporter

安装好Prometheus 以后,我们需要在被监控端安装对应的Exporter,这个Exporter会搜集数据并提交给Prometheus。下面看看如何在一个Ubuntu的服务器上安装Exporter 首先登陆到Ubuntu 服务器,创建用户,下载Exporter,解压,并将二进制文件拷到我们的/usr/local/bin目录下,修改权限

<pre class="language-c">```
[cloud_user@limedrop]$ sudo useradd -M -r -s /bin/false node_exporter
[cloud_user@limedrop]$ wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
[cloud_user@limedrop]$ tar xvfz node_exporter-0.18.1.linux-amd64.tar.gz
[cloud_user@limedrop]$ sudo cp node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin/
[cloud_user@limedrop]$ sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

和Prometheus类似,我也创建一个systemd的服务

<pre class="language-c">```
[cloud_user@limedrop]$ sudo vi /etc/systemd/system/node_exporter.service


[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

保存退出之后,启动服务并且查看内容

<pre class="language-c">```
[cloud_user@limedrop]$ sudo systemctl daemon-reload
[cloud_user@limedrop]$ sudo systemctl start node_exporter
[cloud_user@limedrop_web]$ sudo systemctl enable node_exporter
[cloud_user@limedrop]$ curl localhost:9100/metrics

然后登陆我们的Prometheus 服务器,配置prometheus.yml文件

<pre class="language-c">```
[cloud_user@prometheus]$ sudo vi /etc/prometheus/prometheus.yml

添加下面的job和target

<pre class="language-c">```
...
  - job_name: 'LimeDrop Web Server'
    static_configs:
    - targets: ['10.0.1.102:9100']
...

重启prometheus服务

<pre class="language-c">```
[cloud_user@prometheus]$ sudo systemctl restart prometheus

即可在界面进行查询Linux node的metric信息

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »