Prometheus
Prometheus是用于事件监控和警告一个免费的软件应用程序。
安装+启动
下载Prometheus:https://prometheus.io/download/
wget https://github.com/prometheus/prometheus/releases/download/v2.22.0/prometheus-2.22.0.linux-amd64.tar.gz
extract prometheus-2.22.0.linux-amd64.tar.gz
cd prometheus-2.22.0.linux-amd64
./prometheus --config.file=prometheus.yml # 配置文件
浏览器打开: http://localhost:9090/graph
Nodexporter
下载Nodexporter(用来采集主机系统信息)
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
extract node_exporter-1.0.1.linux-amd64.tar.gz
cd node_exporter-1.0.1.linux-amd64
./node_exporter --web.listen-address 127.0.0.1:8080 #设置启动IP地址和端口
修改prometheus配置文件prometheus.yml,从Nodexporter拉取数据:
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: 'node'
static_configs:
- targets: ['localhost:8080']
重新启动prometheus可以看到如下界面:
PromQL
接下来使用PromQL语言查询数据。
node_load1
:过去1分钟的CPU负载
node_memory_Active_bytes
:正在使用的内存
rate(node_cpu_seconds_total{cpu="2"}[1m])
:2号CPU使用情况
Grafana
Grafana是一个数据可视化工具。
安装+启动
下载grafana: https://grafana.com/grafana/download
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_7.2.1_amd64.deb
sudo dpkg -i grafana_7.2.1_amd64.deb
启动:sudo systemctl start grafana-server
设置开机自动启动:
sudo update-rc.d grafana-server defaults
浏览器打开:http://localhost:3000 (Grafana默认端口是3000)
初始用户和密码都是`admin登陆,修改密码。
设置数据源
这里选Prometheus这个数据库。
然后设置名字和URL(URL设置为Prometheus的IP+端口),保存就可以了(之后我们会从这个数据源拉取数据进行展示)。
创建dashboard和pannel
简单来说,dashboard可以拥有多个pannel,每个pannel可以展示一个图。
然后设置pannel的数据:先选择我们刚刚设置的数据源(Prometheus-1),然后在Metrics
这里输入PromQL
语法获取数据即可。
设置完成,点击右上角的Apply
进行保存,就可以看到如下界面: