Kubernetes使用客户端API记录特定Pod [英] Kubernetes go client api for log of a particular pod

查看:389
本文介绍了Kubernetes使用客户端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 Client执行此操作? 我正在使用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.

我可以通过使用

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

推荐答案

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

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使用客户端API记录特定Pod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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