从 .json 或 .yaml 规范实例化资源时,Kubernetes 中接收错误通知的最佳方式?
best way in Kubernetes to receive notifications of errors when instantiating resources from .json or .yaml specifications?
我正在使用 fabric8 在 Kubernetes 之上开发一个集群管理层,我很困惑
'official' API 是在出错的时候获取错误通知
实例化 pods/rep 控制器和服务等
在 "Pod Deployment Code" 部分,我有一个我们为 pods 所做的精简版本。在活动中
一切正常,我们的代码是好的。我们依赖设置 'watches' 为您
可以在方法中看到deployPodWithWatch
。我在给定的 eventReceived
回调中所做的一切
是打印事件,但我们的真实代码将像这样分解通知:
got action:
MODIFIED /
Pod(apiVersion=v1, kind=Pod, metadata=...etc etc
status=PodStatus(
conditions=[
并挑选出 Pod 的 'status' 元素,当我们得到 PodCondition(status=True, type=Ready) 时,我们知道
我们的 pod 已成功部署。
在快乐路径的情况下,这很有效。你实际上可以 运行 变量提供的代码
k8sUrl 设置为适合您站点的 url(希望您的 k8s
安装不需要特定于站点的身份验证,因此我没有为此提供代码)。
但是,假设您将变量 imageName
更改为 "nginBoo"。没有publicdocker
该名称的图像,因此在您 运行 代码之后,将您的 kubernetes 上下文设置为命名空间 "junk",
并做一个
describe pod podboy
您将在末尾看到两条状态消息,其中包含以下 Reason/Message 值
Reason message
failedSync Error syncing pod, skipping...
failed Failed to pull image "nginBoo": API error (500):
Error parsing reference: "nginBoo"
is not a valid repository/tag
我想实现一个监视回调,以便它捕获这些类型的错误。然而,
我唯一看到的是 'MODIFIED' 事件,其中 Pod 具有这样的字段:
state=ContainerState(running=null, terminated=null,
waiting=ContainerStateWaiting(
reason=API error (500):
Error parsing reference:
"nginBoo" is not a valid repository/tag
我想我可以查找包含字符串 'API error' 的原因代码,但这似乎
非常依赖于实现的 hack——它可能不会涵盖所有情况,也许会
随着未来的版本在我脚下改变。我想要更多 'official' 方式
弄清楚是否有错误,但我的搜索已经枯竭——所以我谦虚地
请求所有 k8s 专家的指导。谢谢!
Pod 部署代码
import com.fasterxml.jackson.databind.ObjectMapper
import scala.collection.JavaConverters._
import com.ning.http.client.ws.WebSocket
import com.typesafe.scalalogging.StrictLogging
import io.fabric8.kubernetes.api.model.{DoneableNamespace, Namespace, Pod, ReplicationController}
import io.fabric8.kubernetes.client.DefaultKubernetesClient.ConfigBuilder
import io.fabric8.kubernetes.client.Watcher.Action
import io.fabric8.kubernetes.client.dsl.Resource
import io.fabric8.kubernetes.client.{DefaultKubernetesClient, Watcher}
object ErrorTest extends App with StrictLogging {
// corresponds to --insecure-skip-tls-verify=true, according to io.fabric8.kubernetes.api.model.Cluster
val trustCerts = true
val k8sUrl = "http://localhost:8080"
val namespaceName = "junk" // replace this with name of a namespace that you know exists
val imageName: String = "nginx"
def go(): Unit = {
val kube = getConnection
dumpNamespaces(kube)
deployPodWithWatch(kube, getPod(image = imageName))
}
def deployPodWithWatch(kube: DefaultKubernetesClient, pod: Pod): Unit = {
kube.pods().inNamespace(namespaceName).create(pod) /* create the pod ! */
val podWatchWebSocket: WebSocket = /* create watch on the pod */
kube.pods().inNamespace(namespaceName).withName(pod.getMetadata.getName).watch(getPodWatch)
}
def getPod(image: String): Pod = {
val jsonTemplate =
"""
|{
| "kind": "Pod",
| "apiVersion": "v1",
| "metadata": {
| "name": "podboy",
| "labels": {
| "app": "nginx"
| }
| },
| "spec": {
| "containers": [
| {
| "name": "podboy",
| "image": "<image>",
| "ports": [
| {
| "containerPort": 80,
| "protocol": "TCP"
| }
| ]
| }
| ]
| }
|}
""".
stripMargin
val replacement: String = "image\": \"" + image
val json = jsonTemplate.replaceAll("image\": \"<image>", replacement)
System.out.println("json:" + json);
new ObjectMapper().readValue(json, classOf[Pod])
}
def dumpNamespaces(kube: DefaultKubernetesClient): Unit = {
val namespaceNames = kube.namespaces().list().getItems.asScala.map {
(ns: Namespace) => {
ns.getMetadata.getName
}
}
System.out.println("namespaces are:" + namespaceNames);
}
def getConnection = {
val configBuilder = new ConfigBuilder()
val config =
configBuilder.
trustCerts(trustCerts).
masterUrl(k8sUrl).
build()
new DefaultKubernetesClient(config)
}
def getPodWatch: Watcher[Pod] = {
new Watcher[Pod]() {
def eventReceived(action: Action, watchedPod: Pod) {
System.out.println("got action: " + action + " / " + watchedPod)
}
}
}
go()
}
我建议您查看事件,请参阅 this topic 以获取一些指导。一般来说,每个对象都应该生成您可以观看的事件,并收到此类错误的通知。
我正在使用 fabric8 在 Kubernetes 之上开发一个集群管理层,我很困惑 'official' API 是在出错的时候获取错误通知 实例化 pods/rep 控制器和服务等
在 "Pod Deployment Code" 部分,我有一个我们为 pods 所做的精简版本。在活动中
一切正常,我们的代码是好的。我们依赖设置 'watches' 为您
可以在方法中看到deployPodWithWatch
。我在给定的 eventReceived
回调中所做的一切
是打印事件,但我们的真实代码将像这样分解通知:
got action:
MODIFIED /
Pod(apiVersion=v1, kind=Pod, metadata=...etc etc
status=PodStatus(
conditions=[
并挑选出 Pod 的 'status' 元素,当我们得到 PodCondition(status=True, type=Ready) 时,我们知道 我们的 pod 已成功部署。
在快乐路径的情况下,这很有效。你实际上可以 运行 变量提供的代码 k8sUrl 设置为适合您站点的 url(希望您的 k8s 安装不需要特定于站点的身份验证,因此我没有为此提供代码)。
但是,假设您将变量 imageName
更改为 "nginBoo"。没有publicdocker
该名称的图像,因此在您 运行 代码之后,将您的 kubernetes 上下文设置为命名空间 "junk",
并做一个
describe pod podboy
您将在末尾看到两条状态消息,其中包含以下 Reason/Message 值
Reason message
failedSync Error syncing pod, skipping...
failed Failed to pull image "nginBoo": API error (500):
Error parsing reference: "nginBoo"
is not a valid repository/tag
我想实现一个监视回调,以便它捕获这些类型的错误。然而, 我唯一看到的是 'MODIFIED' 事件,其中 Pod 具有这样的字段:
state=ContainerState(running=null, terminated=null,
waiting=ContainerStateWaiting(
reason=API error (500):
Error parsing reference:
"nginBoo" is not a valid repository/tag
我想我可以查找包含字符串 'API error' 的原因代码,但这似乎 非常依赖于实现的 hack——它可能不会涵盖所有情况,也许会 随着未来的版本在我脚下改变。我想要更多 'official' 方式 弄清楚是否有错误,但我的搜索已经枯竭——所以我谦虚地 请求所有 k8s 专家的指导。谢谢!
Pod 部署代码
import com.fasterxml.jackson.databind.ObjectMapper
import scala.collection.JavaConverters._
import com.ning.http.client.ws.WebSocket
import com.typesafe.scalalogging.StrictLogging
import io.fabric8.kubernetes.api.model.{DoneableNamespace, Namespace, Pod, ReplicationController}
import io.fabric8.kubernetes.client.DefaultKubernetesClient.ConfigBuilder
import io.fabric8.kubernetes.client.Watcher.Action
import io.fabric8.kubernetes.client.dsl.Resource
import io.fabric8.kubernetes.client.{DefaultKubernetesClient, Watcher}
object ErrorTest extends App with StrictLogging {
// corresponds to --insecure-skip-tls-verify=true, according to io.fabric8.kubernetes.api.model.Cluster
val trustCerts = true
val k8sUrl = "http://localhost:8080"
val namespaceName = "junk" // replace this with name of a namespace that you know exists
val imageName: String = "nginx"
def go(): Unit = {
val kube = getConnection
dumpNamespaces(kube)
deployPodWithWatch(kube, getPod(image = imageName))
}
def deployPodWithWatch(kube: DefaultKubernetesClient, pod: Pod): Unit = {
kube.pods().inNamespace(namespaceName).create(pod) /* create the pod ! */
val podWatchWebSocket: WebSocket = /* create watch on the pod */
kube.pods().inNamespace(namespaceName).withName(pod.getMetadata.getName).watch(getPodWatch)
}
def getPod(image: String): Pod = {
val jsonTemplate =
"""
|{
| "kind": "Pod",
| "apiVersion": "v1",
| "metadata": {
| "name": "podboy",
| "labels": {
| "app": "nginx"
| }
| },
| "spec": {
| "containers": [
| {
| "name": "podboy",
| "image": "<image>",
| "ports": [
| {
| "containerPort": 80,
| "protocol": "TCP"
| }
| ]
| }
| ]
| }
|}
""".
stripMargin
val replacement: String = "image\": \"" + image
val json = jsonTemplate.replaceAll("image\": \"<image>", replacement)
System.out.println("json:" + json);
new ObjectMapper().readValue(json, classOf[Pod])
}
def dumpNamespaces(kube: DefaultKubernetesClient): Unit = {
val namespaceNames = kube.namespaces().list().getItems.asScala.map {
(ns: Namespace) => {
ns.getMetadata.getName
}
}
System.out.println("namespaces are:" + namespaceNames);
}
def getConnection = {
val configBuilder = new ConfigBuilder()
val config =
configBuilder.
trustCerts(trustCerts).
masterUrl(k8sUrl).
build()
new DefaultKubernetesClient(config)
}
def getPodWatch: Watcher[Pod] = {
new Watcher[Pod]() {
def eventReceived(action: Action, watchedPod: Pod) {
System.out.println("got action: " + action + " / " + watchedPod)
}
}
}
go()
}
我建议您查看事件,请参阅 this topic 以获取一些指导。一般来说,每个对象都应该生成您可以观看的事件,并收到此类错误的通知。