Kubernetes
Connect a Kubernetes cluster to Lantern with two OpenTelemetry Collector releases and kube-state-metrics, so every pod's logs, traces and metrics, the nodes' metrics and the cluster's events arrive with their Kubernetes identity.
Lantern needs one thing from a cluster: something in it that sends OTLP to
your organisation's edge, https://otlp.lntrn.dev, with one of your
organisation's edge credentials. This page sets up the shape Lantern is built
around, using the upstream OpenTelemetry Collector chart:
- an agent on every node (a DaemonSet). It:
- reads every container's stdout;
- takes OTLP from the pods on its node;
- collects node, pod and container metrics;
- stamps every record with the pod, namespace, node and workload it came from.
- one forwarder (a Deployment). It:
- receives everything the agents send;
- scrapes kube-state-metrics and watches the cluster's events;
- names the cluster;
- is the only thing that holds the credential and talks to Lantern.
- kube-state-metrics, for the state of pods, deployments, jobs and nodes.
Applications in the cluster send to their node's agent, with no credential of their own.
Before you start
- A cluster you can install into, with
kubectland Helm 3. - Nodes that may run a DaemonSet with read-only
hostPathmounts of/var/log/podsand/. Some managed "autopilot" modes forbid them; the agent needs them to read container logs and node metrics. - Outbound HTTPS from the cluster to
otlp.lntrn.dev. - An edge credential for your organisation (an admin makes it; see Server telemetry). Make one for this cluster alone, so revoking it affects nothing else.
Checked with the opentelemetry-collector chart 0.113.1 and
otel/opentelemetry-collector-contrib 0.116.1, the versions Lantern's own
collectors run, and kube-state-metrics chart 8.6.0, on Kubernetes 1.35.
Newer collectors rename some Kubernetes attributes and metrics. Stay on these
versions until Lantern documents a newer one.
1. Store the credential
Everything goes in one namespace, lantern here:
kubectl create namespace lantern
kubectl -n lantern create secret generic lantern-edge \
--from-literal=username='<org>.<credential id>' \
--from-literal=password='<password>'The forwarder reads the Secret by name. If you manage secrets with Sealed
Secrets, SOPS or External Secrets, produce the same Secret with keys
username and password.
2. Add the chart repositories
helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update3. kube-state-metrics
fullnameOverride: kube-state-metrics
# What Lantern reads; everything else is series nobody queries.
collectors: [cronjobs, daemonsets, deployments, horizontalpodautoscalers, jobs, namespaces, nodes, persistentvolumeclaims, persistentvolumes, poddisruptionbudgets, pods, statefulsets]
metricAllowlist:
- ^kube_pod_(info|start_time|status_(phase|ready|qos_class))$
- ^kube_pod_(init_)?container_(info|resource_(requests|limits)|status_(ready|restarts_total|running|waiting_reason|terminated_reason))$
- ^kube_pod_container_status_last_terminated_(reason|timestamp|exitcode)$
- ^kube_job_(owner|status_failed)$
- ^kube_(node|deployment|daemonset|statefulset|cronjob|namespace|persistentvolume|persistentvolumeclaim|horizontalpodautoscaler|poddisruptionbudget)_.+$helm upgrade --install kube-state-metrics prometheus-community/kube-state-metrics \
-n lantern --version 8.6.0 -f kube-state-metrics.yamlThe allowlist keeps the families Lantern's pod, node and workload pages and
its job monitors read, which is about half of what kube-state-metrics exports
by default. fullnameOverride gives it the Service name the forwarder
scrapes.
4. The forwarder
Set k8s.cluster.name to the name the cluster should have in Lantern, and
the default environment if this cluster isn't production:
# lantern-forwarder: the one collector in the cluster that talks to Lantern.
# The agents send it everything; it also scrapes kube-state-metrics and
# watches the cluster's Kubernetes events, stamps the cluster's name, and
# exports to your org's OTLP edge with the org's edge credential.
mode: deployment
fullnameOverride: lantern-forwarder
replicaCount: 1
image:
repository: otel/opentelemetry-collector-contrib
tag: 0.116.1
command:
name: otelcol-contrib
extraEnvs:
- name: LANTERN_EDGE_USER
valueFrom:
secretKeyRef: {name: lantern-edge, key: username}
- name: LANTERN_EDGE_PASSWORD
valueFrom:
secretKeyRef: {name: lantern-edge, key: password}
# k8sobjects watches events cluster-wide.
clusterRole:
create: true
rules:
- apiGroups: [events.k8s.io]
resources: [events]
verbs: [get, list, watch]
ports:
jaeger-compact: {enabled: false}
jaeger-thrift: {enabled: false}
jaeger-grpc: {enabled: false}
zipkin: {enabled: false}
resources:
requests: {cpu: 100m, memory: 256Mi}
limits: {memory: 1Gi}
alternateConfig:
extensions:
health_check:
endpoint: ${env:MY_POD_IP}:13133
basicauth/lantern:
client_auth:
username: ${env:LANTERN_EDGE_USER}
password: ${env:LANTERN_EDGE_PASSWORD}
receivers:
otlp:
protocols:
grpc: {endpoint: "${env:MY_POD_IP}:4317"}
http: {endpoint: "${env:MY_POD_IP}:4318"}
prometheus:
config:
scrape_configs:
- job_name: kube-state-metrics
scrape_interval: 30s
static_configs:
- targets: ["kube-state-metrics:8080"]
k8sobjects:
auth_type: serviceAccount
objects:
- group: events.k8s.io
name: events
mode: watch
processors:
memory_limiter:
check_interval: 1s
limit_percentage: 80
spike_limit_percentage: 20
resource/cluster:
attributes:
- key: k8s.cluster.name
value: my-cluster # how Lantern names this cluster
action: upsert
# The environment, for records whose SDK did not say. Lantern
# assumes prod otherwise.
- key: deployment.environment.name
value: prod
action: insert
# Lantern files records marked this way as Kubernetes events.
resource/k8s-events:
attributes:
- key: lantern.source
value: k8s-events
action: upsert
batch:
timeout: 5s
exporters:
otlphttp/lantern:
endpoint: https://otlp.lntrn.dev
auth:
authenticator: basicauth/lantern
sending_queue:
enabled: true
queue_size: 5000
retry_on_failure:
enabled: true
max_elapsed_time: 0
service:
extensions: [health_check, basicauth/lantern]
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, resource/cluster, batch]
exporters: [otlphttp/lantern]
metrics:
receivers: [otlp, prometheus]
processors: [memory_limiter, resource/cluster, batch]
exporters: [otlphttp/lantern]
logs:
receivers: [otlp]
processors: [memory_limiter, resource/cluster, batch]
exporters: [otlphttp/lantern]
logs/events:
receivers: [k8sobjects]
processors: [memory_limiter, resource/cluster, resource/k8s-events, batch]
exporters: [otlphttp/lantern]helm upgrade --install lantern-forwarder open-telemetry/opentelemetry-collector \
-n lantern --version 0.113.1 -f forwarder.yamlRecords marked lantern.source: k8s-events are filed as Kubernetes events,
which the pod and node pages show (OOMKilled, BackOff, FailedScheduling
and the rest). They are a separate signal from logs.
The queue is in memory: a Lantern outage longer than the queue holds, or a
forwarder restart during one, loses what was queued. To survive both, give
the forwarder a volume and a file_storage extension, and set the queue's
storage to it.
5. The agent
# lantern-agent: one collector per node. It reads every container's stdout,
# takes OTLP from the node's pods, collects node and pod metrics, stamps each
# record with the pod it came from, and passes everything to the forwarder.
mode: daemonset
fullnameOverride: lantern-agent
image:
repository: otel/opentelemetry-collector-contrib
tag: 0.116.1
command:
name: otelcol-contrib
# A Service in front of the DaemonSet, node-local: a pod's OTLP stays on its
# own node's agent, which is the one that can name the pod.
service:
enabled: true
internalTrafficPolicy: Local
clusterRole:
create: true
rules:
- apiGroups: [""]
resources: [pods, namespaces, nodes]
verbs: [get, list, watch]
- apiGroups: [apps]
resources: [replicasets, deployments, daemonsets, statefulsets]
verbs: [get, list, watch]
- apiGroups: [batch]
resources: [jobs, cronjobs]
verbs: [get, list, watch]
- apiGroups: [""]
resources: [nodes/stats, nodes/proxy]
verbs: [get]
extraEnvs:
- name: K8S_NODE_NAME
valueFrom:
fieldRef: {fieldPath: spec.nodeName}
- name: K8S_NODE_IP
valueFrom:
fieldRef: {fieldPath: status.hostIP}
extraVolumes:
- name: varlogpods
hostPath: {path: /var/log/pods}
- name: hostfs
hostPath: {path: /}
extraVolumeMounts:
- name: varlogpods
mountPath: /var/log/pods
readOnly: true
- name: hostfs
mountPath: /hostfs
readOnly: true
ports:
jaeger-compact: {enabled: false}
jaeger-thrift: {enabled: false}
jaeger-grpc: {enabled: false}
zipkin: {enabled: false}
resources:
requests: {cpu: 100m, memory: 256Mi}
limits: {memory: 512Mi}
alternateConfig:
extensions:
health_check:
endpoint: ${env:MY_POD_IP}:13133
receivers:
otlp:
protocols:
grpc: {endpoint: "${env:MY_POD_IP}:4317"}
http: {endpoint: "${env:MY_POD_IP}:4318"}
filelog:
include: [/var/log/pods/*/*/*.log]
exclude:
# The collectors' own output.
- /var/log/pods/*/opentelemetry-collector/*.log
start_at: end
include_file_path: true
operators:
# The container runtime's line format, and the pod, namespace and
# container from the file's path.
- type: container
id: container-parser
# A JSON line's fields become attributes: its level the severity,
# its trace_id and span_id the record's trace context, its time the
# timestamp. Numeric levels are pino's.
- type: json_parser
id: parse-json
if: body matches "^{"
parse_from: body
parse_to: attributes
on_error: send_quiet
severity:
parse_from: attributes.level
overwrite_text: true
mapping:
trace: 10
debug: 20
info: 30
warn: 40
error: 50
fatal: 60
trace:
trace_id: {parse_from: attributes.trace_id}
span_id: {parse_from: attributes.span_id}
timestamp:
parse_from: attributes.time
layout_type: gotime
layout: 2006-01-02T15:04:05.999999999Z07:00
# The message replaces the raw JSON as the body.
- type: move
id: promote-msg
if: type(attributes.msg) == "string" and attributes.msg != ""
from: attributes.msg
to: body
- type: move
id: promote-message
if: body matches "^{" and type(attributes.message) == "string" and attributes.message != ""
from: attributes.message
to: body
# Plain-text lines that start with a level: "ERROR …", "[warn] …".
- type: regex_parser
id: level-from-prefix
if: attributes.level == nil and not (body matches "^{")
regex: '^\[?(?P<level>(?i:trace|debug|info|warn|warning|error|fatal))\]?[\s:]'
parse_from: body
parse_to: attributes
on_error: send_quiet
severity:
parse_from: attributes.level
overwrite_text: true
# The fields above now live in the record's own columns.
- type: remove
if: attributes.level != nil
field: attributes.level
- type: remove
if: attributes.time != nil
field: attributes.time
- type: remove
if: attributes.trace_id != nil
field: attributes.trace_id
- type: remove
if: attributes.span_id != nil
field: attributes.span_id
- type: remove
if: attributes.logtag != nil
field: attributes.logtag
kubeletstats:
auth_type: serviceAccount
collection_interval: 30s
endpoint: https://${env:K8S_NODE_IP}:10250
insecure_skip_verify: true
metric_groups: [node, pod, container]
hostmetrics:
collection_interval: 30s
root_path: /hostfs
scrapers:
cpu: {}
load: {}
memory: {}
filesystem:
exclude_fs_types:
match_type: strict
fs_types: [autofs, binfmt_misc, bpf, cgroup2, configfs, debugfs, devpts, devtmpfs, fusectl, hugetlbfs, mqueue, nsfs, overlay, proc, procfs, pstore, rpc_pipefs, securityfs, selinuxfs, squashfs, sysfs, tracefs]
processors:
memory_limiter:
check_interval: 5s
limit_percentage: 80
spike_limit_percentage: 25
# The pod each record came from, and its workload: Lantern's k8s.* keys.
k8sattributes:
passthrough: false
filter:
node_from_env_var: K8S_NODE_NAME
extract:
metadata:
- k8s.namespace.name
- k8s.pod.name
- k8s.pod.uid
- k8s.node.name
- k8s.deployment.name
- k8s.statefulset.name
- k8s.daemonset.name
- k8s.job.name
- k8s.cronjob.name
- k8s.container.name
- container.image.name
- container.image.tag
labels:
- key: lantern.io/service
from: pod
tag_name: lantern.label.service
- key: app.kubernetes.io/name
from: pod
tag_name: lantern.label.app_name
pod_association:
- sources:
- from: resource_attribute
name: k8s.pod.ip
- sources:
- from: resource_attribute
name: k8s.pod.name
- from: resource_attribute
name: k8s.namespace.name
- sources:
- from: connection
# One service.name for every signal: the pod's lantern.io/service label
# wins; then what the SDK sent; then, for stdout and kubelet metrics,
# app.kubernetes.io/name, the owning workload, or the container.
transform/service-name:
error_mode: ignore
trace_statements: &service-name
- context: resource
statements:
- set(attributes["service.name"], attributes["lantern.label.service"]) where attributes["lantern.label.service"] != nil
- set(attributes["service.name"], attributes["lantern.label.app_name"]) where attributes["service.name"] == nil and attributes["lantern.label.app_name"] != nil
- set(attributes["service.name"], attributes["k8s.deployment.name"]) where attributes["service.name"] == nil and attributes["k8s.deployment.name"] != nil
- set(attributes["service.name"], attributes["k8s.statefulset.name"]) where attributes["service.name"] == nil and attributes["k8s.statefulset.name"] != nil
- set(attributes["service.name"], attributes["k8s.daemonset.name"]) where attributes["service.name"] == nil and attributes["k8s.daemonset.name"] != nil
- set(attributes["service.name"], attributes["k8s.cronjob.name"]) where attributes["service.name"] == nil and attributes["k8s.cronjob.name"] != nil
- set(attributes["service.name"], attributes["k8s.container.name"]) where attributes["service.name"] == nil and attributes["k8s.container.name"] != nil
- set(attributes["service.version"], attributes["container.image.tag"]) where attributes["service.version"] == nil and attributes["container.image.tag"] != nil
- delete_key(attributes, "lantern.label.service")
- delete_key(attributes, "lantern.label.app_name")
metric_statements: *service-name
log_statements: *service-name
batch:
send_batch_size: 8192
timeout: 2s
exporters:
otlp/forwarder:
endpoint: lantern-forwarder:4317
tls:
insecure: true
retry_on_failure:
enabled: true
service:
extensions: [health_check]
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, k8sattributes, transform/service-name, batch]
exporters: [otlp/forwarder]
metrics:
receivers: [otlp, kubeletstats, hostmetrics]
processors: [memory_limiter, k8sattributes, transform/service-name, batch]
exporters: [otlp/forwarder]
logs:
receivers: [otlp, filelog]
processors: [memory_limiter, k8sattributes, transform/service-name, batch]
exporters: [otlp/forwarder]helm upgrade --install lantern-agent open-telemetry/opentelemetry-collector \
-n lantern --version 0.113.1 -f agent.yamlThe agent finds each record's pod differently by source:
- OTLP: by the connection's source address. That is why the Service is node-local: a pod's OTLP must reach the agent on its own node, the only one that knows its pods.
- stdout: by the log file's path.
A pod that is only a few seconds old may send its first spans before the
agent has seen it. Those spans arrive without k8s.* keys.
6. Point your applications at the agent
Applications send OTLP/HTTP to the agent, unauthenticated, inside the cluster:
env:
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://lantern-agent.lantern.svc:4318
- name: OTEL_EXPORTER_OTLP_PROTOCOL
value: http/protobuf
- name: OTEL_SERVICE_NAME
value: checkout-api
- name: OTEL_RESOURCE_ATTRIBUTES
value: service.namespace=checkout,service.version=1.4.2
# Logs go to stdout, where the agent reads them; don't also export them.
- name: OTEL_LOGS_EXPORTER
value: none(gRPC on :4317 works too, inside the cluster.) The SDK setup is on the
Go and Node.js pages; only the
endpoint changes, and there is no OTEL_EXPORTER_OTLP_HEADERS.
Write logs to stdout as JSON with level, msg, time, trace_id and
span_id. Plain-text lines are kept too, with a severity when they start
with one (ERROR …, [warn] …).
What Lantern gets from the cluster
| Key | Set by | From |
|---|---|---|
k8s.cluster.name | forwarder | forwarder.yaml |
k8s.namespace.name, k8s.pod.name, k8s.node.name, k8s.container.name | agent | the pod the record came from |
k8s.deployment.name, k8s.statefulset.name, k8s.daemonset.name, k8s.job.name, k8s.cronjob.name | agent | the pod's owner |
service.name | the app, or the agent | the pod label lantern.io/service if set; else the SDK's service.name; else app.kubernetes.io/name, the workload, or the container |
service.version | the app, or the agent | the SDK; else the container image's tag |
deployment.environment | the app, or the forwarder | deployment.environment.name from the SDK; else a namespace ending -stage is stage; else the forwarder's default |
org.id | Lantern | the edge credential, always |
Label a workload's pods with lantern.io/service: <name> to give its spans,
logs and metrics the same service.name whatever its SDK says. An
uninstrumented workload's stdout and kubelet metrics then appear under that
name too.
Verify
Every pod should be running, and the forwarder's log should show no
401 or export errors:
kubectl -n lantern get pods
kubectl -n lantern logs deploy/lantern-forwarder --tail=50Then, within a minute or two, from the CLI (or the console's Logs, Metrics and Infrastructure pages):
# container stdout, by namespace
lntrn --org <org> query logs 'k8s.cluster.name:my-cluster' --since 10m --agg count --group-by k8s.namespace.name --table
# kube-state-metrics
lntrn --org <org> query metrics --agg max --of kube_pod_status_ready --group-by k8s.namespace.name --since 10m --table
# kubelet metrics
lntrn --org <org> query metrics --agg avg --of k8s_pod_memory_working_set_bytes --group-by k8s.pod.name --since 10m --table
# Kubernetes events
lntrn --org <org> query events --since 10mA forwarder log line with 401 means the Secret's username or password is
wrong, or the credential was revoked. A line with 429 means the forwarder
is sending more often than the credential's rate allows; raise its batch
timeout.
Removing it
helm -n lantern uninstall lantern-agent lantern-forwarder kube-state-metrics
kubectl delete namespace lanternThen revoke the cluster's edge credential. What the cluster sent stays in Lantern until it ages out.
Node.js
Send traces, metrics and logs from a Node.js server or script to Lantern with the OpenTelemetry Node SDK, automatic instrumentation and pino, over OTLP/HTTP.
Browsers and React
Report a web app's errors, failed requests, web vitals and spans to Lantern with @lntrn/web, upload source maps so its stacks read as source, and send from React Native with a device token.