Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get logs from kubernetes using Go?

Tags:

go

kubernetes

I'm looking for the solution of how to get logs from a pod in Kubernetes cluster using Go. I've looked at "https://github.com/kubernetes/client-go" and "https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client", but couldn't understand how to use them for this purpose. I have no issues getting information of a pod or any other object in K8S except for logs.

For example, I'm using Get() from "https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#example-Client--Get" to get K8S job info:

found := &batchv1.Job{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: job.Name, Namespace: job.Namespace}, found)

Please share of how you get pod's logs nowadays. Any suggestions would be appreciated!

Update: The solution provided in Kubernetes go client api for log of a particular pod is out of date. It have some tips, but it is not up to date with current libraries.

like image 657
Stepan Maksimchuk Avatar asked Dec 19 '18 13:12

Stepan Maksimchuk


People also ask

How do I find my Kubernetes logs?

To get Kubectl pod logs, you can access them by adding the -p flag. Kubectl will then get all of the logs stored for the pod. This includes lines that were emitted by containers that were terminated.

How do I get a log file from Kubernetes pod?

If you run kubectl logs pod_name , a list of containers in the pod is displayed. You can use one of the container names to get the logs for that specific container.

How do you check k8 pod logs?

Checking the logs of a running pod All that you need to do to do that is to run the following command: kubectl logs nginx-7d8b49557c-c2lx9.


1 Answers

The answer by anon_coword got me interested, in getting logs in a bit more complicated case:

  1. I want to preform the action multiple times, and check the logs multiple times.
  2. I want to have many pods that will react the same way.

Here are a few examples: https://github.com/nwaizer/GetPodLogsEfficiently One example is:

package main

import (
    "GetPodLogsEfficiently/client"
    "GetPodLogsEfficiently/utils"
    "bufio"
    "context"
    "fmt"
    corev1 "k8s.io/api/core/v1"
    "time"
)

func GetPodLogs(cancelCtx context.Context, PodName string) {
    PodLogsConnection := client.Client.Pods(utils.Namespace).GetLogs(PodName, &corev1.PodLogOptions{
        Follow:    true,
        TailLines: &[]int64{int64(10)}[0],
    })
    LogStream, _ := PodLogsConnection.Stream(context.Background())
    defer LogStream.Close()

    reader := bufio.NewScanner(LogStream)
    var line string
    for {
        select {
        case <-cancelCtx.Done():
            break
        default:
            for reader.Scan() {
                line = reader.Text()
                fmt.Printf("Pod: %v line: %v\n", PodName, line)
            }
        }
    }
}
func main() {
    ctx := context.Background()
    cancelCtx, endGofunc := context.WithCancel(ctx)
    for _, pod := range utils.GetPods().Items {
        fmt.Println(pod.Name)
        go GetPodLogs(cancelCtx, pod.Name)
    }
    time.Sleep(10 * time.Second)
    endGofunc()
}
like image 97
Niv Waizer Avatar answered Sep 29 '22 08:09

Niv Waizer