首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >activeDeadlineSeconds即将失效

activeDeadlineSeconds即将失效
EN

Stack Overflow用户
提问于 2020-05-05 23:20:39
回答 2查看 430关注 0票数 0

我们在部署时尝试设置activeDeadlineSeconds时遇到问题。虽然我们在kubectl上看到了相应的解释,但这是一个有效的部署参数。请参考下图:

现在,当我们尝试为部署设置相同的参数时,它会显示这是无效的。请参考下图:

如果我们在这里做错了什么,请告诉我们。您可以使用以下yaml进行快速实验:

代码语言:javascript
复制
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: test
  name: test
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: test
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

我们尝试这样做是因为在我们的例子中有一个初始化容器,它有时会挂起,因为activeDeadlineSeconds也包含初始化容器;progressdeadlineseconds不包含初始化容器

有没有别的选择呢?

EN

回答 2

Stack Overflow用户

发布于 2020-05-06 17:35:17

在部署上使用activeDeadlineSeconds的想法意味着您希望运行一些不会持续太长时间的东西。这违背了部署的目的。部署意味着你想要持久的东西。

正如Amit Kumar Gupta解释的那样:

在语法上,deployment.spec.template.specpod.spec相同(这就是为什么您在kubectl explain输出中看到activeDeadlineSeconds ),但是从语义上讲,pod规范中的所有字段在部署上下文中并不都是有意义的/允许的/支持的(这就是为什么您会看到禁止的错误消息-在幕后,创建部署会导致创建ReplicaSets)。

如果我们检查documentation,我们可以看到activeDeadlineSeconds仅可用于Job和Pod。

Job和Pod的目的是运行一个任务,并在任务完成后死亡。

activeDeadlineSeconds

指定作业在系统尝试终止之前可能处于活动状态的相对于startTime的持续时间(以秒为单位);值必须为正整数

如果您正在考虑设置activeDeadlineSeconds,这意味着您不希望部署Pod持续存在,这是一个错误的方法。

有没有替代方案呢?

这实际上取决于您的应用程序为什么需要这种方法。如果您真的认为这种方法有意义,您可以打开一个issue并为此发出一个特性请求。

票数 0
EN

Stack Overflow用户

发布于 2020-05-22 23:32:15

有没有替代方案呢?

这是一个非常简单的linux任务,可以使用timeouttimelimit二进制文件来解决。

代码语言:javascript
复制
$ timeout -k 1 5 bash -c "while true ; do sleep 1 && echo 1 ; done" || echo
# the above task will be terminated in 5 secs or killed in 6 secs.

# timeout [OPTION] DURATION COMMAND [ARG]...
# -k, --kill-after=DURATION also send a KILL signal if COMMAND is still running
    this long after the initial signal was sent (not available on some containers)
# DURATION is a floating point number with an optional suffix: 's' for
#   seconds (the default), 'm' for minutes, 'h' for hours or 'd' for
#   days.  A duration of 0 disables the associated timeout.
# the above task will be terminated in 5 secs or killed in 6 secs.


$ timelimit -t 5 -T 1 bash -c "while true ; do sleep 1 && echo 1 ; done" || exit 0
# the above task will be terminated in 5 secs or killed in 6 secs.

# -t 5 - five seconds before sending SIGTERM to the process  
# -T 1 - one second after SIGTERM was sent and before sending SIGKILL

只需在InitContainer命令中指定它,以便在一段合理的时间后停止任务。

要使InitContainer认为即使命令已终止也已成功完成,请将|| echo|| exit 0添加到命令的末尾,如上例所示。

默认情况下,系统中存在timeout (在busybox、ubuntu、centos和alpine上选中),但timelimit不存在,可以由适当的包管理器安装。例如:

代码语言:javascript
复制
$ apt-cache search timelimit
timelimit  - simple utility to limit a process's absolute execution time

$ apt-get -y install timelimit

您可以找到仅使用bash命令终止进程的其他方法:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61616663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档