在本文中,我们将介绍在Debian 10(Buster)操作系统上安装Prometheus和node_exporter,由于这是一种二进制安装方法,因此没有必要的依赖关系。Prometheus是一个免费的开源监控系统,可让你从任何目标系统收集时间序列数据指标,其Web界面使你能够执行强大的查询,可视化收集的数据以及配置警报。
一、创建Prometheus系统用户/组、配置和数据目录 1、创建Prometheus系统用户/组 我们将创建一个专门的Prometheus系统用户和组,-r或-system选项用于此目的: sudo groupadd --system prometheus sudo useradd -s /sbin/nologin --system -g prometheus prometheus 这创建了一个不需要/bin/bash shell的系统用户,这就是我们使用-s /sbin/nologin的原因。 2、创建配置和数据目录 Prometheus需要目录来存储数据和配置文件,使用以下命令创建所有必需的目录: sudo mkdir /var/lib/prometheus for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done
二、在Debian 10(Buster)上下载并安装Prometheus 让我们下载最新版本的Prometheus存档并将其解压缩以获取二进制文件,你可以从Prometheus发布的Github页面查看(地址:https://github.com/prometheus/prometheus/releases): sudo apt-get -y install wget mkdir -p /tmp/prometheus && cd /tmp/prometheus curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest \ | grep browser_download_url \ | grep linux-amd64 \ | cut -d '"' -f 4 \ | wget -qi - 提取文件: tar xvf prometheus*.tar.gz cd prometheus*/ 附:将prometheus二进制文件移动到/usr/local/bin/ 由于/usr/local/bin/在你的PATH中,让我们将二进制文件复制到它: sudo mv prometheus promtool /usr/local/bin/ 将prometheus配置模板移动到/etc目录: sudo mv prometheus.yml /etc/prometheus/prometheus.yml 还将控制台和console_libraries移动到/etc/prometheus目录: sudo mv consoles/ console_libraries/ /etc/prometheus/ cd ~/ rm -rf /tmp/prometheus 参考:在CentOS 7/Ubuntu 18.04系统上安装Prometheus服务器的方法。
三、创建/编辑Prometheus配置文件 Prometheus配置文件将位于/etc/prometheus/prometheus.yml下: sudo vim /etc/prometheus/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=<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']
四、创建Prometheus systemd服务单元文件 为了能够使用systemd管理Prometheus服务,你需要明确定义此单元文件: sudo tee /etc/systemd/system/prometheus.service<<EOF [Unit] Description=Prometheus Documentation=https://prometheus.io/docs/introduction/overview/ Wants=network-online.target After=network-online.target [Service] Type=simple User=prometheus Group=prometheus ExecReload=/bin/kill -HUP $MAINPID 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 \ --web.listen-address=0.0.0.0:9090 \ --web.external-url= SyslogIdentifier=prometheus Restart=always [Install] WantedBy=multi-user.target EOF 附1:更改目录权限 将这些目录的所有权更改为Prometheus用户和组: for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done sudo chown -R prometheus:prometheus /var/lib/prometheus/ 附2:重新加载systemd守护程序并启动该服务 sudo systemctl daemon-reload sudo systemctl start prometheus sudo systemctl enable prometheus 确认该服务正在运行: $ systemctl status prometheus
在URL地址:http://[ip_hostname]:9090上访问Prometheus Web界面,在访问地址上请自行改过自己的ip等参数:
五、在Debian 10 Buster上安装node_exporter 下载node_exporter存档: curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest \ | grep browser_download_url \ | grep linux-amd64 \ | cut -d '"' -f 4 \ | wget -qi - 提取下载的文件并将二进制文件移动到/usr/local/bin: tar -xvf node_exporter*.tar.gz cd node_exporter*/ sudo cp node_exporter /usr/local/bin 确认安装,运行node_exporter --version:
创建node_exporter服务: sudo tee /etc/systemd/system/node_exporter.service <<EOF [Unit] Description=Node Exporter Wants=network-online.target After=network-online.target [Service] User=prometheus ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=default.target EOF 重新加载systemd并启动服务: sudo systemctl daemon-reload sudo systemctl start node_exporter sudo systemctl enable node_exporter 确认状态: $ systemctl status node_exporter.service
确认要运行的服务后,让我们将node_exporter添加到Prometheus服务器: sudo vim /etc/prometheus.yml 在scrape_config部分下添加新job: - job_name: 'node_exporter' static_configs: - targets: ['localhost:9100'] 重启Prometheus: sudo systemctl restart prometheus 至此,安装配置完成。
相关主题 |