从.json或.yaml规范实例化资源时,Kubernetes中接收错误通知的最佳方法是什么? [英] best way in Kubernetes to receive notifications of errors when instantiating resources from .json or .yaml specifications?

查看:92
本文介绍了从.json或.yaml规范实例化资源时,Kubernetes中接收错误通知的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用fabric8在Kubernetes之上开发一个集群管理层,而对于 官方" API是在出现问题时获取错误通知 实例化pod/rep控制器&服务等

I am using fabric8 to develop a cluster management layer on top of Kubernetes, and I am confused as to what the 'official' API is to obtain notifications of errors when things go wrong when instantiating pods/rep controllers & services etc.

在"Pod部署代码"部分中,我简要介绍了我们对Pod所做的工作.万一 一切正常,我们的代码很好.我们依靠为您设置手表" 可以在方法deployPodWithWatch中看到.我在给定的eventReceived回调中所做的一切 是打印事件,但是我们的实际代码会拆分如下通知:

In the section "Pod Deployment Code" I have a stripped down version of what we do for pods. In the event that everything goes correctly, our code is fine. We rely on setting 'watches' as you can see in the method deployPodWithWatch. All I do in the given eventReceived callback is to print the event, but our real code will break apart a notification like this:

got action: 
    MODIFIED / 
      Pod(apiVersion=v1, kind=Pod, metadata=...etc etc
        status=PodStatus(
            conditions=[

并选择Pod的'status'元素,当我们得到PodCondition(status = True,type = Ready)时,我们知道 我们的pod已成功部署.

and pick out the 'status' element of the Pod and when we get PodCondition(status=True, type=Ready), we know that our pod has been successfully deployed.

在幸福的道路上,这很好用.您实际上可以运行变量提供的代码 k8sUrl设置为您网站的正确网址(希望您的k8s 安装不需要特定于站点的身份验证,因此我没有为此提供代码.

In the happy path case this works great. And you can actually run the code supplied with variable k8sUrl set to the proper url for your site (hopefully your k8s installation does not require auth which is site specific so i didn't provide code for that).

但是,假设您将变量imageName更改为"nginBoo".没有公共码头工人 名称的图片,因此在运行代码后,将kubernetes上下文设置为名称空间"junk", 然后做一个

However, suppose you change the variable imageName to "nginBoo". There is no public docker image of that name, so after you run the code, set your kubernetes context to the namespace "junk", and do a

  describe pod podboy

您将在末尾看到两条状态消息,其中包含"Reason/Message"的以下值

you will see two status messages at the end with the following values for 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

我想实现一个监视回调,以便捕获这些类型的错误.然而, 我唯一看到的是已修改"事件,其中Pod的字段如下:

I would like to implement a watch callback so that it catches these types of errors. However, the only thing that I see are 'MODIFIED' events wherein the Pod has a field like this:

 state=ContainerState(running=null, terminated=null, 
        waiting=ContainerStateWaiting(
            reason=API error (500): 
                Error parsing reference: 
                    "nginBoo" is not a valid repository/tag

我想我可以查找包含字符串"API错误"的原因码,但这似乎 很大程度上是依赖于实现的黑客-它可能无法涵盖所有​​情况,也许它将 将来的版本会改变我的脚下.我想要一些更正式"的方式 弄清楚是否有错误,但是我的搜索变得干-了-所以我很谦虚 向您那里的所有k8s专家寻求指导.谢谢!

I suppose I could look for a reason code that contained the string 'API error' but this seems to be very much an implementation-dependent hack -- it might not cover all cases, and maybe it will change under my feet with future versions. I'd like some more 'official' way of figuring out if there was an error, but my searches have come up dry -- so I humbly request guidance from all of you k8s experts out there. Thanks !

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()
}

推荐答案

我建议您看看事件,请参阅

I'd suggest you to have a look at events, see this topic for some guidance. Generally each object should generate events you can watch and be notified of such errors.

这篇关于从.json或.yaml规范实例化资源时,Kubernetes中接收错误通知的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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