Redian新闻
>
基于腾讯云 Lighthouse 自建 Grafana 和 Prometheus 服务

基于腾讯云 Lighthouse 自建 Grafana 和 Prometheus 服务

公众号新闻

链接:https://cloud.tencent.com/developer/article/2351514

kubernetes 是一种强大的容器编排系统,它使您能够轻松地部署、管理和扩展容器化应用程序。在监控和可视化方面,Grafana 和 Prometheus 是两个非常有用的工具。本文将引导读者朋友们在 Kubernetes 集群上安装和配置 Grafana 和 Prometheus。

前言

    当在Kubernetes集群上安装Grafana和Prometheus之前,让我们首先了解一下这两个工具的基本概念和功能。

Grafana

    Grafana 的主要作用是帮助用户将监控数据可视化,从而更容易理解应用程序和系统的性能、健康状况以及趋势。它提供了一个用户友好的界面,可以让用户创建漂亮和有用的监控仪表板,用于监视和分析数据。当与Prometheus等数据源集成时,Grafana可以展示Prometheus采集到的数据。

Prometheus

    Prometheus 是一个开源的监控和警报工具,最初由SoundCloud开发并开源。其工作原理是定期从各种目标(如应用程序、容器、操作系统、数据库等)中抓取指标数据,存储在本地数据库中,并提供查询和警报功能。这种方式使得Prometheus适合于云原生环境和容器化应用程序的监控。

准备Kubernetes集群

    Lighthouse是腾讯云推出的一款轻量应用服务器产品,它提供了高性价比、高可用性、高安全性和高灵活性的云端计算服务。具体信息参阅官方文档:轻量应用服务器概述。
    为了部署Grafana和Prometheus服务,我们需要购买一台轻量应用服务器,并配置好相关的环境和依赖。购买步骤参见官方文档:使用轻量应用服务器一键创建应用,应用类型请选择**k3s**。

创建kubernetes部署配置文件

    1、将下面的代码保存为grafpro.yaml,可根据实际修改部分参数,或使用后文的脚本自动修改。
kind: Deployment
apiVersion: apps/v1
metadata:
name: &name grafpro
labels:
app: *name
spec:
selector:
matchLabels:
app: *name
template:
metadata:
labels:
app: *name
spec:
containers:
- name: grafana
image: grafana/grafana
securityContext:
runAsUser: 0
ports:
- containerPort: 3000
volumeMounts:
- name: *name
subPath: grafana
mountPath: /var/lib/grafana
- name: prometheus
image: prom/prometheus
securityContext:
runAsUser: 0
ports:
- containerPort: 9090
volumeMounts:
- name: *name
subPath: etc
mountPath: /etc/prometheus
- name: *name
subPath: prometheus
mountPath: /prometheus
volumes:
- name: *name
hostPath:
path: /srv/grafpro
type: DirectoryOrCreate
---
kind: Service
apiVersion: v1
metadata:
name: &name grafpro
labels:
app: *name
spec:
selector:
app: *name
ports:
- name: grafana
port: 3000
targetPort: 3000
- name: prometheus
port: 9090
targetPort: 9090
---
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: &name grafpro
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web,websecure
spec:
rules:
- host: grafana.example.org
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: *name
port:
name: grafana
- host: prometheus.example.org
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: *name
port:
name: prometheus
tls:
- secretName: default

2、将下面的代码保存为grafpro.sh,请注意修改GRAFPRO_STORAGEGRAFPRO_DOMAINPROMETHEUS_DOMAIN变量的值。
export GRAFPRO_STORAGE=${GRAFPRO_STORAGE:-"/srv/grafpro"}

export GRAFANA_DOMAIN=${GRAFPRO_DOMAIN:-"grafana.example.org"}
export PROMETHEUS_DOMAIN=${PROMETHEUS_DOMAIN:-"grafana.example.org"}

mkdir -p $GRAFPRO_STORAGE/etc

cat <<EOF >$GRAFPRO_STORAGE/etc/prometheus.yml
global:
scrape_interval: 5s
scrape_timeout: 5s
evaluation_interval: 15s

scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- 127.0.0.1:9090
EOF


