简体   繁体   English

在Kubernetes中是否有针对关机或在杀死Pod之前的探针?

[英]Is there a probe for shutdown or before kill pods in Kubernetes?

Is there a way to get a trigger to shutdown, so we can close all connections gracefully before shutdown and don't proceed any actions after that probe and keeping the probe ready to kill. 是否有办法触发关闭,所以我们可以在关闭前正常关闭所有连接,并且在该探针之后不要执行任何操作并保持探针准备终止。

This including flushing logs, keeping any state of the application saved before handing over to the new pod and many more use cases. 这包括刷新日志,在移交给新的Pod之前保留应用程序的任何状态以及更多用例。

You have 2 options: 您有2个选择:

  1. Containers (PID 1) receive SIGTERM before the container (and the pod) is removed. 容器(PID 1)在删除容器(和容器)之前会收到SIGTERM。 You can trap SIGTERM and act on it. 您可以捕获SIGTERM并对其采取行动。

  2. You can use the preStop lifecycle hook 您可以使用preStop生命周期挂钩

Important implementation details can be found here: https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods 重要的实现细节可以在这里找到: https : //kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods

httpGet example httpGet示例

apiVersion: v1
kind: Pod
metadata:
  name: prestop-pod
spec:
  terminationGracePeriodSeconds: 5
  containers:
    - name: nginx
      image: nginx
      lifecycle:
        preStop:
          httpGet:
            # only port is reqired
            port: 80
            path: "?preStop"
            # scheme: HTTP
            # host: ...
            # httpHeaders:
            #   name: ...
            #   value: ...

After kubectl apply -f on this file, run kubectl log -f prestop-pod while executing kubectl delete pod prestop-pod on another terminal. kubectl apply -f此文件后,在另一个终端上执行kubectl delete pod prestop-pod时,运行kubectl log -f prestop-pod You should see something like: 您应该看到类似以下内容:

$ kubectl apply -f prestop.yaml 
pod/prestop-pod created
$ kubectl logs -f prestop-pod
10.244.0.1 - - [21/Mar/2019:09:15:20 +0000] "GET /?preStop HTTP/1.1" 200 612 "-" "Go-http-client/1.1" "-"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM