文章目录
一、前言
先放一张Prometheus的架构图,有助于我们理解后续的操作。
Prometheus daemon负责定时去目标上抓取metrics指标数据,而且目标需要暴露一个http服务接口让Prometheus可以定时抓取;
exporter指的是输出监控组件信息的的HTTP接口;
Pushgateway指的是Client主动推送metrics到Pushgateway,而Prometheus定时去Pushgateway上抓取数据;
Alertmanager是独立于Prometheus的一个组件,提供报警功能,并支持Prometheus的查询语句;
Grafana之类的组件则是可视化工具。
推荐阅读博客:基于docker 搭建Prometheus+Grafana
二、启动node-exporter
1. 拉取镜像
docker pull prom/node-exporter:v1.1.2
2. 启动容器
注意启动命令中的:ro
是read only
只读的意思
docker run -d --name node-exporter -p 9100:9100 -v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro" -v "/:/rootfs:ro" prom/node-exporter:v1.1.2
3. 访问服务
http://192.168.56.101:9100/metrics
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 9
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.15.8"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 1.356784e+06
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 1.356784e+06
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 1.444915e+06
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 733
三、启动prometheus
1. 拉取镜像
docker pull prom/prometheus:v2.28.1
2. 准备配置文件
注意修改目标的IP地址。在这里读者可能有个疑问,下面- targets: ['172.17.0.2:9100']
中这个ip是怎么来的,读者可以通过下面的方式得到:
docker inspect node-exporter | grep 'IPAddress'
prometheus.yml
global:
scrape_interval: 60s
evaluation_interval: 60s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
labels:
instance: prometheus
- job_name: linux
static_configs:
- targets: ['172.17.0.2:9100']
labels:
instance: localhost
3. 启动容器
docker run -d --name prometheus -p 9090:9090 -v $(pwd)/mounts/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus:v2.28.1
4. 访问Web UI
http://192.168.56.101:9090/
5. 查看Targets
启动后,点击下面的选项:
如果prometheus刚启动,则会出现下面up异常的情况:
不必担心,过一会儿就好了,如下所示:
四、启动grafana
1. 拉取镜像
docker pull grafana/grafana:8.0.4
2. 更改挂载目录权限
mkdir grafana-storage
chmod a+rw grafana-storage/
如果不更改权限,后面就会报下面的错:
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
3. 启动容器
docker run -d --name grafana -p 3000:3000 -v $(pwd)/mounts/grafana-storage:/var/lib/grafana grafana/grafana:8.0.4
4. 访问Web UI
http://192.168.56.101:3000/
默认的用户名和密码都是admin
,登录后会让你重置密码,还可以输入admin
5. 使用grafana
- 点击添加数据源
- 选择类型为Prometheus
- 填写URL,然后点击下面的
Save & Test
- 回到首页,选择新建仪表盘
- 选择刚才的数据源,以及查看的类型等,就可以看到效果了