正确使用kubernetes手表的方法
我是Kubernetes的新手,我真的不确定如何正确执行手表;尤其是我不确定如何处理resourceVersion参数 目标是监视具有特定标签的新POD,如果出现错误或与集群断开连接,则能够从上次发生的事件重新启动监视 我正在做这样的事情:正确使用kubernetes手表的方法,kubernetes,watch,Kubernetes,Watch,我是Kubernetes的新手,我真的不确定如何正确执行手表;尤其是我不确定如何处理resourceVersion参数 目标是监视具有特定标签的新POD,如果出现错误或与集群断开连接,则能够从上次发生的事件重新启动监视 我正在做这样的事情: // after setting up the connection and some parameters String lastResourceVersion = null; // at beginning version is unknown whil
// after setting up the connection and some parameters
String lastResourceVersion = null; // at beginning version is unknown
while (true) {
try {
Watch<V1Pod> watcher = Watch.createWatch(
client,
api.listNamespacedPodCall(namespace, pretty, fieldSelector, labelSelector, lastResourceVersion, forEver, true, null, null),
new TypeToken<Watch.Response<V1Pod>>() {}.getType()
);
for (Watch.Response<V1Pod> item : watcher) {
//increment the version
lastResourceVersion = item.object.getMetadata().getResourceVersion();
// do some stuff with the pod
}
} catch (ApiException apiException) {
log.error("restarting the watch from "+lastResourceVersion, apiException);
}
}
//设置连接和一些参数后
字符串lastResourceVersion=null;//开始时版本未知
while(true){
试一试{
Watch watcher=Watch.createWatch(
客户
listNamespacedPodCall(名称空间、pretty、fieldSelector、labelSelector、lastResourceVersion、forEver、true、null、null),
新的TypeToken(){}.getType()
);
for(Watch.Response项:watcher){
//增加版本
lastResourceVersion=item.object.getMetadata().getResourceVersion();
//用豆荚做点什么
}
}捕获(ApiException ApiException){
log.error(“从“+lastResourceVersion,apiException”重新启动监视);
}
}
使用Pod的resourceVersion重新初始化监视呼叫是否正确?这个数字是集群中所有事件的时间戳,还是不同的api将使用不同的序列
我需要注意特定的例外情况吗?如果是资源的话,版本会变老吗
谢谢,亚当是对的
这最好用
引用相关部分(重点):
检索资源集合(命名空间或群集范围)时,来自服务器的响应将包含一个resourceVersion值,可用于启动对服务器的监视
。。。剪断
当请求的监视操作因该资源的历史版本不可用而失败时,客户端必须通过识别状态代码410已消失、清除其本地缓存、执行列表操作以及从该新列表操作返回的资源版本启动监视来处理该情况。
因此,在调用watch之前,您应该列出并从列表中提取resourceVersion(而不是其中的对象)。然后用这个版本启动手表。如果手表因某种原因失败,您必须再次列出,然后使用该列表中的resourceVersion重新建立手表。我不熟悉手表功能的cpp或java实现,但一般来说,您需要获取/列出资源并使用其resourceVersion来设置手表。资源版本本身是一个与etcd中的资源版本相对应的数字,用于确定资源是否有任何更改。关于第二点,是的,如果资源版本太旧,您应该捕获异常。您可以在这里阅读更多内容:下面是一个在go中实现列表、监视和处理所有可能异常的示例(如果您了解cpp,您应该很容易理解它)