Getting Python to log to Kubernetes

By default, python print() statements won’t show up in kubernetes logs.

This can be fixed by setting the environment variable PYTHONUNBUFFERED=1:

resource "kubernetes_deployment" "imgproxy-lite" {
  metadata {
    name = "imgproxy-lite"
  }
  spec {
    replicas = 5
    selector {
      match_labels = {
        app = "imgproxy-lite"
      }
    }
    template {
      metadata {
        labels = {
          app = "imgproxy-lite"
        }
      }
      spec {
        container {
          image = "images.local:5000/imgproxy-lite"
          name  = "imgproxy-lite"
          env {
            name  = "PYTHONUNBUFFERED"
            value = "1"
          }
        }
      }
    }
  }
}

Or by using -u in your container entrypoint/cmd:

cmd [ "python3", "-u", "/usr/sbin/l3lb" ]

Nathan Hensel

on caving, mountaineering, networking, computing, electronics


2024-11-03