Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes go client api for log of a particular pod

Tags:

kubernetes

I am using kube go client with kube api to access kube data. I am currently not finding any api call for logs of a particular pod.

kubectl logs pod-name

returns the logs for a particular pod. How do I do this using go client? I am using v1.0.6 of kubernetes.

I can get the pod by using

client.Pods("namespace").Get("pod-name")
like image 966
sadlil Avatar asked Oct 07 '15 03:10

sadlil


People also ask

How do you check logs of pods in Kubernetes?

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 you get old logs of pods in Kubernetes?

In case that a pod restarts, and you wanted to check the logs of the previous run, what you need to do is to use the --previous flag: kubectl logs nginx-7d8b49557c-c2lx9 --previous.


1 Answers

Client Go has offered a function GetLogs for this, which has been answered in How to get logs from kubernetes using Go?


Looking at how kubectl implements its commands can be helpful when getting a feel for how to use the client library. In this case, kubectl's implementation of the logs command looks like this:

    req := client.RESTClient.Get().
        Namespace(namespace).
        Name(podID).
        Resource("pods").
        SubResource("log").
        Param("follow", strconv.FormatBool(logOptions.Follow)).
        Param("container", logOptions.Container).
        Param("previous", strconv.FormatBool(logOptions.Previous)).
        Param("timestamps", strconv.FormatBool(logOptions.Timestamps))

    if logOptions.SinceSeconds != nil {
        req.Param("sinceSeconds", strconv.FormatInt(*logOptions.SinceSeconds, 10))
    }
    if logOptions.SinceTime != nil {
        req.Param("sinceTime", logOptions.SinceTime.Format(time.RFC3339))
    }
    if logOptions.LimitBytes != nil {
        req.Param("limitBytes", strconv.FormatInt(*logOptions.LimitBytes, 10))
    }
    if logOptions.TailLines != nil {
        req.Param("tailLines", strconv.FormatInt(*logOptions.TailLines, 10))
    }
    readCloser, err := req.Stream()
    if err != nil {
        return err
    }

    defer readCloser.Close()
    _, err = io.Copy(out, readCloser)
    return err
like image 173
Alex Robinson Avatar answered Oct 12 '22 05:10

Alex Robinson