gpt4 book ai didi

kubernetes - preStop 钩子(Hook)没有被执行

转载 作者:行者123 更新时间:2023-12-02 12:17:04 27 4
gpt4 key购买 nike

我正在测试生命周期钩子(Hook),启动后工作得很好,但我认为前停止永远不会被执行。还有一个answer ,但它不起作用,实际上如果它起作用,它将与 k8s 文档相矛盾。因此,从文档中:

PreStop

This hook is called immediately before a container is terminated dueto an API request or management event such as liveness probe failure,preemption, resource contention and others. A call to the preStop hookfails if the container is already in terminated or completed state.


所以,API 请求让我觉得我可以简单地做 kubectl delete pod POD ,我很好。
更多来自文档( pod shutdown process):

1.- User sends command to delete Pod, with default grace period (30s)

2.- The Pod in the API server is updated with the time beyond which the Pod is considered “dead” along with the grace period.

3.- Pod shows up as “Terminating” when listed in client commands

4.- (simultaneous with 3) When the Kubelet sees that a Pod has been marked as terminating because the time in 2 has been set, it begins the pod shutdown process.

4.1.- If one of the Pod’s containers has defined a preStop hook, it is invoked inside of the container. If the preStop hook is still running after the grace period expires, step 2 is then invoked with a small (2 second) extended grace period.

4.2.- The container is sent the TERM signal. Note that not all containers in the Pod will receive the TERM signal at the same time and may each require a preStop hook if the order in which they shut down matters.

...


所以,自从你做 kubectl delete pod POD , pods 开始 Terminating ,我想我可以做到。
从另一个答案来看,我不能这样做,但方法是进行滚动更新。好吧,我尝试了所有可能的方法,但也没有用。
我的测试:
我有一个部署:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-deploy
spec:
replicas: 1
template:
metadata:
name: lifecycle-demo
labels:
lifecycle: demo
spec:
containers:
- name: nginx
image: nginx
lifecycle:
postStart:
exec:
command:
- /bin/sh
- -c
- echo "Hello at" `date` > /usr/share/post-start
preStop:
exec:
command:
- /bin/sh"
- -c
- echo "Goodbye at" `date` > /usr/share/pre-stop
volumeMounts:
- name: hooks
mountPath: /usr/share/
volumes:
- name: hooks
hostPath:
path: /usr/hooks/
我期待 pre-stoppost-start要在 /usr/hooks/ 中创建的文件, 在主机(运行 pod 的节点)上。启动后存在,但停止前,永远不会。
  • 我试过kubectl delete pod POD ,它没有工作。
  • 我试过kubectl replace -f deploy.yaml ,使用不同的图像,当我这样做时 kubectl get rs ,我可以看到创建的新副本集,但文件不存在。
  • 我试过kubectl set image ... ,我可以再次看到新的 replicaSet已创建,但文件不存在。
  • 我什至尝试将它们放在完全分开的卷中,因为我想可能是当我杀死 pod 并重新创建它时,它会重新创建应该创建文件的文件夹,因此它会删除文件夹和 pre-stop文件,但事实并非如此。
    注意:它总是在同一个节点上重新创建。我确定了这一点。

  • 我没有尝试过通过设置低 CPU 限制来炸毁容器并破坏它,但这不是我需要的。
    知道在什么情况下 preStop钩子(Hook)会被触发吗?

    最佳答案

    将此作为社区 wiki 发布,以获得更好的可见性。

    There is a typo in the second "/bin/sh"; for preStop. There is an extra double quote ("). It was letting me to create the deployment, but was the cause it was not creating the file. All works fine now.


    问题所在的确切点在这里:
              preStop:
    exec:
    command:
    - /bin/sh" # <- this quotation
    - -c
    - echo "Goodbye at" `date` > /usr/share/pre-stop
    正确的应该是这样的:
              preStop:
    exec:
    command:
    - /bin/sh
    - -c
    - echo "Goodbye at" `date` > /usr/share/pre-stop

    在撰写此社区 Wiki 帖子时,此 Deployment list 已过时。需要进行以下更改才能运行此 list :
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: good-deployment
    spec:
    selector:
    matchLabels:
    lifecycle: demo
    replicas: 1
    template:
    metadata:
    labels:
    lifecycle: demo
    spec:
    containers:
    - name: nginx
    image: nginx
    lifecycle:
    postStart:
    exec:
    command:
    - /bin/sh
    - -c
    - echo "Hello at" `date` > /usr/share/post-start
    preStop:
    exec:
    command:
    - /bin/sh
    - -c
    - echo "Goodbye at" `date` > /usr/share/pre-stop
    volumeMounts:
    - name: hooks
    mountPath: /usr/share/
    volumes:
    - name: hooks
    hostPath:
    path: /usr/hooks/
    变化如下:
    1. apiVersion
    +--------------------------------+---------------------+
    | Old | New |
    +--------------------------------+---------------------+
    | apiVersion: extensions/v1beta1 | apiVersion: apps/v1 |
    +--------------------------------+---------------------+
    StackOverflow 答案以获取更多引用:
  • Stackoverflow.com: Questions: No matches for kind “Deployment” in version extensions/v1beta1

  • 2. selector已添加 selector spec 下的部分:
    spec:
    selector:
    matchLabels:
    lifecycle: demo
    附加引用链接:
  • What is spec - selector - matchLabels used for while creating a deployment?
  • Kubernetes.io: Docs: Concepts: Workloads: Controllers: Deployment: Selector
  • 关于kubernetes - preStop 钩子(Hook)没有被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55280259/

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