homek8s/infrastructure/logging/alloy/config.alloy

311 lines
8.8 KiB
Text

// Grafana Alloy — log collection for the house cluster.
//
// Three sources, and none of them touch the Kubernetes API (everything is read
// off the node's filesystem), so this DaemonSet needs ZERO RBAC beyond the
// default ServiceAccount:
//
// 1. kube/pods — /var/log/pods/<ns>_<pod>_<uid>/<container>/*.log
// 2. systemd-journal — /var/log/journal (kubelet, containerd, sshd, kernel)
// 3. docker/json — /var/lib/docker/containers/*/*-json.log (Pi-only
// services that are not pods: local image registry, Garage)
//
// `tail_from_end = false` on both file sources is deliberate: the nodes already
// hold history (pod log files reach back days; the linode's cilium-agent file
// reaches back 18 days, the Pi's registry json log 31 days). Ingesting those
// files from byte 0 is what gives the store real depth on day one instead of
// starting the 7-day clock at deploy time. The `timestamp` stages stamp every
// entry with the timestamp carried by the line itself rather than the ingest
// time — without that, backfill would land as "now" and be worthless.
//
// Positions live on a hostPath (/var/lib/alloy-data) so a pod restart resumes
// instead of re-shipping every byte.
logging {
level = "info"
format = "logfmt"
}
loki.write "local" {
endpoint {
url = "http://loki.logging.svc.cluster.local:3100/loki/api/v1/push"
}
external_labels = {
cluster = "homek8s",
}
}
// --------------------------------------------------------------- pod stdout
local.file_match "pod_logs" {
path_targets = [{ "__path__" = "/var/log/pods/*/*/*.log*" }]
sync_period = "15s"
}
discovery.relabel "pod_logs" {
targets = local.file_match.pod_logs.targets
rule {
source_labels = ["__path__"]
regex = "/var/log/pods/([^/]+)_([^_]+)_([^/]+)/([^/]+)/[^/]+"
target_label = "namespace"
replacement = "$1"
}
rule {
source_labels = ["__path__"]
regex = "/var/log/pods/([^/]+)_([^_]+)_([^/]+)/([^/]+)/[^/]+"
target_label = "pod"
replacement = "$2"
}
rule {
source_labels = ["__path__"]
regex = "/var/log/pods/([^/]+)_([^_]+)_([^/]+)/([^/]+)/[^/]+"
target_label = "container"
replacement = "$4"
}
rule {
target_label = "job"
replacement = "kube/pods"
}
rule {
target_label = "node"
replacement = sys.env("NODE_NAME")
}
}
loki.source.file "pod_logs" {
targets = discovery.relabel.pod_logs.output
forward_to = [loki.process.pod_logs.receiver]
tail_from_end = false
}
loki.process "pod_logs" {
// CRI format: "<RFC3339Nano> <stdout|stderr> <F|P> <message>"
stage.cri {}
stage.timestamp {
source = "time"
format = "RFC3339Nano"
}
stage.labels {
values = {
stream = "stream",
}
}
// osint/news-scraper prints one JSON-ish *item* dump per crawled article
// (`{'title': ..., 'url': ..., ... }`, up to 4.7 kB each). That is scraped
// payload, not an operational log line — the same rows are written to the
// osint Postgres DB — and it is ~88 % of the namespace's volume (measured
// ~150 MB/h during a crawl). Keeping it would push 8 days of retention past
// the 8Gi PVC, so it is dropped here, by exact prefix, for that one
// container. Every operational line (scrapy INFO/WARNING/ERROR, tracebacks,
// duplicate-URL warnings) still ships.
stage.match {
selector = "{namespace=\"osint\", container=\"news-scraper\"}"
stage.drop {
expression = "^\\{'title':"
drop_counter_reason = "osint_article_payload"
}
}
forward_to = [loki.write.local.receiver]
}
// ----------------------------------------------------------- systemd journal
loki.relabel "journal" {
forward_to = []
rule {
source_labels = ["__journal__systemd_unit"]
target_label = "unit"
}
rule {
source_labels = ["__journal_priority_keyword"]
target_label = "level"
}
rule {
source_labels = ["__journal__hostname"]
target_label = "host"
}
rule {
target_label = "job"
replacement = "systemd-journal"
}
rule {
target_label = "node"
replacement = sys.env("NODE_NAME")
}
}
loki.source.journal "system" {
path = "/var/log/journal"
// 192h matches the store's retention: read the whole retained window once,
// then follow. The default is 7h, which would drop most of the history.
max_age = "192h"
relabel_rules = loki.relabel.journal.rules
forward_to = [loki.write.local.receiver]
}
// ------------------------------------------------- non-pod Docker containers
local.file_match "docker_logs" {
path_targets = [{ "__path__" = "/var/lib/docker/containers/*/*-json.log" }]
sync_period = "30s"
}
discovery.relabel "docker_logs" {
targets = local.file_match.docker_logs.targets
rule {
source_labels = ["__path__"]
regex = "/var/lib/docker/containers/([0-9a-f]{12})[0-9a-f]*/[^/]+-json\\.log"
target_label = "container_id"
replacement = "$1"
}
rule {
target_label = "job"
replacement = "docker/json"
}
rule {
target_label = "node"
replacement = sys.env("NODE_NAME")
}
}
loki.source.file "docker_logs" {
targets = discovery.relabel.docker_logs.output
forward_to = [loki.process.docker_logs.receiver]
tail_from_end = false
}
// ------------------------------------------- kube-apiserver audit log (host file)
// siriussec only. The apiserver writes JSON audit records to
// /var/log/kubernetes/audit/audit.log (mode 600 root; the apiserver rotates it
// itself: 100 MB x 48, compressed, ~2.2 GB/day). This is a host file, not pod
// output, hence the extra hostPath mount in alloy.yaml.
//
// SECURITY — this source does NOT forward records as-is. At
// `level: RequestResponse` the record for a `get secrets` carries
// `responseObject.data`: the plaintext secret value. Shipping verbatim would
// copy every secret that has been read into a second queryable system. Instead
// the record is parsed and re-emitted as a packed *allowlist* of fields, so
// `requestObject` / `responseObject` never reach Loki. `stage.json` drops any
// line it cannot parse, so a malformed record cannot fall through to raw bytes.
// (Filter is deliberately unconditional — stricter than "strip only for
// objectRef.resource == secrets". If audit bodies for *writes* are ever wanted,
// that is a separate, reviewed change.)
//
// Only the live file plus the rotated `.gz` archives are read; nothing in this
// directory is ever deleted by the collector, so the apiserver's own 4.8 GB /
// 7-day rotation stays the only thing pruning it. Loki's 192h retention is
// independent and applies to the *stored copies*.
local.file_match "audit_live" {
path_targets = [{
"__path__" = "/var/log/kubernetes/audit/audit.log",
job = "kube/apiserver-audit",
node = sys.env("NODE_NAME"),
}]
sync_period = "10s"
}
local.file_match "audit_rotated" {
path_targets = [{
"__path__" = "/var/log/kubernetes/audit/*.log.gz",
job = "kube/apiserver-audit",
node = sys.env("NODE_NAME"),
}]
sync_period = "1m"
}
loki.source.file "audit_live" {
targets = local.file_match.audit_live.targets
forward_to = [loki.process.audit.receiver]
tail_from_end = false
}
loki.source.file "audit_rotated" {
targets = local.file_match.audit_rotated.targets
forward_to = [loki.process.audit.receiver]
tail_from_end = false
decompression {
enabled = true
format = "gz"
}
}
loki.process "audit" {
// Field allowlist. Everything not listed here — in particular
// requestObject/responseObject — is discarded with the parsed record.
stage.json {
expressions = {
level = "level",
stage = "stage",
verb = "verb",
user = "user.username",
resource = "objectRef.resource",
apiGroup = "objectRef.apiGroup",
ns = "objectRef.namespace",
name = "objectRef.name",
subresource = "objectRef.subresource",
responseCode = "responseStatus.code",
sourceIP = "sourceIPs[0]",
userAgent = "userAgent",
auditID = "auditID",
requestURI = "requestURI",
time = "requestReceivedTimestamp",
}
}
stage.timestamp {
source = "time"
format = "RFC3339Nano"
}
// verb x resource is small and fixed; `user` and the object name stay in the
// packed body so they do not multiply the stream count.
stage.labels {
values = {
verb = "verb",
resource = "resource",
}
}
stage.pack {
labels = []
}
forward_to = [loki.write.local.receiver]
}
loki.process "docker_logs" {
stage.json {
expressions = {
log = "log",
time = "time",
}
}
stage.timestamp {
source = "time"
format = "RFC3339Nano"
}
stage.output {
source = "log"
}
forward_to = [loki.write.local.receiver]
}