在客户端读取 kubernetes pod 的日志时找不到资源
Resource not found while reading logs for kubernetes pod in client go
首先创建 pod 部署
if pod, err = clientset.CoreV1().Pods(namespace).Create(&v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "mithu",
Namespace: namespace,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Name: "mithu",
Image: "test-app",
ImagePullPolicy: "IfNotPresent",
Ports: []v1.ContainerPort{
v1.ContainerPort{
Name: "local",
HostPort: 8081,
ContainerPort: 8081,
Protocol: v1.ProtocolTCP,
},
},
Env: []v1.EnvVar{
v1.EnvVar{
Name: "ASD",
Value: "",
},
},
},
},
RestartPolicy: v1.RestartPolicyOnFailure,
},
}); err == nil {
fmt.Println("Pod created succesfuly with status", pod.Name)
} else {
fmt.Println("Error1: ", err)
}
time.Sleep(5000)
现在 pod 已经部署好了,我正在阅读日志
req := clientset.RESTClient().Get().
Namespace(namespace).
Name(pod.Name).
Resource("pods").
SubResource("log").
Param("follow", "").
Param("container", "").
Param("previous", "").
Param("timestamps", "")
readCloser, err := req.Stream()
if err != nil {
fmt.Println("Error2: ", err)
} else {
buf := new(bytes.Buffer)
_, err = io.Copy(buf, readCloser)
fmt.Println("log : ", buf.String())
}
已阅读 中的日志
Pod 已成功创建,但客户端似乎找不到资源,出现 Error2: the server could not find the requested resource (get pods.meta.k8s.io mithu)
错误
如果我尝试从命令行获取日志,没有问题。我认为主要问题在 pods.meta.k8s.io 和命令行参数 pods
之间
这对我有用:
req := clientset.CoreV1().Pods(namespace).GetLogs(
pod.Name,
&v1.PodLogOptions{},
)
readCloser, err := req.Stream()
if err != nil {
fmt.Println("Error2: ", err)
} else {
buf := new(bytes.Buffer)
_, err = io.Copy(buf, readCloser)
fmt.Println("log : ", buf.String())
}
首先创建 pod 部署
if pod, err = clientset.CoreV1().Pods(namespace).Create(&v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "mithu",
Namespace: namespace,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Name: "mithu",
Image: "test-app",
ImagePullPolicy: "IfNotPresent",
Ports: []v1.ContainerPort{
v1.ContainerPort{
Name: "local",
HostPort: 8081,
ContainerPort: 8081,
Protocol: v1.ProtocolTCP,
},
},
Env: []v1.EnvVar{
v1.EnvVar{
Name: "ASD",
Value: "",
},
},
},
},
RestartPolicy: v1.RestartPolicyOnFailure,
},
}); err == nil {
fmt.Println("Pod created succesfuly with status", pod.Name)
} else {
fmt.Println("Error1: ", err)
}
time.Sleep(5000)
现在 pod 已经部署好了,我正在阅读日志
req := clientset.RESTClient().Get().
Namespace(namespace).
Name(pod.Name).
Resource("pods").
SubResource("log").
Param("follow", "").
Param("container", "").
Param("previous", "").
Param("timestamps", "")
readCloser, err := req.Stream()
if err != nil {
fmt.Println("Error2: ", err)
} else {
buf := new(bytes.Buffer)
_, err = io.Copy(buf, readCloser)
fmt.Println("log : ", buf.String())
}
已阅读 Error2: the server could not find the requested resource (get pods.meta.k8s.io mithu)
如果我尝试从命令行获取日志,没有问题。我认为主要问题在 pods.meta.k8s.io 和命令行参数 pods
之间这对我有用:
req := clientset.CoreV1().Pods(namespace).GetLogs(
pod.Name,
&v1.PodLogOptions{},
)
readCloser, err := req.Stream()
if err != nil {
fmt.Println("Error2: ", err)
} else {
buf := new(bytes.Buffer)
_, err = io.Copy(buf, readCloser)
fmt.Println("log : ", buf.String())
}