一:安装prometheus
1.服务器下载文件
wget [https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz](https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz)
我是下载到/xww目录下 (要记住下载和操作的位置),进行解压
#解压下载包
tar -zxvf prometheus-2.8.1.linux-amd64.tar.gz
#重命名下载包
mv prometheus-2.8.1.linux-amd64/ prometheus
2.进入prometheus文件下,到修改prometheus.yml文件,确定启动ip
# 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'.
scrape_interval: 5s
static_configs:
- targets: ['your ip:9090']#这里需要填写监控的ip
labels:
instance: prometheus
- job_name: 'node_exporter'
scrape_interval: 10s
static_configs:
- targets: ['your ip:9100']#这里需要填写监控的ip
labels:
instance: nodel
修改后进行保存,启动prometheus
./prometheus
3.输入命令,添加用户,用账号启动服务
groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
4.赋权和创建prometheus运行数据目录
chown -R prometheus:prometheus /xww/prometheus/
mkdir -p /xww/prometheus-data
chown -R prometheus:prometheus /xww/prometheus-data
5.设置开机启动,创建并且修改prometheus.service文件
touch /usr/lib/systemd/system/prometheus.service
chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
# Type设置为notify时,服务会不断重启
Type=simple
User=prometheus
# --storage.tsdb.path是可选项,默认数据目录在运行目录的./dada目录中
ExecStart=/xww/prometheus/prometheus --config.file=/xww/prometheus/prometheus.yml --storage.tsdb.path=/xww/prometheus-data
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动prometheus
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus #查看服务状态
6.浏览器访问your ip:9090
二:部署node_exporter
#下载
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
#解压
tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz
#重命名
mv node_exporter-1.0.1.linux-amd64 /usr/local/node_exporter
#创建用户赋予权限
groupadd prometheus
useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus
chown prometheus.prometheus -R /xww/node_exporter
创建修改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=prometheus
ExecStart=/xww/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动
systemctl enable node_exporter
systemctl start node_exporter
备注:我之前这样操作没有把我的node_exporter,我是进入到node_exporter包内,输入 ./node_exporter才启动的