「OBSERVABILITY」- 日志收集方案
问题描述
该笔记将记录:与集群日志收集有关的内容,以及常见问题的解决办法;
解决方案
Apache Flume
Welcome to Apache Flume — Apache Flume
GoAccess
GoAccess – Visual Web Log Analyzer
Logwatch
Logwatch download | SourceForge.net
日志类别
系统日志 容器日志
systemd-journal
属于系统日志
kubernetes-events
属于容器日志
收集方案: 1)Grafana Agent/Kubernetes Events
kubernetes-container
linux-kernel
Linux 内核日志;
关于 Linux 系统日志
日志服务
日志轮转
日志分析
关于日志的集中化管理
应用程序错误日志管理
Sentry – https://sentry.io/welcome
日志的来源可以分为两种: 1)由系统产生的日志,通常用 syslog 处理; 2)由应用程序产生的日志,写入自定义的日志文件;
「shell 在手分析服务器日志不愁?」
「Top 8 Log Analyzers」
「基于 Fluentd 的日志收集方案」
「Fluentd | Open Source Data Collector | Unified Logging Layer」
Sentry
ElastAlert – https://github.com/Yelp/elastalert
清华大学开源软件镜像站: https://mirrors.tuna.tsinghua.edu.cn/elasticstack/
参考文献
Wikipedia/Rsyslog Wikipedia/syslog 12 Critical Linux Log Files You Must be Monitoring[……]
「OBSERVABILITY」- 应用性能监测
解决方案
Open Source Application Performance Monitoring (APM) Tools
4)Pinpoint 1)Apache SkyWalking™ | SkyWalking Team 3)Dapper, a Large-Scale Distributed Systems Tracing Infrastructure – Google Research
1)Jaeger: open source, end-to-end distributed tracing 2)OpenZipkin · A distributed tracing system 3)
方案对比
使用 OpenTelemetry 零代码修改接收 SkyWalking 追踪数据 https://zhuanlan.zhihu.com/p/556409746
skywalking 兼容 OpenTelemetry 标准 skywalking 也可以接入 使用 OpenTelemetry 零代码修改接收 SkyWalking 追踪数据 标准里
目前使用较多的就是 pinpoint zipkin jaeger skywalking 这些吧, 然后就官方 agant 支持 rust 的话有 zipkin、skywalking,这两个也都兼容 OpenTelemetry 标准
Rust | OpenTelemetry https://opentelemetry.io/docs/instrumentation/rust/
skywalking-rust/simple_trace_report.rs at master · apache/skywalking-rust (github.com) 这个例子里 skywalking 是 agent 主动上报的
opentelemetry-rust/server.rs at main · open-telemetry/opentelemetry-rust (github.com) 这里有一个跟 grpc 集成的例子,用的就是 tonic 它例子里好像是可以自动处理 propagator 的
[rust 微服务]grpc 分布式追踪怎么做?OpenTelemetry?jaeger?_哔哩哔哩_bilibili skywalking java 栈的,特吃资源。功能也是大而全 jaeger 的 ui 简陋[……]
「Fluentd」- 安装(CentOS 7.x)
第一步、准备工作
1)同步服务器时间: 参考 Time Synchronization/systemd-based 笔记
2)增加文件描述符: 在安装后,通过 systemctl edit 修改,而非修改 system.conf 文件
3)调整内核的参数: 根据官方文件建议,如果是在由多个 Fluentd 实例组成的高负载环境中,优化网络内核参数
第二步、安装启动
# 该脚本将添加 /etc/yum.repos.d/td.repo 仓库,并安装 td-agent 服务
curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent3.sh | sh
# 增加文件描述符
systemctl edit td-agent.service
# 启动服务
systemctl start td-agent.service
systemctl enable td-agent.service
第三步、测试服务
默认 /etc/td-agent/td-agent.conf 配置,从 HTTP 接收数据,并写入 /var/log/td-agent/td-agent.log 文件。
#1 第一步、使用 tail 命令监控日志:
tail -f /var/log/td-agent/td-agent.log
#2 执行如下命令发送“日志”数据:
curl -X POST -d ‘json={“json”:”message”}’ ‘http://localhost:8888/debug.test’
#3 然后观察控制台,如果产生类似如下输入,则表示服务搭建成功:
2020-05-26 16:34:27.089123484 +0800 debug.test: {“json”:”message”}
参考文献
Fluentd/Before Installation Fluentd/Install by RPM Package (Redhat Linux)[……]
「Grafana」- 概念术语
功能概述(⇒ Introduction to Grafana)
Introduction to Grafana | Grafana documentation
Grafana 的主要用途便是数据展示(可视化)。
Grafana OSS,Grafana Open Source Software
当安装后,我们能够根据自己的需要来定制 Grafana 界面,具有多种多样的选择: 1)首先是 in Getting started with Grafana 页面,引导我们入门; 2)通过 Playlist 特性,我们能够来显示天气、只能家居等等内容; 3)作为管理员,能够通过 authentication 和 Provisioning 为团队提供服务; 4)更多的引导和想法,参考 Grafana Labs Community Forums 社区;
Explore metrics, logs, and traces:通过 Grafana 能够浏览数据,进行各种对比。参考 Explore 文档,获取更多详细内容; Alerts:支持多种告警渠道(PagerDuty, SMS, email, VictorOps, OpsGenie, Slack),通过 Alert Rules 来配置告警规则; Annotations:注解功能,允许我们在图上添加注解描述性信息。在出现问题时,关联数据很有用。 Dashboard variables:是对 Dashboard 的复用,允许页面引用变量来呈现不同页面,Variables 决定 Dashboard 的展示数据; Configure Grafana:允许我们对 Grafana 进行各种的配置,前提是我们了解 Grafana 的配置选项及使用方法。 Import dashboards and plugins:支持导入 Dashboard 与 插件。 Authentication:支持多种认证方式,并能将用户划入不同的组织当中。企业版还能划入 Team 中,并集成其他认证系统。 Provisioning:通过该特性,我们能够实现 Grafana 的快速自动部署及应用,快速为团队提供 Grafana 服务; Permissions:通过权限来分离和共享 Dashboard,多不同用户施以不同的权限,企业版还能细化到 Datasource 级别; Other Grafana Labs OSS Projects:Loki 日志平台;Tempo 性能追踪平台;Mimir 为 Prometheus 提供可扩展的长期存储;
Grafana Enterprise
其是 Grafana 企业版本,提供各种企业级功能特性。 出于成本考虑,我们暂时不会购买企业版授权,所以不再关注此部分内容。
Gra[……]
「Grafana」- 配置维护
配置文件(⇒ Setup/Configure Grafana)
Configure Grafana | Grafana documentation
配置文件路径: 1)$WORKING_DIR/conf/defaults.ini 或 /usr/local/etc/grafana/grafana.ini; 2)具体路径取决于系统;
配置注释: 1)使用 ; 对行进行注释;
通过环境变量覆盖配置: 1)GF_<SectionName>_<KeyName> 来指定被覆盖变量的名称;
在配置文件中,使用变量: 1)我们使用的环境变量:${<environment variable>} 2)或者 $__<provider>{<argument>} 形式: —- $__env{LOGDIR}/grafana 从环境变量中读取; —- $__file{/etc/secrets/gf_sql_password} 从文件中读取; —- $__vault{XXXX} 从 Vault 中读取;
至于文档的其余部分,多是配置参数的具体细节,这里不再展开。
Configure Grafana Enterprise
关于企业版的配置,我们这里不再关注。
服务重启(⇒ Setup/Restart Grafana)
与常规的服务重启相似: 1)brew services restart grafana 2)systemctl restart grafana-server 3)docker-compose restart grafana 4)……[……]
「Grafana」- 安装部署
版本选择(⇒ What’s new)
What’s new | Grafana documentation
正如官方文档所述,Grafana 一直在变,对于发布亮点,参考 What’s new 页面。
当部署时,我们通常选用最新的稳定版本(升级是早晚的事情,选用旧版本也要升级)。
服务部署(⇒ Setup/Install Grafana)
Setup | Grafana documentation
官方提供各种安装方法: 1)Install on Debian or Ubuntu 2)Run Grafana Docker image 3)Deploy Grafana on Kubernetes(针对我们的场景,我们只关心该部署方式) 4)Install on RPM-based Linux 5)Install on Windows 6)Install on macOS
服务升级(⇒ Setup/Upgrade Grafana)
Upgrade Grafana | Grafana documentation
根据官方文档描述,升级过程比较简单,但是依旧可能存在轻微地破坏性的变化。
在升级前,官方建议: 1)备份插件 2)备份配置 3)数据库备份,以在降级时恢复;[……]
「Kubernetes」- 部署 Grafana 服务
问题描述
该笔记将记录:在 Kubernetes Cluster 中,部署 Grafana 的方法,以及常见问题的解决办法。
解决方案
官方文档目前仅提供通过 YAML 进行部署的方法,而我们主要采用 HELM 进行部署。
通过 HELM 部署
# 生成 values 文件,并进行配置
helm show values grafana/grafana > grafana.helm-values.yaml
vim grafana.helm-values.yaml
…(1)调整 Ingress 配置
# 安装 Grafana 服务
helm –namespace=grafana upgrade –install grafana grafana/grafana -f grafana.helm-values.yaml
# 查看 Grafana 密码
kubectl get secret –namespace grafana loki-grafana -o jsonpath=”{.data.admin-password}” | base64 –decode ; echo
# Add datasource
# Basic Auth: Username:Password
# http://loki-loki-distributed-gateway.loki.svc.cluster.local/
登录 Grafana 系统
http://localhost:3000/ admin password
参考文献
Deploy Grafana on Kubernetes | Grafana documentation grafana/helm-charts[……]
「Grafana」- 高可用性(⇒ Setup/Set up Grafana for high availability)
[……]
「Grafana」- 观测 Grafana 指标(⇒ Setup/Set up Grafana monitoring)
该笔记提到的监控日志告警是对 Grafana 的检测,而非利用 Grafana 来监控其他服务。
Grafana 支持 Jaeger tracing;能够向 Prometheus 暴露指标;
TODO ! Grafana Loki 的规则及告警的使用方法; TODO ! Grafana Loki 配置告警规则,并在告警中显示日志;[……]
「GRAFANA」- 数据源(Data Source)
数据源列表
Alertmanager AWS CloudWatch Azure Monitor Elasticsearch Google Cloud Monitoring Graphite Loki Microsoft SQL Server (MSSQL) MySQL OpenTSDB PostgreSQL Prometheus Jaeger Zipkin Tempo Testdata
FIXME ! Grafana 展示 Zabbix 监控数据; How to Monitor MySQL with Grafana | Grafana Labs
InfluxDB
通过 Dashboard 配置(GUI)
Grafana documentation/InfluxDB data source
Data Sources / InfluxDB
Query Language: Flux(InfluxDB 2.4.0 使用 Flux 为其查询语言)
HTTP URL: http://172.31.253.41:8086 Access: Server (default)
InfluxDB Details Organization: k4nz.com Token: xxxxxxxxxxxxxxxxxxxx Default Bucket: pve
通过 YAML 配置
Grafana documentation/Provision InfluxDB
apiVersion: 1
datasources:
– name: InfluxDB_v2_Flux
type: influxdb
access: proxy
url: http://localhost:8086
secureJsonData:
token: api-token
jsonData:
version: Flux
organization: organization
defaultBucket: bucket
tlsSkipVerify: true
通过 Provisioning 配置
Data source management | Grafana documentation Grafana documentation/Data sources/Loki
问题描述
当更新 Grafana 配置后,需要重启服务以加载配置,但是如果数据没有持久化,则会导致数据丢失;
解决方案
虽然能够通过内置数据库(sqlite)来持久化存储,但是我们通过配置的方式来进行数据源的配置;
不同 Datasource 的配置方法不同,参考特定[……]
「Grafana」- Explore:数据浏览器(⇒ Explore)
参考文献
Explore | Grafana documentation[……]
「Grafana」- 安全、用户、权限(=> Setup/Configure security)
保护 Grafan 的安全: 1)限制能够访问服务的地址或主机名;限制能够访问数据源的网络地址或主机名;data_source_proxy_whitelist; 2)请求安全:显示用户的请求频率; 3)防火墙安全:通过防火墙,限制对 Grafana 的访问; 4)代理服务器:通过代理服务器来访问 Grafana 界面; 5)显示用户查询权限:即使 Viewer 仅具有某些受限访问权限,而实际上还是能够通过 API 发送各种查询;通过企业版 Data Source Permissions 限制对多个不同数据源的访问权限;或常见组织,在组织内使用数据源,通过数据源的限制,来控制用户访问的数据; 6)匿名访问:对于匿名访问进行一定程度的控制;
Configure authentication
配置访问认证,集中认证:
Configure generic OAuth authentication Configure Google OAuth2 Authentication Configure JWT Authentication Configure Azure AD OAuth2 authentication Configure GitLab OAuth2 Authentication Configure LDAP Authentication Configure enhanced LDAP integration Configure Grafana authentication Configure SAML authentication Configure Okta OAuth2 authentication Configure auth proxy authentication Configure GitHub OAuth2 Authentication
Configure LDAP Authentication
Configure LDAP Authentication | Grafana documentation
FIXME ! Grafana !!! LDAP 相关的认证将是我们关注的重点
Configure database encryption
配置数据库加密:
Encrypt database secrets using Google Cloud KMS Encrypt database secrets using Hashicorp Vault Encrypt database secrets using AWS KMS Encrypt database secrets using Azure Key Vault Integrate Grafana with Hashicorp Vaul[……]
「Grafana Agent」- 指标、日志、追踪
问题描述
我们使用 Grafana Loki 日志系统,现在我们需要采集 Kubernetes Events 信息,并保存到我们的 Grafana Loki 系统中;
经过 Google 查找,我们得知 Grafana Agent 能够完成对 Kubernetes Events 的采集和处理(Kubernetes Events (beta));
该笔记将记录:我们将学习 Grafana Agent 的使用方法,以及与其相关的配置案例,并针对特定问题的解决办法;
解决方案
Grafana Agent | Grafana Agent documentation
GitHub – grafana/agent: Telemetry agent for the LGTM stack.
原理简述
WIP
特性特征
Grafana Agent 具有很多亮点;
但是对于我们来说,它最突出的地方是对各种 exporter 做了集成:其内嵌各种 Exporter 服务(诸如 node_exporter、mysql_exporter 等等),还集成 Log、Metric、Trace 的客户端,减轻我们对微服务的管理。
应用场景
Kubernetes Events Collection[……]
「Grafana Agent」- 服务部署
with Helm
Installing Grafana Agent Operator with Helm | Grafana Agent documentation
helm repo add grafana https://grafana.github.io/helm-charts
helm pull grafana/grafana-agent-operator # grafana-agent-operator-0.2.8.tgz
helm show values ./grafana-agent-operator-0.2.8.tgz > grafana-agent-operator-0.2.8.helm-values.yaml
vim grafana-agent-operator-0.2.8.helm-values.yaml
…(1)nameOverride: “grafana-agent”
…(2)fullnameOverride: “grafana-agent”
…(3)kubeletService.namespace: observing-client
helm install –namespace=observing-client –create-namespace \
grafana-agent \
./grafana-agent-operator-0.2.8.tgz -f grafana-agent-operator-0.2.8.helm-values.yaml[……]
「Grafana Agent」- 采集 Kubernetes Events 日志
问题描述
该笔记将记录:通过 Grafana Agent 采集 Kubernetes Events 日志的方法,以及相关问题的解决办法。
解决方案
第一步、部署 Grafana Agent Operator 组件
参考 Grafana Agent documentation/Installing Grafana Agent Operator with Helm 文档,以获取更多安装细节; 或者,参考 Installing and Upgrading 笔记;
我们通过 Grafana Agent Operator 组件来完成 Grafana Agent 的部署及管理;
第二步、部署 Grafana Agent 实例
参考 Custom Resource Quickstart/Step 1: Deploy GrafanaAgent resource 文档,该文档足够我们运行一个 Grafana Agent 实例;
但是,针对 Kubernetes Events 采集,需要增加额外的 Integration 配置:
apiVersion: monitoring.grafana.com/v1alpha1
kind: GrafanaAgent
metadata:
name: grafana-agent
namespace: observing-client
labels:
app: grafana-agent
spec:
…
integrations:
selector:
matchLabels:
agent: grafana-agent-integrations
…
第三步、部署 Log Instance 资源
—
apiVersion: monitoring.grafana.com/v1alpha1
kind: LogsInstance
metadata:
name: infra-log-central
namespace: observing-client
labels:
agent: grafana-agent-logs
spec:
clients:
# 专用于 kubernetes events 的日志
– url: http://loki-loki-distributed-gateway.loki.svc.cluster.local/loki/api/v1/push
basicAuth:
username:
name: infra-log-central
key: username
password[……]
「Grafana Loki」- 日志收集
问题描述
在我们的技术栈中,Grafana Loki 出现的频率较高,能够覆盖我们多数业务场景,所以我们将尝试对其学习并使用;
该笔记将记录:我们对 Grafana Loki 的学习笔记,与之关的概念术语、部署、使用、维护,及常见问题解决办法;
解决方案
官方文档是我们学习的开始,以官方文档为中心进行学习与使用。
该部分笔记将围绕官方文档展开:
Release notes => Installing and Upgrading Fundamentals => Concepts and Fundamentals Installation => Installing and Upgrading Upgrading => Installing and Upgrading Getting started => Getting Started Best practices => Best practices
Configuration Clients(负责将查询日志发送到 Grafana Loki 中) —- Loki Promtail —- Lambda Promtail —- AWS —- Docker driver —- Fluent Bit —- Fluentd —- Logstash —- k6 load testing Alerting and Recording Rules => Alerting and Recording Rules LogQL => LogQL Operations(运维管理相关的内容,包括 认证、多租户、存储、排障 等等;) —- Authentication => Problems Solving and How-to —- Loki in Grafana —- Observability —- Overrides Exporter —- Scalability —- Storage => Storage (Grafana Loki) —- Multi-tenancy —- Loki Canary —- Troubleshooting —- Recording Rules HTTP API => HTTP API, REST API Storage => Storage (Grafana Loki) Tools Community => Concepts and Fundamentals/Community Maintaining => Concepts and Fundamentals/Maintaining Design[……]
「Grafana Loki」- 概念术语
问题描述
该笔记将记录:与 Grafana Loki 相关的内容,以及相关问题的解决方案。
解决方案
下面的内容是快速阅读官方文档而摘录的关键信息,至少对于我们来说是重要的,以形成对 Grafana Loki 的整体认识,并不具备参考价值,详细内容建议阅读官方文档;
如果有必要,子章节将对我们关心的特定概念进行进一步的讨论和描述;
仅索引元数据;日志以块的形式压缩存储;数据保存在对象存储中;
=> Fundamentals
Overview
Grafana Loki 是整个日志栈的核心组件,除此之外还有其他组件; 日志仅索引元数据,是根据标签;原始日志内容保持不变; Agent 收集日志,转为流,并写入 Loki 中;Promtail Agent 专为 Loki 安装而设计,其他 Agent 也可无缝对接 Loki 服务; App -> Agent -> Loki Loki 索引的是流(流是关联到具有唯一标签集的日志集合),高质量的标签集是关键;
特新特征: 1)在索引时,高效的内存使用:仅索引标签是其优势; 2)多租户(Multi-tenancy):多租户可共享 Loki 实例,日志将以 Tenancy ID 进行区分; 3)LogQL:类似于 PromQL,用于日志查询; 4)可扩展性:所有组件可运行在同个进程中,但又可以微服务的形式独立运行,以支持扩展; 5)灵活性:许多代理(客户端)都有插件支持,这可将现有的可观察性堆栈加入 Loki,使得 Loki 成为其日志聚合工具,而无需切换现有的可观察性堆栈; 6)Grafana 集成:Loki 与 Grafana 无缝集成,提供完整的可观察性堆栈;
关于 EFK 与 Grafana Loki 的对比(官方肯定会列举自生的优势,所以我们也更加关注这些优势): 1)单进程模式,将数据保存在本地;水平扩展模式,则将数据保存在对象存储中;日志直接存储,其仅索引标签;比完整索引的运行成本更低,并允许开发人员积极地从他们的应用程序中进行日志;缺点就是 如果要查询内容,则需要加载所有与标签匹配的块; 2)Promtail 为 Loki 量身定制:主模式发现日志并转发到 Loki 中(附带标签);并能 K8S 服务发现 Pod,或作为 Docker Log Dirver,也能处理 systemd 日志; 3)与 Prometheus 一起,配合服务发现(相同机制),使得 日志 与 Metric 具有相同的标签,使得问题定问和分析更加容易; 4)Grafana 专门针对来自 Prometheus 和 Loki 等来源的时间序列数据量身定制。可以设置仪表板来可视化指标(即将提供日志支持),并且可以使用探索视图对您的数据进行临时查询。与 Kiba[……]
「Grafana Loki」- 最佳实践
Static labels are good 1)类似 host, application, and environment 都是不错的标签,可以固定使用这些标签;
Use dynamic labels sparingly(有节制地) 1)不要过多的使用动态 Label 2)更推荐使用 filter expression 来过滤日志;
Label values must always be bounded 1)还是再说动态标签的问题,不要由过多不同类型的值;
Be aware of dynamic labels applied by clients 1)还是动态标签的问题,通过 logcli 或 Series API 确定 Label 的类型和其取值类型; 2)防止某些不该出现的 Label 被加入 Stream 中;
Configure caching 1)开启数据缓存,以提升 Loki 性能;
Time ordering of logs 1)日志要按时间顺序发送,否则会被决绝(除非允许非按序日志); 2)或建议在日志上增加新 Label 来标识来自不同系统的日志(适用于多主机但日志时间存在偏差的场景) 3)或者 Promtail 创建新的日志时间;
Use chunk_target_size 1)chunk_target_size 只是 Loki 将 Chunk 填充到特定压缩大小(1.5M),这个大小更高效; 2)还有一些选项会影响 Chunk 的填充大小:max_chunk_age=1h,chunk_idle_period=30m 3) 5-10x or 7.5-10MB 才能填充 1.5M 块大小(具体取决于压缩算法); 4)记住,块是基于流;越多的流,内存将存在越多的块;如果 Stream 过多,那很可能在块填满之前就被 Flush 掉; 5)总之,保证块尽量被填满;如果程序日志过多,可以考虑增加 Label 来创建不同 Stream 以保存日志;
-print-config-stderr / -log-config-reverse-order 1)用于在启动的时候打印配置文件;[……]
「Grafana Loki」- 组件
Distributor
1)Wirte Path 的第一站,进行初始化检查,比如 Tenant ID 等等,以减少 Ingester 压力,然后将流量发往多个 Ingester;Distributor 是无状态,前端负载均衡可以流量均发 2)Validation:时间戳检查、Label 检查、日志长度检查等等; 3)Preprocessing:预先处理,比如对 Label 排序以使得 Hash 一致; 4)Rate limiting:能够针对租户进行频率限制;增加 Distributor 节点,这能够限制某租户在每个 Distributor 的写入频率(加入租户和写入频率是固定的); 注意 distributor 使用自己的 ring 来与对等体注册,并获取节点数量,但这与 Ingester 的 ring 是两回事; 5)Forwarding:负责将数据进行转发到 Ingester(负责最后写入); 6)Replication factor:Distributor 会将日志写入多个 Ingester 中,并要保证 floor(replication_factor / 2) + 1 个写入成功;并且还提供 WAL 机制以防止数据丢失; 7)Hashing:Distributor 与 副本因数结合,以确定 Stream 应该写入哪个 Ingester 中;Stream 是通过 LabelSet 及 Tenant ID 进行 Hash 以找到对应的 Ingester 来存储日志;Ingester 将自己注册到 Hash Ring 中,并附带一堆 Token(Random Unsigned Int 32);进行 Hash 查找时,先找到大于 Stream 的最小 Hash,然后是下一个 Ingrester 的 Token,以此类推(顺时针);Ingester 拥有的 Token 仅负责部分范围 Hash; 8)Quorum consistency:Distributor 使用相同的 Hash ring,所以写入请求发往任意一个 Distributor 都是可以的;Loki 使用 Dynamo 风格的仲裁一致性,以保证读写一致,即 Distributor 将等待 (1/2) +1 个 Ingester 返回后再返回给客户端;
Ingester
1)负责将数据写入后端存储;负责将内存查询的日志返回; 2)lifecycler 来管理 Ingester 的生命周期(但是 WAL 取代此功能):PENDING(当它正在等待另一个 ingester 的切换时), JOINING(当它当前将其令牌插入环并初始化自身时。此时能够接收请求), ACTIVE(当它完全初始化时。它可能会收到对其拥有的令牌的写入和读取请求), LEAVING(当它关闭时。它可能会收到对[……]
「Grafana-Loki」- HTTP API
Loki 提供的 HTTP 接口等等;
参考文献
HTTP API | Grafana Loki documentation[……]
「Grafana Loki」- LogQL:日志查询语句(=> /LogQL)
日志查询
Log stream selector
Log pipeline
Log queries examples
度量值查询
时间范围是用来匹配某个时间内的事件频率;
模板函数
匹配网路地址
查询示例
参考文献
LogQL | Grafana Loki documentation[……]
「Grafana Loki」- 存储(Storage)
问题描述
我们将对与 Grafana Loki 存储有关概念进行进一步的学习,以阅读官方文档为主: 1)/Operations/Storage: Storage | Grafana Loki documentation 2)/Storage: Storage | Grafana Loki documentation
该笔记将记录:阅读官方文档而产生的学习笔记,以及相关问题的解决办法;
解决方案
Loki 需要保存两种数据:Chunk;Index;
Grafana Loki 仅索引标签(并不索引日志内容,这与其他日志系统不同),而日志内容则压缩存储,这两点使得 Loki 能够运维简单且成本低廉;
storage schema
/Operations/Storage/Storage schema | Grafana Loki documentation
Grafana Loki 运行其在运行期间切换日志存储,这里的“切换”是指“从某个时间点开始,将后续日志写入新的存储服务中”;
概念术语(⇒ Storage)
Loki,其收到的每条日志有三个组成部分:Tenant-ID;Label-Set;Log-Content;
Stream(流)
通过 Tenant-ID 与 Label-Set 参数,将日志分类为不同的 Stream;
Chunk(块)
针对同个 Stream 的日志条目,其被压缩为 Chunk(块);
Ingester 接收日志条目,并追加(或创建)Chunk,并保存在内存中,最后周期(可配置)刷新到存储系统中;Loki 支持多种存储系统;
能够用于保存 Chunk 的存储有: 1)Amazon DynamoDB 2)Google Bigtable 3)Apache Cassandra 4)Amazon S3 5)Google Cloud Storage 6)Filesystem (please read more about the filesystem to understand the pros/cons before using with production data) 8)Baidu Object Storage
Index(索引)
在 Index 中,保存有 Steam 的 Label-Set,并且关联到相关的 Chunk; Index 也保存在存储中,Loki 支持使用各种不同的存储来保存 Index;
能够用于保存 Index 的存储有: 1)Single Store (boltdb-shipper) – Recommended for 2.0 and newer index store which stores[……]
「Grafana Loki」- 安装部署
问题描述
我们采用 Promtail 来采集日志,Grafana Loki 来存储日志,以对日志进行集中管理(即单个 Grafana Loki 实例);
该笔记将记录:在 Kubernetes Cluster 中,部署 Grafana Loki 的方法,以及相关问题的解决办法;
解决方案
下载 Loki 和 Promtail 程序; 下载两个程序的配置文件; 启动 Loki 服务; 配置 Promtail 文件以抓取日志,并启动 Promtail 服务;
版本选择(⇒ Release notes)
现在(05/20/2022)最新为 2.5 版本,我们首次部署,所以我们并不关心发行说明;
部署模式
如前所述,Loki 的所有模块都在同个二进制程序中。
Monolithic mode 1)-target=all,所有组件在同个进程中运行; 2)可用于测试或小规模使用(<100G);性能取决于节点性能;流量是循环到各节点; 3)通过 memberlist_config 配置,以使多个成员共享同个存储;
Simple scalable deployment mode 1)读写分离:-target=read and -target=write 2)分离读写而带来的高可用、性能提升; 3)需要前端负载均衡,以将 /loki/api/v1/push 推到 wirte node 中; 4)支持 TB 级别日志;若干 TB 级别;
Microservices mode 1)所有组件独立运行:ingester distributor query-frontend query-scheduler querier index-gateway ruler compactor 2)比较复杂(但配置 Kuberntes 比较容易) 3)具有更好的观测性,也是最高效的 Loki 安装; 4)适用于大规模场景以及高可控性;该模式最适合 Kubernetes 部署。有 Jsonnet 和分布式 Helm 图表安装。
安装方法
Installation | Grafana Loki documentation
Install using Tanka (recommended)
# 05/23/2022 我们没有采用该部署方式,原因是:我们的工作需要面对非常多、各种类型的系统,而 Tanka/Jsonnet 并非所有系统都在使用的工具,所以我们暂时不考虑引入该工具(等该工具大面积普及时我们再开始使用);
Simple scalable deployment with Helm
# 05/23/2022 我们暂时不采用该部署方式,所以相关文档不再阅读;
Instal[……]
「Grafana Loki」- 快速部署(测试、体验、实验)
问题描述
通过 Docker Compose 方式,进行开始入门和体验 Grafana Loki 服务;
该笔记将记录:部署测试使用的 Grafana Loki 的过程,以及相关问题的解决办法。
解决方案
依赖环境
Docker Docker Compose
下载配置
wget https://raw.githubusercontent.com/grafana/loki/main/examples/getting-started/loki-config.yaml -O loki-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/main/examples/getting-started/promtail-local-config.yaml -O promtail-local-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/main/examples/getting-started/docker-compose.yaml -O docker-compose.yaml
运行服务
docker-compose up -d
访问服务
# firefox http://ip-address:3000/
Grafana / Explore / Log browser>
{container=”evaluate-loki_flog_1″} |= “GET”
Run Query
参考文献
Getting started | Grafana Loki documentation[……]
「LOGGING」- 部署 Grafana Loki 服务,实现日志集中管理
问题描述
我们采用 Promtail 来采集日志,Grafana Loki 来存储日志,并对日志进行集中管理(即单个 Grafana Loki 实例来管理多个集群日志)。
该笔记将记录:采用 Grafana Loki 进行日志集中管理的方法,若干需要考虑的问题,以及相关问题的解决办法;
解决方案
在部署前,我们需要考虑的问题:
部署方式
我们直接采用 Grafana Loki 的微服务式(分布式)部署方式,原因如下: 1)官方文档也推荐该部署模式,官方文档也表示模式也最适合 Kubernetes 部署; 2)也符合我们的部署场景,我们需要在 Kubernetes Cluster 中运行日志采集;
我们采用 Helm 进行部署,原因是不用引入新的技术栈。而未采用官方推荐的 Tanka 进行部署,原因是 Tanka 暂时还没有成为像 Helm 那样广泛使用,引入新的工具需要额外的学习成本、维护成本、培训成本;
代理认证
在我们的场景中,鉴于是多集群日志管理,流量需要通过公网(部署 VPN 需要而外成本,所以流量目前是通过公网的),所以我们需要对 Grafana Loki 进行认证,防止非法访问。
但是 Grafana Loki 本身并不提供认证功能(Authentication),需要增加前端服务来提供认证,比如 Nginx Reverse Proxy 等等。
不过,这是个常见需求,所以 Grafana Loki 的 Helm Chart 包含 Basic Auth 特性,在部署的设置打开相关配置即可。
关于 Basic Auth 的位置 1) 虽然 Basic Auth 是在 Grafana Loki Helm Chart 的 ingress 中进行设置,但是实际的 Basic Auth 是添加到 Nginx 当中的; 2)所以不管 Grafana 还是 Promtail 服务,当访问 Gateway 服务时,都需要配置相关的 Basic Auth 认证;
日志标签
我们需要区分来自不同集群的日志,即 Promtail 需要增加标签,以表明该日志所来自的集群。
如果在 scrape_configs/Job 中增加,则每个都需要增加响应的标签。所以,我们使用 clients.external_labels 属性,来为所有 clients 增加标签;
日志保留
Retention | Grafana Loki documentation
Compactor 执行日志清理工作,而日志保留时长通过 limits_config 进行控制;
部署 Loki 集群
helm repo add grafana https://grafan[……]
「Grafana-Loki」- 常见问题处理
认证(⇒ Operations/Authentication)
Authentication | Grafana Loki documentation
Grafana Loki 服务并不提供认证层,官方文件建议在前端的 Reverse Porxy 进行相关的认证服务。
题外话,Reverse Proxy 同时需要负责多租户的 X-Scope-OrgID HTTP Header 的添加;
Template variable service failed Query error: 504
query_range request returns a 504 · Issue #4721 · grafana/loki · GitHub
问题描述
在 Loki 中,如果时间范围选择的较旧,则会产生如下错误:
Templating
Template variable service failed Query error: 504
// 这里的主要错误还是 504 网关问题
原因分析
根据 Loki 架构,我们查看 Querier 日志,发现在索引下载或日志取回的过程中,请求被取消
level=error ts=2022-11-16T12:57:19.037772819Z caller=frontend_processor.go:69 msg=”error processing requests” address=10.34.0.76:9095 err=”rpc error: code = Unknown desc = context canceled”
level=error ts=2022-11-16T12:57:19.037798173Z caller=frontend_processor.go:69 msg=”error processing requests” address=10.34.0.76:9095 err=”rpc error: code = Unknown desc = context canceled”
level=error ts=2022-11-16T12:57:19.037829129Z caller=frontend_processor.go:69 msg=”error processing requests” address=10.34.0.76:9095 err=”rpc error: code = Unknown desc = context canceled”
level=error ts=2022-11-16T12:57:19.037863643Z caller=frontend_processor.go:69 msg=”error pro[……]
「Grafana Loki」- Alerting and Recording Rules
参考文献
Alerting and Recording Rules | Grafana Loki documentation[……]
「Grafana Loki/Client」- Promtail
问题描述
该笔记将记录:与 Promtail 有关的内容、使用方法、以及相关问题的解决办法;
解决方案
Promtail,其为日志采集工具,其将采集分散在各个环境中的日志,并发送到集中的日志存储服务 Grafana Loki 中;
概念术语
Configuration
配置文件(promtail.yaml)的配置参数,以及相关说明;
Pipelines
从 抓取日志 到 发送到日志存储服务,总共分为四步: 1)Parsing stages parse the current log line and extract data out of it. The extracted data is then available for use by other stages. 2)Transform stages transform extracted data from previous stages. 3)Action stages take extracted data from previous stages and do something with them. Actions can: 4)Filtering stages optionally apply a subset of stages or drop entries based on some condition.
Scraping
从日志源(诸如 File、systemd journal、Windows Envent、Syslog 等等 )中抓取日志的方法;
Stages
各种 Stages 的使用方法;
Troubleshooting
运行 prometail 命令,能够针对 scrape_configs 配置进行调试,观察其行为
cat my.log | promtail –stdin –dry-run –inspect –client.url http://127.0.0.1:3100/loki/api/v1/push
cat my.log | promtail –config.file promtail.yaml
但是 clients.external_labels 似乎无法通过该方式进行显示,或许是 promtail 2.5.0 暂时不支持;
Cloud setup GCP Logs
官方文档的该部分内容与 GCP 相关,所以并非我们关注的重点,所以我们将忽略该部分;
参考文献
Troubleshooting | Grafana Loki documentation[……]
「Grafana Loki / Promtail」- 安装部署
解决方案
我们通过 Helm 进行 Promtail 安装部署;
helm repo add grafana https://grafana.github.io/helm-charts
helm pull grafana/promtail # promtail-5.0.0.tgz
helm show values ./promtail-5.0.0.tgz > promtail-5.0.0.helm-values.yaml
vim promtail-5.0.0.helm-values.yaml
…(0)config.clients:http://loki-loki-distributed-gateway.loki.svc.cluster.local/loki/api/v1/push
…(1)config.clients:添加 Basic Auth 认证;
…(2)config.clients:通过 external_labels 字段,来添加集群标签,以区分日志来源;
# helm –namespace=observing-client –create-namespace \
upgrade –install promtail \
./promtail-5.0.0.tgz -f promtail-5.0.0.helm-values.yaml[……]
「PROMTAIL」- 维护配置
positions.yaml
Grafana Loki documentation/Configuration/positions Positions stored in host path volume · Issue #416 · grafana/loki · GitHub
promtail 不断抓取日志文件,并将已抓取的位置记录在 positions.yaml 中。
通过 Helm 部署,该文件通过 hostPath 得以持久化(/run/promtail/)。如果节点重启,该文件将丢失(鉴于某些日志在重启后将丢失,所以这也是合理的)。[……]