将TTL添加到kubernetes api服务器 [英] Adding TTLs to kubernetes api server

查看:106
本文介绍了将TTL添加到kubernetes api服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

紧跟带有TTL的Kubernetes端点:

将来是否有计划将TTL添加到Kubernetes API资源?

Is there a plan to add TTLs to Kubernetes API resources in the future?

如果我想尝试自己向API添加可选的TTL,可以有人帮助我入门,方法是将我指向代码或文档中的正确位置,在此我应该开始寻找所需的内容改变吗?

If I wanted to try to experiment with adding an optional TTL to the API myself, can somebody help me get started by pointing me to the right place in the code or docs where I should start looking to figure out what I need to change?

我假设基础存储的接口具有TTL,而我需要更改的只是如何向API服务器传递TTL,然后将其转发到存储.听起来对吗?

I'm assuming that the interface to the underlying storage has TTLs and all I need to change is how the API server could be passed a TTL that I would then forward to the storage. Does that sound right?

推荐答案

您无需修改​​kubernetes即可.

You do not need to modify kubernetes to do this.

这是您自己的方法.

  1. 向要具有TTL的每个对象添加注释.注释可以说出它应该何时过期.您可以选择此批注的名称和格式.
  2. 每次更新对象时更新注释.
  3. 运行另一个过程,该过程重复列出给定类型的所有对象,并删除需要过期的对象.

以下是针对端点执行此操作的特定命令.

Here are specific commands to do this for endpoints.

将注释添加到从现在起一分钟后到期的端点:

Add an annotation to an endpoint with expiration time one minute from now:

   #!/bin/bash
   expiretime=$(date -v+60S +%s)
   kubectl annotate endpoints/somename expires-at=$expiretime

脚本以列出端点,并删除具有到期时间的端点:

Script to list endpoints, and delete those with expires-at after now:

   #!/bin/bash
   while 1
   do 
     for NS in $(kubectl get namespaces -o name | cut -f 2 -d "/")
     do 
         for NAME in $(kubectl --namespace=$NS get endpoints -o name)
         do
             exp=$( kubectl get --namespace $NS $NAME -o jsonpath={.metadata.annotations."expires-at"} 2> /dev/null) && \
             [[ $exp < $(date +%s) ]] && \
             echo "Deleting expired endpoints $NAME in $NS" && \ 
             kubectl delete $NS $NAME
         done
     done
   done

吊舱是运行上述脚本的好地方.它将具有对API的自动访问权限,并带有复制控制器,它将永远运行.

A pod is a great place to run the above script. It will have automatic access to the API and with a replication controller, it will run forever.

这篇关于将TTL添加到kubernetes api服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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