0

Note: this is more a bash/shell script issue than Kubernetes.

So I have one replica of a pod, and so kubectl get pods will only return one active replica.

Conceptually what I want to do is simple. I want to do a kubectl logs my-pod-nnn1 with a watcher, HOWEVER when that pod terminates I want to stream a message "my-pod-nnn1 terminated, now logging my-pod-nnn2", and then stream the new logs. This is a fairly complex process (for me) and I'm wondering what approach I could take, and if this is possible (or perhaps not necessary) with multi-threading of some kind (which I have not done). Thanks

Oliver Williams
  • 5,966
  • 7
  • 36
  • 78

1 Answers1

1

as a rough outline for what you'll need to do, if I've read your needs right

slm-log-continue() {
  while true ; do
    PODLINE=$(kubectl get pod | grep regional-wkspce | grep Running | awk '/^regional-wkspce-.*/{print $1}')
    if [[ -z "$PODLINE" ]]; then
        echo no pod currently active, waiting 5 seconds ...
        sleep 5
    else
        echo "----- logging for $PODLINE -----"
        kubectl logs -f $PODLINE
        echo "----- pod $PODLINE disconnected -----"
    fi
  done
}

assuming kubectl logs terminates after the pod does and it's received the end of the logs ( I've not tested ), something like that should do what you need without any fancy multithreading. it will just find the current pod using whatever regex against the get pods output, extract the name and then spit out logs until it dies.

Oliver Williams
  • 5,966
  • 7
  • 36
  • 78
Michael Speer
  • 4,656
  • 2
  • 19
  • 10
  • I have tested this and it works. Thanks. Of course it would only work for a case where there is only one Running replica present. Any ideas on how to stream multiple logs at a time? Obviously each line would need to be somehow parsed or intercepted and tagged with some initial string for source, or it would be confusing which pod they came from. – Oliver Williams Feb 10 '22 at 03:29
  • @OliverWilliams your edit removed the actual 5 second wait. hope you remembered it locally :P – Michael Speer Feb 11 '22 at 12:34
  • @OliverWilliams https://stackoverflow.com/questions/33069736/how-do-i-get-logs-from-all-pods-of-a-kubernetes-replication-controller it looks like you can use selectors to stream logs from multiple pods. good luck – Michael Speer Feb 11 '22 at 12:38