Try Sailor Cloud - 25% off!

Claim Now
Back to all posts

Kubernetes Logging and Monitoring: Observability Guide

Kubernetes Logging and Monitoring: Observability Guide

Observability is crucial for understanding what’s happening in your cluster and applications.

Viewing Pod Logs

# Basic logs
kubectl logs my-pod

# Follow logs
kubectl logs -f my-pod

# Previous container (after restart)
kubectl logs my-pod --previous

# Specific container in multi-container pod
kubectl logs my-pod -c sidecar

# Logs from all pods with label
kubectl logs -l app=nginx

# Last N lines
kubectl logs my-pod --tail=100

# Since timestamp
kubectl logs my-pod --since=1h

Container-Level Logging

Log to stdout/stderr

Applications should log to stdout/stderr for Kubernetes to capture:

# Don't log to file
CMD ["./app", "-log", "/dev/stdout"]

Sidecar Log Shipping

spec:
  containers:
  - name: app
    image: my-app
    volumeMounts:
    - name: logs
      mountPath: /var/log/app
  - name: log-shipper
    image: fluent-bit
    volumeMounts:
    - name: logs
      mountPath: /var/log/app
      readOnly: true
  volumes:
  - name: logs
    emptyDir: {}

Metrics Server

# Install metrics server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

# View node resources
kubectl top nodes

# View pod resources
kubectl top pods
kubectl top pods -A --sort-by=memory

Prometheus Stack

# Install using Helm
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/kube-prometheus-stack

Key Metrics to Monitor

Cluster Level

  • Node CPU/Memory usage
  • Pod count per node
  • Cluster resource utilization

Pod Level

  • CPU/Memory usage vs requests/limits
  • Restart count
  • Container states

Application Level

  • Request latency
  • Error rates
  • Throughput

Centralized Logging Stack (EFK)

  • Elasticsearch: Log storage
  • Fluent Bit/Fluentd: Log collection
  • Kibana: Visualization
helm repo add elastic https://helm.elastic.co
helm install elasticsearch elastic/elasticsearch
helm install kibana elastic/kibana

Debugging Commands

# Events sorted by time
kubectl get events --sort-by='.lastTimestamp'

# Describe for detailed info
kubectl describe pod my-pod

# Resource usage
kubectl top pods --containers

Practice Logging

Logging and monitoring appear in CKA exams. Practice at Sailor.sh.

Start Free Practice