Kubernetes go 客户端 api 用于特定 pod 的日志 [英] Kubernetes go client api for log of a particular pod

查看:52
本文介绍了Kubernetes go 客户端 api 用于特定 pod 的日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 kube api 的 kube go 客户端来访问 kube 数据.我目前没有找到任何对特定 pod 日志的 api 调用.

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

返回特定 pod 的日志.我如何使用 go 客户端执行此操作?我使用的是 v1.0.6 的 kubernetes.

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

我可以通过使用获取 pod

I can get the pod by using

client.Pods("namespace").Get("pod-name")

推荐答案

Client Go 为此提供了一个函数 GetLogs,已在 如何使用golang从kubernetes获取日志?

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

了解如何使用客户端库时,查看 kubectl 如何实现其命令会很有帮助.在这种情况下,logbectl 命令的实现 看起来像这样:

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

这篇关于Kubernetes go 客户端 api 用于特定 pod 的日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