cat grafpro.yaml \
| sed "s#/srv/grafpro#$GRAFPRO_STORAGE#g" \
| sed "s#grafana.example.org#$GRAFANA_DOMAIN#g" \
| sed "s#prometheus.example.org#$PROMETHEUS_DOMAIN#g" \
| kubectl apply -f -

3、将grafpro.yamlgrafpro.sh文件上传到k8s主服务器上的同一目录内。注意检查grafpro.sh文件的换行是否为LF,否脚本可能无法执行。

部署 Grafana 和 Prometheus

    通过ssh登录服务器,执行以下命令,部署Grafana和Prometheus服务。
chmod +x grafpro.sh


./grafpro.sh



测试和验证


Grafana

1、浏览器打开http://grafana.example.org,使用用户名admin和密码admin登录,此时会提示修改一个新的密码,修改后将自动转入控制台页面。
2、浏览器打开http://grafana.example.org/connections/datasources/edit/bc3e3d0a-090a-469f-8c25-f93818410784,在Prometheus server URL输入框内填入http://127.0.0.1:9090,点击Save & Test后保存。
3、浏览器打开http://grafana.example.org/connections/datasources/edit/bc3e3d0a-090a-469f-8c25-f93818410784/dashboards,导入Prometheus 2.0 Stats仪表盘。

测试图表

Prometheus

    浏览器打开http://prometheus.example.org,无需登录即可使用查询面板。生产环境建议至少添加Basic Auth认证,防止数据泄露。

运行状态
好了,今天的小知识你学会了吗?


END

官方站点:www.linuxprobe.com

Linux命令大全:www.linuxcool.com

刘遄老师QQ:5604215

Linux技术交流群:2636170

(新群,火热加群中……)

想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!


微信扫码关注该文公众号作者

戳这里提交新闻线索和高质量文章给我们。
相关阅读
180刀Razer DeathAdder V3 Pro Ultra-lightweight Ergonomic Wireless轻松驾驭!Prometheus 如何监控指标,快速定位故障【Light Up】蒋圣力老师light up讲座回顾:以国际法为灯的循梦而行《癸卯中秋有咏并序》In Northeast China, Tourists From the South Spark a Winter Boom西雅图“THE EMERALD”美丽岸高端海景公寓---Penthouse高端户型鉴赏A City in Bloom: Shanghai Through the ’90s自动生成prompt:Automatic prompt engineeringChina’s Priciest Cities Push to Build — and Sell — Cheap HomesThe Bookstore Giving Shanghai’s Women a Room of Their Own10倍性能提升-SLS Prometheus 时序存储技术演进Homebuyers Wait as Home Price Fall ContinuesCIIE Watch | Sustainability in Spotlight: from Product to Booth【五律】挪威琅琊榜仙境巧遇竹韵诗友‘Blossoms Shanghai’: Wong Kar-wai’s Love Letter to His Hometown轻松驾驭!Prometheus 如何监控指标,快速定位故障?旅美散记(24):巧遇橡树林China's Top Court Spotlights Domestic Violence Children随时入住|现房【Greenhouse 免中介费】接受本科生|近NEU最热公寓2b2b 5275一百五十七 耦园一文带你了解 Prometheus3天搞懂 Prometheus 监控!睡觉更香了,头发也多了The lessons from Microsoft’s startling comeback | 商论双语Prometheus and the Ring of Fire我写的第一本英文书 'The Story of Rice'Migratory Birds Protection in Spotlight at World Coastal Forum最快十分钟通关!厦门实施!24-hour visa-free transit through Xiamen GaoqiNeurIPS 2023 Spotlight | 基于超图的表格语言模型Beyond the State: China's Private Medical Groups Go Global不到$100买Le Creuset!博主晒加拿大Winners、HomeSense疯狂折扣买名牌!打造出色的Prometheus监控系统,你会吗购买Powerhouse Museum 《大气记忆》澳大利亚首映门票!参与抽奖有机会赢取价值100澳币礼品包!NeurIPS 2023 Spotlight | 面向通用决策场景的MCTS基准框架:LightZero早鸟项目|BOA春季Insight Program项目开启,大一/大二可报名!‘Princess Iron Fan’: The Height of Modern Chinese Animation马化腾称阿里云不如腾讯云?腾讯云回应
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。