创建基础 loki-config.yaml 文件
在这一步,你将为 Loki 创建一个最小化的配置文件。这个文件 loki-config.yaml 告诉 Loki 如何运行、在哪里存储数据以及使用哪种 schema(模式)。对于本次 Lab,我们将配置 Loki 使用本地文件系统进行存储。
我们将使用 nano 文本编辑器来创建文件。运行以下命令打开一个名为 loki-config.yaml 的新文件:
nano loki-config.yaml
现在,将以下 YAML 内容复制并粘贴到 nano 编辑器中:
auth_enabled: false
server:
http_listen_port: 3100
common:
path_prefix: /tmp/loki
storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules
ring:
kvstore:
store: inmemory
schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h
storage_config:
boltdb_shipper:
active_index_directory: /tmp/loki/index
cache_location: /tmp/loki/cache
filesystem:
directory: /tmp/loki/chunks
ruler:
alertmanager_url: http://localhost:9093
limits_config:
allow_structured_metadata: false
此配置将 Loki 所需的所有内容都保留在容器内的 /tmp/loki 目录下,这可以避免权限问题,并使设置易于理解。它禁用了身份验证(authentication),为单节点使用启用了内存中的环(in-memory ring),并将块(blocks)和索引(indexes)存储在本地文件系统上。
文件中每个部分的作用如下:
auth_enabled: false: 关闭身份验证,这样你就可以在没有额外凭证的情况下与 Loki 通信。这使得本地测试更简单;在生产环境中,你会启用身份验证。
server.http_listen_port: 3100: 告诉 Loki 在端口 3100 上监听 HTTP 请求。当你 curl localhost:3100 时,你访问的就是这个端口。
common.path_prefix: 设置 Loki 在容器内部存储临时数据的基础文件夹。此文件中其他所有路径都基于 /tmp/loki 构建,因此所有内容都保留在一个地方。
common.storage.filesystem: 指向 Loki 存储日志数据块(chunks)和规则文件(rule files)的两个文件夹。由于我们没有挂载任何特殊内容到容器中,使用 /tmp/loki 可以避免权限方面的麻烦。
common.ring.kvstore.store: inmemory: 将成员信息保存在内存中。对于单节点 Lab 来说这很完美;一个真正的集群会使用像 Consul 或 etcd 这样的共享存储。
schema_config: 定义 Loki 应如何布局日志数据的索引。我们设置了一个起始日期,选择了 boltdb-shipper 存储,并将索引保留在文件系统上。prefix 和 period 控制文件的命名方式以及新索引文件的创建频率(每 24 小时)。
storage_config: 为 boltdb-shipper 索引(active_index_directory 和 cache_location)以及原始块存储提供了确切的文件夹。所有路径再次位于 /tmp/loki 下,以保持整洁。
ruler.alertmanager_url: 准备 Loki 的 ruler 组件,以便将警报发送到 http://localhost:9093 的 Alertmanager。如果 Alertmanager 没有运行,Loki 不会崩溃;当它尝试发送警报时,只会记录一个警告。
limits_config.allow_structured_metadata: false: 禁用了一个高级功能,以便 Loki 坚持使用初学者期望的更简单、纯文本的日志元数据。
按下 Ctrl+X 退出,然后按 Y 确认保存,最后按 Enter 以 loki-config.yaml 的名称保存文件。