Loki + Prometheus Operator + MGR
简介
这三个是K8s 云原生架构中最核心的三大模块:
●Prometheus Operator:监控体系核心(指标监控、告警)
●Loki:日志体系核心(轻量日志收集、查询)
●MySQL 组复制(MGR):数据库高可用核心(分布式强一致集群)
一、Prometheus Operator(K8s 监控标准方案)
K8s 生态官方标准监控框架,用来自动化管理 Prometheus、AlertManager、Grafana 等监控组件。
核心能力
1自动发现 K8s 所有资源(Pod/Node/Service)并抓取指标
2自动生成告警规则、自动配置 Grafana 看板
3提供 CRD 自定义资源:Prometheus/ServiceMonitor/PodMonitor/PrometheusRule
核心组件
●Prometheus:时序数据库,存储监控指标
●AlertManager:告警发送(邮件、钉钉、企业微信)
●Grafana:监控大盘可视化
●ServiceMonitor:定义要监控的服务
典型用途
●CPU / 内存 / 网络 / 磁盘监控
●K8s 组件状态监控
●应用 QPS、延迟、错误率监控
二、Loki(轻量 K8s 日志方案)
是什么
Grafana 开源的轻量级日志系统,对标 ELK,但极轻量、低成本。
架构(极简)
为什么比 ELK 强
●不存储全文日志,只存储索引 + 日志原文件
●存储成本只有 ELK 的 1/10
●与 K8s 标签天然兼容
●查询语法非常简单:{app="mysql"} |= "error"
典型用途
●容器日志收集
●应用日志排查
●与监控联动(监控 + 日志同面板)
三、MySQL Group Replication(MySQL 组复制 MGR)
是什么
MySQL 官方分布式强一致高可用集群方案,实现:
●多节点实时同步
●自动故障转移
●数据强一致性(无丢数据)
核心特点
●单主模式:一个写节点,其他只读
●多主模式:所有节点都可写(生产不推荐)
●故障自动检测:节点挂了自动切换
●数据强一致:Raft 一致性协议保证
K8s 上的价值
●数据库永不宕机
●自动扩缩容
●数据不丢失
●一键备份恢复
四、三者在 K8s 中的整体架构(最关键)
三者关系总结
●Prometheus Operator:告诉你系统哪里坏了
●Loki:告诉你为什么坏了(看日志)
●MySQL MGR:保证核心数据不丢、不停
五、K8s 部署方式(Helm 一键)
1. 部署 Prometheus Operator
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/kube-prometheus-stack
2. 部署 Loki
helm repo add grafana https://grafana.github.io/helm-charts
helm install loki grafana/loki-stack
3. 部署 MySQL Group Replication
MySQL Operator 会自动创建 MGR 集群,无需手动配置。
helm repo add mysql https://helm.mysql.com
helm install mysql mysql/mysql-operator --set cluster.replicas=3
六、生产最佳实践
1.Prometheus Operator
○使用持久化存储
○配置告警规则(CPU、内存、磁盘、Pod 异常)
2.Loki
○开启压缩
○设置日志保留 7-30 天
○与 Grafana 联动
3.MySQL MGR
○必须 3 节点以上(Raft 选举)
○单主模式
○开启自动备份
○使用本地 SSD 存储
总结
●Prometheus Operator = K8s 监控大脑
●Loki = K8s 轻量日志方案
●MySQL Group Replication = MySQL 高可用强一致集群
三者组合 = K8s 生产环境标准可观测性 + 高可用架构。
三者如何联动
┌──────────────────────────────────────────────────────────────────┐
│ Kubernetes / VM 集群 │
│ │
│ ┌─────────────────────────MGR 集群───────────────────────────┐ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │
│ │ │ MySQL Primary│ ◄──► │ MySQL Replica│ ◄──► │ MySQL Replica│ │ │
│ │ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ │
│ └────────┼─────────────────────┼─────────────────────┼─────────┘ │
│ │ (日志) │ (日志) │ (日志) │
│ ▼ ▼ ▼ │
│ [ Promtail ] [ Promtail ] [ Promtail ] │
│ │ │ │ │
└───────────┼─────────────────────┼─────────────────────┼──────────┘
│ │ │
▼ ▼ ▼
┌──────────────┐ ┌──────────────────────────────┐
│ Loki 集群 │ │ Prometheus Operator (指标) │
└──────┬───────┘ └──────────────┬───────────────┘
│ │
└──────────────┬──────────────┘
▼
┌─────────────────┐
│ Grafana 统一UI │ (排查问题:左边看指标暴涨,右边看MGR错误日志)
└─────────────────┘
更多推荐


所有评论(0)