咱们都知道,Prometheus 是一个监控和可观察性的一个标准解决方案。这里呢,我也不许备去介绍 Prometheus 是什么,咱们直接切入到咱们主题。咱们怎么使用 Cortex 实现Prometheus 的多租户的管理的一个平台的搭建,下一章,主要讲咱们怎么使用 API 去实现对 Cortex 的管理控制。
<!--more-->linux
首先咱们下载好咱们的 Prometheus 以后,老夫下载的是: prometheus-2.13.0.linux-amd64.tar.gz . 解压到指定的目录:github
tar -zxvf prometheus-2.13.0.linux-amd64.tar.gz -C /opt/module
执行:shell
./prometheus
这样,一个简单的 Prometheus 服务就启动了。segmentfault
这里,当咱们须要监控某一个服务时,咱们须要在配置文件中进行一下配置。这里咱们监控一下:api
scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' static_configs: - targets: ['192.168.1.111:9090'] labels: instance: prometheus
这里,咱们从新启动一下 Prometheus 服务,为了验证一下,咱们须要在浏览器中输入地址: http://192.168.1.111:9090/浏览器
而后咱们看到的是这个:ui
按照图中的红色框框,咱们能够看到的是这个:this
咱们看最后一个,最后一个是我配置的 Prometheus 的配置。 他的 state 是 up , 表示当前的监控是 OK 的。上面的三个暂时无论,我有 exporter 没有启动。看到 up 以后,就表示咱们的这个服务是正常的。url
这里咱们要将数据写入到 Cortex 中 ,这里,咱们是将 Cortex 做为 Prometheus 的一个远程存储点。因此咱们须要在 Prometheus 配置中针对这个 远程存储进行配置。
remote_write: - url: http://192.168.50.178:8088/api/prom/push basic_auth: username: "sunyang_2" password: "sunyang"
这里,咱们的这个 basic_auth 会在请求的 Header 里面添加 Authorization 。 这里的url 是咱们的 cortex 服务的地址,能够是你本身的的代理地址,也能够是 cortex 原地址。下面是 cortex 的地址。
remote_write: - url: http://localhost:9009/api/prom/push
这里安装 Cortex ,主要看我的的网速。
首先,咱们须要先克隆 cortex 下来:
git clone https://github.com/cortexproject/cortex.git
进入 cortex 的根目录:
go build ./cmd/cortex
这里,只要不出问题,那就没问题。出了问题,就只好跟着错误信息进行排除了。
./cortex -config.file=./docs/configuration/single-process-config.yaml
老夫这个安装的是 Windows 版本的。 Linux 版本中,咱们的安装也很简单:
Ubuntu & Debian
sudo apt-get install -y adduser libfontconfig1 wget https://dl.grafana.com/oss/release/grafana_6.6.2_amd64.deb sudo dpkg -i grafana_6.6.2_amd64.deb
Standalone Linux Binaries
wget https://dl.grafana.com/oss/release/grafana-6.6.2.linux-amd64.tar.gz tar -zxvf grafana-6.6.2.linux-amd64.tar.gz
Redhat & Centos
wget https://dl.grafana.com/oss/release/grafana-6.6.2-1.x86_64.rpm sudo yum localinstall grafana-6.6.2-1.x86_64.rpm
咱们在 浏览器中 输入 "http://ip:3000" , 默认的帐号密码是 : admin / admin
咱们进去以后,首先建立一个 数据源,这里,咱们须要建立的数据源是 Prometheus ,可是咱们的真实地址在 Cortex 。如今他们的这个流程是这样的:
咱们这里添加两个数据源,一个是 cortex , 一个是 prometheus .
这里我给你们看一下这两个数据源的配置后:
这里特别注意: cortex 中的 URL : "http://192.168.1.111:9009/api/prom"
咱们在配置好数据源后,就开始添加咱们的 DashBoard .
这里能够看到,咱们的Prometheus , Cortex 添加的 DashBoard 都是有东西的,虽然咱们的这个DashBoard 仍是初始化的状态。但这说明,咱们的DashBoard 是正常的。如今,咱们就要来配置咱们的 Query .
首先配置咱们的 Prometheus-Dashboard .
咱们选择 "Edit"
按照个人配置来:
这里我选择的 "metrics" 是 wmi_cpu_time_total , 这个你们能够随意 , 你们能够直接填 "up" . 我这里的是对个人 Windows 系统作的监控。而后,咱们保存:
配置完Prometheus ,咱们配置咱们的 Cortex-Dashboard
这里,我为了方便查看 Cortex 和 Prometheus , 我把监控放在了一块儿。
两边都有了数据,说明咱们的配置成功。