Prometheus环境搭建入门(docker)

一、前言

先放一张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. 启动容器

注意启动命令中的:roread 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/

prometheus web ui

5. 查看Targets

启动后,点击下面的选项:
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
    prometh
  • 填写URL,然后点击下面的Save & Test
    填写URL
  • 回到首页,选择新建仪表盘
    new dashboard
  • 选择刚才的数据源,以及查看的类型等,就可以看到效果了
    新建仪表盘
    grafana
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭建華

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值