gpt4 book ai didi

kubernetes - 为什么 Kubernetes pod 不优雅停止?

转载 作者:行者123 更新时间:2023-12-03 08:58:22 29 4
gpt4 key购买 nike

我遇到了即使删除 pod 也不会立即停止的问题。

应该修复什么才能正常终止?

list 文件。

apiVersion: apps/v1
kind: Deployment
metadata:
name: cmd-example
spec:
replicas: 1
selector:
matchLabels:
app: cmd-example
template:
metadata:
labels:
app: cmd-example
spec:
terminationGracePeriodSeconds: 30
containers:
- name: cmd-container
image: alpine:3.8
resources:
requests:
cpu: 100m
memory: 100Mi
command: ["/bin/sh"]
args: ["-c", "while true; do exec sleep 100;done"]

复制过程

  1. 创建部署。
    $ kubectl apply -f deployments.yaml
  2. 删除部署。
    kubectl delete -f 020-deployments.yaml

kubectl get po -w输出是。

cmd-example-5cccf79598-zpvmz   1/1       Running   0         2s
cmd-example-5cccf79598-zpvmz 1/1 Terminating 0 6s
cmd-example-5cccf79598-zpvmz 0/1 Terminating 0 37s
cmd-example-5cccf79598-zpvmz 0/1 Terminating 0 38s
cmd-example-5cccf79598-zpvmz 0/1 Terminating 0 38s

这应该会更快完成。
大约花了 30 秒才完成。也许是由于终止时的SIGKILLGracePeriodSeconds 30s所致。
为什么不使用 SIGTERM 立即清理 pod?

应该修复什么?

环境

我在以下环境中确认了它。

  • Mac 版 Docker:18.06.1-ce、Kubernetes:v1.10.3
  • 适用于 Windows 的 Docker:18.06.1-ce、Kubernetes:v1.10.3
  • Google Kubernetes 引擎:1.11.2-gke.15

最佳答案

问题的原因

这个shell即使接受SIGTERM信号也不会停止。

解决方案

使用 trap 命令。

换地方

    command: ["/bin/sh"]
args: ["-c", "trap 'exit 0' 15;while true; do exec sleep 100 & wait $!; done"]

结果

删除后,pod很快就被清理了!

img-example-d68954677-mwsqp   1/1       Running   0         2s
img-example-d68954677-mwsqp 1/1 Terminating 0 8s
img-example-d68954677-mwsqp 0/1 Terminating 0 10s
img-example-d68954677-mwsqp 0/1 Terminating 0 11s
img-example-d68954677-mwsqp 0/1 Terminating 0 11s

关于kubernetes - 为什么 Kubernetes pod 不优雅停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53199243/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com