Grafana 中文入門教程 | 構(gòu)建你的第一個儀表盤
公眾號關(guān)注“杰哥的IT之旅”,
選擇“星標”,重磅干貨,第一時間送達!

服務監(jiān)控到底咋配置?怎樣保證數(shù)據(jù)安全? 保證健康的服務到底應該監(jiān)控些什么? 配置好后的儀表盤為啥消失了? 查詢 Query 又該咋寫?
在卡拉搜索,我們用 Grafana 監(jiān)控所有的服務狀態(tài),從引擎到索引。完善的監(jiān)控幫助我們實時了解卡拉的搜索延遲,慢搜索,Docker 狀態(tài)等等。如果你對搜索引擎、數(shù)據(jù)庫搜索、App 內(nèi)搜索感興趣,也歡迎通過博客[2] 或 Demo[3] 進一步了解或試用卡拉搜索
1. Grafana 是什么
Grafana 支持的數(shù)據(jù)源
Prometheus 本文中的例子,你沒聽過也沒關(guān)系不影響閱讀,把它想象成帶時間戳的 MySQL 就好 Graphite OpenTSDB InfluxDB MySQL/PostgreSQL Microsoft SQL Server 等等
2. 什么情況下會用到 Grafana 或者監(jiān)控儀表盤
我的位置是哪里,在路中間么(當然這個無法通過監(jiān)控系統(tǒng)實現(xiàn),得看路) 我的速度是多少 - 速度儀表盤 我的油、發(fā)動機水溫等等關(guān)鍵指標是多少 - 其它儀表盤
阿里在雙十一控制室用了監(jiān)控儀表盤,因此所有雙十一的新聞基本上都可以看到這個儀表盤 各酷炫公司大廳里常常放一個儀表盤來展示實力(用戶數(shù)啦、營收啦之類) 你的 PC 上的資源管理器、Mac 上的 Activity Monitor 都是某種意義上的儀表盤
3. 安裝和配置 Grafana
version: '3.4'
services:
prometheus:
image: prom/prometheus
container_name: prometheus
hostname: prometheus
ports:
- 9090:9090
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
prometheus-exporter:
image: prom/node-exporter
container_name: prometheus-exporter
hostname: prometheus-exporter
ports:
- 9100:9100
grafana:
image: grafana/grafana
container_name: grafana
hostname: grafana
ports:
- 3000:3000
volumes:
- ./grafana.ini:/etc/grafana/grafana.ini
Prometheus 普羅米修斯時序數(shù)據(jù)庫,用來存儲和查詢你的監(jiān)控數(shù)據(jù) Promethues-exporter 一個模擬數(shù)據(jù)源,用來監(jiān)控你本機的狀態(tài),比如有幾個 CPU,CPU 的負載之類 Grafana 本尊
docker-compose up,應該會看到類似:
http://localhost:3000 應該就可以看到 Grafana 跑起來的初始登錄界面。初始的用戶名是 admin,密碼也是 admin。輸入之后,會要求你改密碼

4. Grfana 工作原理
Prometheus-exporter 又是什么呢?Prometheus-exporter 這個服務,會查詢你的本地電腦的信息,比如內(nèi)存還有多少、CPU 負載之類,然后將數(shù)據(jù)導出至普羅米修斯數(shù)據(jù)庫。
Prometheus-exporter),而最右邊的 Grafana 會查詢中間的普羅米修斯,來展示儀表盤。5. 搭建你的第一個儀表盤
第 1 步 - 設(shè)置數(shù)據(jù)源

Add Data Source 即添加數(shù)據(jù)源,選擇 Prometheus
http://prometheus:9090 就可以了。你可能會問,為什么不是 localhost:9090呢?原因是,我們用了docker-compose起的三個服務,可以把它們想象成三臺獨立的服務器,因此需要用一個域名來互相通信。我們在 docker-compose.yml 中設(shè)置的普羅米修斯服務器的名字就叫prometheus,因此這里需要用前者。

Data source is working 這個檢測,這時表明你的 Grafana 已經(jīng)跟普羅米修斯說上話了第 2 步 - 導入 Dashboard
prometheus-exporter 也就是本機的系統(tǒng)信息監(jiān)控,那么我們可以先找一個同樣用了這個數(shù)據(jù)源的儀表盤。在 Grafana 網(wǎng)站上,你其實可以找到很多別人已經(jīng)做好的儀表,可以用來監(jiān)測非常多標準化的服務。JVM Spring Boot MySQL 監(jiān)控 Laravel 監(jiān)控
Import 即導入,在出現(xiàn)的界面中填入 1860 即我們要導入的儀表盤編號即可。


第 3 步 - 生成和創(chuàng)建新的儀表盤
如何在各種語言中記錄你關(guān)注的數(shù)據(jù)?
包括如何用 Prometheus 查詢數(shù)據(jù)
如何手動生成一個儀表盤
Dashboard 就可以進入添加儀表盤的界面。scrape_duration_seconds,先不用管它的含義是什么,就當它是雙 11 的銷售額好了:
Apply 或 Save 你的儀表盤就被保存了。這時候,用一個大屏幕展示一下,慶祝一下雙十一又過了千億吧6. 總結(jié)
How to explore prometheus with easy hello world projects[11] Node Exporter Guide[12] Grafana Tutorials[13] Grafana Simple Synthetic Monitoring[14] Grafana 快速入門[15]
參考資料
卡拉搜索: https://kalasearch.cn/
[2]博客: https://kalasearch.cn/blog
[3]Demo: https://kalasearch.cn/
[4]Grafana 教程代碼: https://github.com/Kalasearch/grafana-tutorial
[5]https://github.com/Kalasearch/grafana-tutorial: https://github.com/Kalasearch/grafana-tutorial
[6]一張圖: https://medium.com/@bhargavshah2011/monitoring-docker-containers-using-cadvisor-and-prometheus-5350ae038f45
[7]技術(shù)博客: https://kalasearch.cn/blog
[8]https://grafana.com/grafana/dashboards: https://grafana.com/grafana/dashboards
[9]https://grafana.com/grafana/dashboards/1860: https://grafana.com/grafana/dashboards/1860
[10]卡拉搜索首頁: https://kalasearch.cn/
[11]How to explore prometheus with easy hello world projects: https://grafana.com/blog/2019/12/04/how-to-explore-prometheus-with-easy-hello-world-projects/
[12]Node Exporter Guide: https://prometheus.io/docs/guides/node-exporter/
[13]Grafana Tutorials: https://grafana.com/tutorials/
[14]Grafana Simple Synthetic Monitoring: https://grafana.com/blog/2019/06/18/grafana-tutorial-simple-synthetic-monitoring-for-applications/
[15]Grafana 快速入門: https://ken.io/note/grafana-quickstart-influxdb-datasource-graph
推薦閱讀
Nginx系列教程(一)| 手把手教你在Linux環(huán)境下搭建Nginx服務

