HAOJX

代码获取pod的日志

字数统计: 99阅读时长: 1 min
2021/11/26 Share
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import (
"context"
"fmt"
"io"
v1 "k8s.io/api/core/v1"
"k8sapi/src/configs"
)

func main(){
client:= configs.NewK8sConfig().InitClient()
//req:= client.CoreV1().Pods("default").GetLogs("deploy-nginx-5745bb45d7-b7wsv",&v1.PodLogOptions{})
//ret ,_:= req.DoRaw(context.Background())
//fmt.Println(string(ret))
req:= client.CoreV1().Pods("kube-system").GetLogs("kube-apiserver-node01",&v1.PodLogOptions{Follow: true})
read,_:= req.Stream(context.Background())
for {
buff:= make([]byte,1024)
n,err:= read.Read(buff)
if err !=nil && err!= io.EOF {
break
}

fmt.Println(string(buff[0:n]))

}

}
CATALOG