Loki Stack Management Skill
Manage and analyze Loki log aggregation stack including Loki, Promtail, and related components.
MANDATORY: Discovery-First Pattern
Always check Loki and log agent health before querying log streams.
Phase 1: Discovery
#!/bin/bash
echo "=== Loki Deployment ==="
kubectl get statefulset,deployment -l app.kubernetes.io/name=loki --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,KIND:.kind,NAME:.metadata.name,READY:.status.readyReplicas,REPLICAS:.spec.replicas' 2>/dev/null
echo ""
echo "=== Loki Pods ==="
kubectl get pods -l app.kubernetes.io/name=loki --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase,RESTARTS:.status.containerStatuses[0].restartCount,NODE:.spec.nodeName' 2>/dev/null
echo ""
echo "=== Promtail DaemonSet ==="
kubectl get daemonset -l app.kubernetes.io/name=promtail --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,DESIRED:.status.desiredNumberScheduled,CURRENT:.status.currentNumberScheduled,READY:.status.numberReady' 2>/dev/null
echo ""
echo "=== Log Agents (Promtail/Alloy/FluentBit) ==="
kubectl get pods -l app.kubernetes.io/name=promtail --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName' 2>/dev/null | head -15
kubectl get pods -l app.kubernetes.io/name=alloy --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase' 2>/dev/null | head -10
echo ""
echo "=== Loki Services ==="
kubectl get svc -l app.kubernetes.io/name=loki --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,TYPE:.spec.type,PORTS:.spec.ports[*].port' 2>/dev/null
Phase 2: Analysis
#!/bin/bash
echo "=== Loki Ring Health ==="
LOKI_NS=$(kubectl get pods -l app.kubernetes.io/name=loki --all-namespaces -o jsonpath='{.items[0].metadata.namespace}' 2>/dev/null)
LOKI_POD=$(kubectl get pods -l app.kubernetes.io/name=loki -n "$LOKI_NS" -o jsonpath='{.items[0].metadata.name}' 2>/dev/null)
kubectl exec -n "$LOKI_NS" "$LOKI_POD" -- wget -qO- http://localhost:3100/ready 2>/dev/null
echo ""
echo ""
echo "=== Loki Ingester Status ==="
kubectl exec -n "$LOKI_NS" "$LOKI_POD" -- wget -qO- http://localhost:3100/metrics 2>/dev/null | grep -E "loki_ingester_streams_created_total|loki_distributor_lines_received_total" | head -5
echo ""
echo "=== Promtail Logs (errors) ==="
PROMTAIL_NS=$(kubectl get daemonset -l app.kubernetes.io/name=promtail --all-namespaces -o jsonpath='{.items[0].metadata.namespace}' 2>/dev/null)
kubectl logs daemonset/promtail -n "$PROMTAIL_NS" --tail=20 2>/dev/null | grep -i "error\|fail\|drop" | head -10
echo ""
echo "=== Loki Logs (errors) ==="
kubectl logs -l app.kubernetes.io/name=loki -n "$LOKI_NS" --tail=20 2>/dev/null | grep -i "error\|fail\|warn" | head -10
echo ""
echo "=== Storage Configuration ==="
kubectl get pvc -l app.kubernetes.io/name=loki --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase,CAPACITY:.status.capacity.storage,STORAGECLASS:.spec.storageClassName' 2>/dev/null
echo ""
echo "=== Promtail Nodes Not Running ==="
PROMTAIL_DESIRED=$(kubectl get daemonset -l app.kubernetes.io/name=promtail --all-namespaces -o jsonpath='{.items[0].status.desiredNumberScheduled}' 2>/dev/null)
PROMTAIL_READY=$(kubectl get daemonset -l app.kubernetes.io/name=promtail --all-namespaces -o jsonpath='{.items[0].status.numberReady}' 2>/dev/null)
echo "Desired: $PROMTAIL_DESIRED Ready: $PROMTAIL_READY Missing: $((${PROMTAIL_DESIRED:-0} - ${PROMTAIL_READY:-0}))"
Output Format
- Target ≤50 lines per output
- Use
-o custom-columns for resource listings
- Show DaemonSet coverage (desired vs ready) for log agents
- Aggregate log volume metrics when available
- Never dump full Loki configs -- show storage backend and retention only
Anti-Hallucination Rules
- NEVER assume resource names — always discover via CLI/API in Phase 1 before referencing in Phase 2.
- NEVER fabricate metric names or dimensions — verify against the service documentation or
--help output.
- NEVER mix CLI commands between service versions — confirm which version/API you are targeting.
- ALWAYS use the discovery → verify → analyze chain — every resource referenced must have been discovered first.
- ALWAYS handle empty results gracefully — an empty response is valid data, not an error to retry.
Counter-Rationalizations
| Shortcut |
Counter |
Why |
| "I'll skip discovery and check known resources" |
Always run Phase 1 discovery first |
Resource names change, new resources appear — assumed names cause errors |
| "The user only asked for a quick check" |
Follow the full discovery → analysis flow |
Quick checks miss critical issues; structured analysis catches silent failures |
| "Default configuration is probably fine" |
Audit configuration explicitly |
Defaults often leave logging, security, and optimization features disabled |
| "Metrics aren't needed for this" |
Always check relevant metrics when available |
API/CLI responses show current state; metrics reveal trends and intermittent issues |
| "I don't have access to that" |
Try the command and report the actual error |
Assumed permission failures prevent useful investigation; actual errors are informative |
Common Pitfalls
- Deployment modes: Loki runs as monolithic, simple-scalable, or microservices -- check architecture before debugging
- Promtail coverage: DaemonSet must run on all nodes -- check tolerations for control-plane nodes
- Storage backend: filesystem (single instance), S3/GCS/Azure (scalable) -- check
storage_config in ConfigMap
- Retention: Set via
compactor.retention_enabled and limits_config.retention_period -- not enabled by default
- Rate limiting: Loki enforces per-tenant rate limits -- check for
rate limit errors in distributor logs
- Chunk encoding: snappy is default -- gzip saves space but uses more CPU
- Label cardinality: High-cardinality labels cause performance issues -- avoid dynamic labels like pod names
- Log level: Promtail pipeline stages can filter/transform logs -- check
pipelineStages in config
1---2name: managing-k8s-loki-stack3description: Use when working with K8S Loki Stack — loki stack management for Kubernetes log aggregation. Covers Loki deployment health, Promtail/log agent status, log stream ingestion, storage backend configuration, retention policies, and query performance. Use when debugging log ingestion issues, auditing logging coverage, reviewing Loki storage, or troubleshooting Promtail scrape configs.4---56# Loki Stack Management Skill78Manage and analyze Loki log aggregation stack including Loki, Promtail, and related components.910## MANDATORY: Discovery-First Pattern1112**Always check Loki and log agent health before querying log streams.**1314### Phase 1: Discovery1516```bash17#!/bin/bash1819echo "=== Loki Deployment ==="20kubectl get statefulset,deployment -l app.kubernetes.io/name=loki --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,KIND:.kind,NAME:.metadata.name,READY:.status.readyReplicas,REPLICAS:.spec.replicas' 2>/dev/null2122echo ""23echo "=== Loki Pods ==="24kubectl get pods -l app.kubernetes.io/name=loki --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase,RESTARTS:.status.containerStatuses[0].restartCount,NODE:.spec.nodeName' 2>/dev/null2526echo ""27echo "=== Promtail DaemonSet ==="28kubectl get daemonset -l app.kubernetes.io/name=promtail --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,DESIRED:.status.desiredNumberScheduled,CURRENT:.status.currentNumberScheduled,READY:.status.numberReady' 2>/dev/null2930echo ""31echo "=== Log Agents (Promtail/Alloy/FluentBit) ==="32kubectl get pods -l app.kubernetes.io/name=promtail --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName' 2>/dev/null | head -1533kubectl get pods -l app.kubernetes.io/name=alloy --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase' 2>/dev/null | head -103435echo ""36echo "=== Loki Services ==="37kubectl get svc -l app.kubernetes.io/name=loki --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,TYPE:.spec.type,PORTS:.spec.ports[*].port' 2>/dev/null38```3940### Phase 2: Analysis4142```bash43#!/bin/bash4445echo "=== Loki Ring Health ==="46LOKI_NS=$(kubectl get pods -l app.kubernetes.io/name=loki --all-namespaces -o jsonpath='{.items[0].metadata.namespace}' 2>/dev/null)47LOKI_POD=$(kubectl get pods -l app.kubernetes.io/name=loki -n "$LOKI_NS" -o jsonpath='{.items[0].metadata.name}' 2>/dev/null)48kubectl exec -n "$LOKI_NS" "$LOKI_POD" -- wget -qO- http://localhost:3100/ready 2>/dev/null49echo ""5051echo ""52echo "=== Loki Ingester Status ==="53kubectl exec -n "$LOKI_NS" "$LOKI_POD" -- wget -qO- http://localhost:3100/metrics 2>/dev/null | grep -E "loki_ingester_streams_created_total|loki_distributor_lines_received_total" | head -55455echo ""56echo "=== Promtail Logs (errors) ==="57PROMTAIL_NS=$(kubectl get daemonset -l app.kubernetes.io/name=promtail --all-namespaces -o jsonpath='{.items[0].metadata.namespace}' 2>/dev/null)58kubectl logs daemonset/promtail -n "$PROMTAIL_NS" --tail=20 2>/dev/null | grep -i "error\|fail\|drop" | head -105960echo ""61echo "=== Loki Logs (errors) ==="62kubectl logs -l app.kubernetes.io/name=loki -n "$LOKI_NS" --tail=20 2>/dev/null | grep -i "error\|fail\|warn" | head -106364echo ""65echo "=== Storage Configuration ==="66kubectl get pvc -l app.kubernetes.io/name=loki --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase,CAPACITY:.status.capacity.storage,STORAGECLASS:.spec.storageClassName' 2>/dev/null6768echo ""69echo "=== Promtail Nodes Not Running ==="70PROMTAIL_DESIRED=$(kubectl get daemonset -l app.kubernetes.io/name=promtail --all-namespaces -o jsonpath='{.items[0].status.desiredNumberScheduled}' 2>/dev/null)71PROMTAIL_READY=$(kubectl get daemonset -l app.kubernetes.io/name=promtail --all-namespaces -o jsonpath='{.items[0].status.numberReady}' 2>/dev/null)72echo "Desired: $PROMTAIL_DESIRED Ready: $PROMTAIL_READY Missing: $((${PROMTAIL_DESIRED:-0} - ${PROMTAIL_READY:-0}))"73```7475## Output Format7677- Target ≤50 lines per output78- Use `-o custom-columns` for resource listings79- Show DaemonSet coverage (desired vs ready) for log agents80- Aggregate log volume metrics when available81- Never dump full Loki configs -- show storage backend and retention only8283## Anti-Hallucination Rules84851. **NEVER assume resource names** — always discover via CLI/API in Phase 1 before referencing in Phase 2.862. **NEVER fabricate metric names or dimensions** — verify against the service documentation or `--help` output.873. **NEVER mix CLI commands between service versions** — confirm which version/API you are targeting.884. **ALWAYS use the discovery → verify → analyze chain** — every resource referenced must have been discovered first.895. **ALWAYS handle empty results gracefully** — an empty response is valid data, not an error to retry.9091## Counter-Rationalizations9293| Shortcut | Counter | Why |94|----------|---------|-----|95| "I'll skip discovery and check known resources" | Always run Phase 1 discovery first | Resource names change, new resources appear — assumed names cause errors |96| "The user only asked for a quick check" | Follow the full discovery → analysis flow | Quick checks miss critical issues; structured analysis catches silent failures |97| "Default configuration is probably fine" | Audit configuration explicitly | Defaults often leave logging, security, and optimization features disabled |98| "Metrics aren't needed for this" | Always check relevant metrics when available | API/CLI responses show current state; metrics reveal trends and intermittent issues |99| "I don't have access to that" | Try the command and report the actual error | Assumed permission failures prevent useful investigation; actual errors are informative |100101## Common Pitfalls102103- **Deployment modes**: Loki runs as monolithic, simple-scalable, or microservices -- check architecture before debugging104- **Promtail coverage**: DaemonSet must run on all nodes -- check tolerations for control-plane nodes105- **Storage backend**: filesystem (single instance), S3/GCS/Azure (scalable) -- check `storage_config` in ConfigMap106- **Retention**: Set via `compactor.retention_enabled` and `limits_config.retention_period` -- not enabled by default107- **Rate limiting**: Loki enforces per-tenant rate limits -- check for `rate limit` errors in distributor logs108- **Chunk encoding**: snappy is default -- gzip saves space but uses more CPU109- **Label cardinality**: High-cardinality labels cause performance issues -- avoid dynamic labels like pod names110- **Log level**: Promtail pipeline stages can filter/transform logs -- check `pipelineStages` in config