vlambda博客
学习文章列表

Mac下更新brew及安装Prometheus+Grafana

一 背景

   近来在做容器研究,本地也打算做一次基于原生k8s的动态扩容。k8s提供了基于cpu和内存的扩容方案,但如果需要根据其他规则,例如接口访问次数等等,就需要其他的一些辅助工具,例如监控:prometheus。本篇先准备好相关环境,并了解Prometheus和Grafana的安装和基本配置使用方式。

二 brew重新安装

尝试安装监控工具prometheus的时候,由于换了自己的旧电脑,发现brew install失败,报错信息如下:

当然,已经尝试过了各种切换brew源,并试图更新,但发现网上搜到的都不好用,无法解决问题。所以干脆执行卸载,然后重新安装。结果发现,连一个可用的卸载脚本都不好找了。。

例如这个:

 
   
   
 
/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"

在本地执行的时候,就会给出提示,这已经是很旧的下载版本,目前已被废弃,新版脚本使用Bash重写。

 
   
   
 
lijingyong:Downloads flamingskys$ /usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall)"
Warning: Ruby版本Homebrew安装脚本已被废弃,新版脚本使用Bash重写。
请使用以下命令:
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/uninstall.sh)"

不过好在还是执行成功了。。。

接下来就是重新安装:

 
   
   
 
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

最新的安装命令确实跟以前有所不同,多了很多提示信息,并可以直接在命令行根据提示指定下载源,在不必自己重新设置的条件下,速度快了很多:

Mac下更新brew及安装Prometheus+Grafana

目前支持5个下载源:

1、中科大下载源 2、清华大学下载源 3、北京外国语大学下载源  4、腾讯下载源(不推荐) 5、阿里巴巴下载源(不推荐 缺少cask源)

前三个是推荐的,【腾讯和阿里巴巴的不被推荐】。所以我们按照顺序,使用了中科大的下载源。

三 安装prometheus

   在brew准备好之后,这两个工具的安装就简单很多。基本上参照这篇文章:Mac部署Prometheus + Grafana监控 操作即可。

在本地的实际操作步骤要比上文中提到的简单很多,无需自己设置权限和配置prometheus.yml。直接brew install prometheus即可:

Downloads flamingskys$ brew search prometheus==> Formulaeprometheus prometheus-cppDownloads flamingskys$ brew install prometheus==> Downloading https://homebrew.bintray.com/bottles/prometheus-2.25.2.mojave.bottle.tar.gz==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/1e69783408d0ca19f63ad156fc9ab765831fc88fe9576b9f15b4a3959d4ed64f?re########### 16.5%########### 16.7%############ 16.7%######################################################################## 100.0%==> Pouring prometheus-2.25.2.mojave.bottle.tar.gz==> CaveatsWhen run from `brew services`, `prometheus` is run from`prometheus_brew_services` and uses the flags in: /usr/local/etc/prometheus.argsTo have launchd start prometheus now and restart at login: brew services start prometheusOr, if you don't want/need a background service you can just run: prometheus --config.file=/usr/local/etc/prometheus.yml==> Summary🍺 /usr/local/Cellar/prometheus/2.25.2: 21 files, 156.4MB


 
   
   
 

在上面输出的安装信息中,已经给出了两种启动方式:

Mac下更新brew及安装Prometheus+Grafana



非service方式,需要指定配置文件,/usr/local/etc/prometheus.yml,vi查看内容:

Mac下更新brew及安装Prometheus+Grafana

从而可知,prometheus的默认启动端口为9090,启动后,在浏览器中通过http://localhost:9090 即可查看服务。

Mac下更新brew及安装Prometheus+Grafana

Status=>Targets下可以查看主机:

Mac下更新brew及安装Prometheus+Grafana



四 安装grafana

4.1 安装&启动

   直接brew安装:brew install grafana,当前安装的版本是:Version 7.5.1,执行命令后可以看到如下输出信息:

Downloads flamingskys$ brew install grafana==> Downloading https://homebrew.bintray.com/bottles/grafana-7.5.1.mojave.bottle.tar.gz==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/496292602ececfd9f979ece1b5d744ec7198bfa318a49657299be7708d46cf2a?re######################################################################## 100.0%==> Pouring grafana-7.5.1.mojave.bottle.tar.gz==> CaveatsTo have launchd start grafana now and restart at login: brew services start grafanaOr, if you don't want/need a background service you can just run: grafana-server --config=/usr/local/etc/grafana/grafana.ini --homepath /usr/local/share/grafana --packaging=brew cfg:default.paths.logs=/usr/local/var/log/grafana cfg:default.paths.data=/usr/local/var/lib/grafana cfg:default.paths.plugins=/usr/local/var/lib/grafana/plugins==> Summary🍺 /usr/local/Cellar/grafana/7.5.1: 2,663 files, 179.5MB

可见也是两种启动服务方式:

 
   
   
 
brew services start grafana

grafana-server --config=/usr/local/etc/grafana/grafana.ini --homepath /usr/local/share/grafana --packaging=brew cfg:default.paths.logs=/usr/local/var/log/grafana cfg:default.paths.data=/usr/local/var/lib/grafana cfg:default.paths.plugins=/usr/local/var/lib/grafana/plugins

4.2 查看配置

grafana的配置文件默认是: /usr/local/etc/grafana/grafana.ini,查看内容:

Mac下更新brew及安装Prometheus+Grafana

内容较多,所以这里不再贴出。其中server部分,指定了http协议的访问端口,为3000,所以通过http://localhost:3000 访问服务:

Mac下更新brew及安装Prometheus+Grafana

4.3 登录

grafana默认的用户名和密码都是admin,可以输入执行登录。如果顺利的话,可以进入首页如下图所示:

Mac下更新brew及安装Prometheus+Grafana



4.4 配置数据源为prometheus

上面的首页截图中,可以看到有DATA SOURCES配置,我们点击这里,添加prometheus作为数据源:

Mac下更新brew及安装Prometheus+Grafana

进入配置详情页:

Mac下更新brew及安装Prometheus+Grafana



在HTTP=>URL这一栏,填写http://localhost:9090,点击底部的Save & Test,即可保存成功:

Mac下更新brew及安装Prometheus+Grafana

4.5 创建Dashboard和Query

Mac下更新brew及安装Prometheus+Grafana

进入Dashboard(现在也叫panal),编辑Query:



前面提到,prometheus有默认对主机的监控,所以可以看到,在上面编辑query条件时,输入net_后下拉框自动弹出了目前已经进行了采集的指标列表,选择第一个之后就能够看到监控图表中动态展示的数据内容:



4.5 密码错误问题处理

   可参见下面这篇文章:Grafana重置admin密码方法。Grafana使用sqlite存储密码和一些配置数据,所以找到db文件,并使用sqlite命令修改密码即可,这里不再重述。