Home > other >  can bash use kubectl get pods run a log watch sequentially?
can bash use kubectl get pods run a log watch sequentially?

Time:02-10

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

CodePudding user response:

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 ...
    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.

  • Related