prometheus是一个开源指标监控解决方案,指标就是指的CPU的使用率、内存使用率等数据。前端
这里直接粘贴官网的架构图:
linux
这里采用docker的方式来安装,若是须要使用其余方式的,能够参考官网。nginx
我的使用的配置文件:web
# 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: '/webUI/prometheus/metrics' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: 'example' metrics_path: '/example/metrics' static_configs: - targets: ['example.com']
Prometheus的配置文件使用的yaml格式,若是对yaml不熟悉的能够搜一下相关的资料了解一下;这里先简单介绍一下其中的scrape_configs下的job,job就是Prometheus会定时从它配置的target抓取数据;这个示例中配置了两个job,第一个是Prometheus本身的metrics,第二个是一个示例;这里须要着重介绍一下metrics_path,它的做用是指定抓取metrics数据的路径,默认是targets+'/metrics',若是咱们的metrics路径不是这个,那么就须要经过该参数额外指定。docker
参考:https://prometheus.io/docs/prometheus/latest/installation/#using-docker网络
docker run \ -p 9090:9090 \ -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus
这个命令是前端运行的方式启动的,能够用来一开始作测试使用,一旦当前窗口关闭,则该docker容器也会被关闭掉。架构
docker run \ -d \ --name prometheus \ --network host \ -v /data1/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus --web.listen-address="127.0.0.1:9090" --web.external-url=http://localhost:9090/webUI/prometheus/ --config.file=/etc/prometheus/prometheus.yml
我的推荐使用这个命令,这里简单介绍一个和官网示例不一样的地方:app
执行完上面的命令后使用docker logs prometheus
查看一下日志,若是是正确运行了,日志会像下面这样:curl
level=info ts=2020-12-25T11:07:34.247Z caller=main.go:322 msg="No time or size retention was set so using the default time retention" duration=15d level=info ts=2020-12-25T11:07:34.247Z caller=main.go:360 msg="Starting Prometheus" version="(version=2.23.0, branch=HEAD, revision=26d89b4b0776fe4cd5a3656dfa520f119a375273)" level=info ts=2020-12-25T11:07:34.247Z caller=main.go:365 build_context="(go=go1.15.5, user=root@37609b3a0a21, date=20201126-10:56:17)" level=info ts=2020-12-25T11:07:34.247Z caller=main.go:366 host_details="(Linux 3.10.107-1-tlinux2-0048 #1 SMP Wed Feb 27 14:30:34 CST 2019 x86_64 CMS-154864507 (none))" level=info ts=2020-12-25T11:07:34.247Z caller=main.go:367 fd_limits="(soft=1048576, hard=1048576)" level=info ts=2020-12-25T11:07:34.247Z caller=main.go:368 vm_limits="(soft=unlimited, hard=unlimited)" level=info ts=2020-12-25T11:07:34.250Z caller=main.go:722 msg="Starting TSDB ..." level=info ts=2020-12-25T11:07:34.250Z caller=web.go:528 component=web msg="Start listening for connections" address=127.0.0.1:9090 level=info ts=2020-12-25T11:07:34.250Z caller=web.go:550 component=web msg="Router prefix" prefix=/webUI/prometheus level=info ts=2020-12-25T11:07:34.255Z caller=head.go:645 component=tsdb msg="Replaying on-disk memory mappable chunks if any" level=info ts=2020-12-25T11:07:34.255Z caller=head.go:659 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=8.196µs level=info ts=2020-12-25T11:07:34.255Z caller=head.go:665 component=tsdb msg="Replaying WAL, this may take a while" level=info ts=2020-12-25T11:07:34.256Z caller=head.go:717 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0 level=info ts=2020-12-25T11:07:34.256Z caller=head.go:722 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=28.503µs wal_replay_duration=1.160856ms total_replay_duration=1.217683ms level=info ts=2020-12-25T11:07:34.258Z caller=main.go:742 fs_type=EXT4_SUPER_MAGIC level=info ts=2020-12-25T11:07:34.258Z caller=main.go:745 msg="TSDB started" level=info ts=2020-12-25T11:07:34.258Z caller=main.go:871 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml level=info ts=2020-12-25T11:07:34.258Z caller=main.go:902 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=873.002µs remote_storage=6.228µs web_handler=850ns query_engine=2.311µs scrape=351.488µs scrape_sd=50.71µs notify=22.741µs notify_sd=10.615µs rules=2.97µs level=info ts=2020-12-25T11:07:34.258Z caller=main.go:694 msg="Server is ready to receive web requests."
而后执行curl curl 127.0.0.1:9090/webUI/prometheus/metrics/
,正常状况下会展现Prometheus默认的metrics数据,这里仅展现前几行:测试
# TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 9.755e-05 go_gc_duration_seconds{quantile="0.25"} 0.000129779 go_gc_duration_seconds{quantile="0.5"} 0.000154338 go_gc_duration_seconds{quantile="0.75"} 0.000199801 go_gc_duration_seconds{quantile="1"} 0.000343164 go_gc_duration_seconds_sum 0.008667863 go_gc_duration_seconds_count 50 # HELP go_goroutines Number of goroutines that currently exist. # TYPE go_goroutines gauge go_goroutines 34 # HELP go_info Information about the Go environment. # TYPE go_info gauge go_info{version="go1.15.5"} 1 # HELP go_memstats_alloc_bytes Number of bytes allocated and still in use. # TYPE go_memstats_alloc_bytes gauge go_memstats_alloc_bytes 3.2321096e+07 # 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 2.79534016e+08
我的建议经过nginx作转发来实现Prometheus的web页面访问,具体配置以下:
location ^~ /webUI/prometheus/ { proxy_pass http://127.0.0.1:9090/webUI/prometheus/; }
而后执行sbin/nginx -c config/nginx.conf -s reload
使新增的路由生效,而后就能够经过www.example.com/webUI/prometheus/
来查看Prometheus的web页面了,以下图所示:
Prometheus的界面比较简洁,这里简单介绍一下如何根据Prometheus提供的goroutine的个数变化配置一个图表:
Prometheus提供的可视化图表比较简单,所以这里引入grafana做为metrics的可视化展现界面,下面仍然使用goroutine的个数变化展现如何在grafana配置一个图表:
到这里就把Prometheus的部署介绍完了,Prometheus的更高级用法以及具体实践,例如生产环境如何在代码中使用这些会留待后续介绍。