我正在使用 kube go 客户端和 kube api 来访问 kube 数据。我目前没有找到任何针对特定 pod 日志的 api 调用。
kubectl logs pod-name
返回特定 Pod 的日志。我如何使用 go 客户端执行此操作? 我使用的是 kubernetes v1.0.6。
我可以通过使用获取 Pod
client.Pods("namespace").Get("pod-name")
最佳答案
Client Go为此提供了一个函数GetLogs
,该函数已在 How to get logs from kubernetes using Go? 中得到解答。
了解 kubectl 如何实现其命令对于了解如何使用客户端库很有帮助。在这种情况下,kubectl's implementation of the logs command看起来像这样:
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
关于用于特定 pod 日志的 Kubernetes go 客户端 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060944/