Pod Hook

Pod Hook

kubernetes provide a container for the life cycle, called Pod Hook, Pod Hook initiated by kubelet can occur in the container before starting and stopping the run, is included in the life cycle of container. We can also configure Hook for all the Pod.

kubernetes provides two hooks for us:

  • PostStart: This hook executed immediately after the container is created. However, it can not determine the operating sequence must be before ENTRYPOINT container, since no parameters passed a handler (Pod in the vessel only specify the command parameter, the container starts with the entrypoint program, the command as a parameter to ENTRYPOINT start command; specify args parameter, the container is to use the comand start command and args parameter run.). Environmental mainly for container, ready to work resource of the lamp, but it should be ensured that the running time is short, otherwise the container will not be long reach Running state.
  • PreStop: Hook container executed before termination. It is blocking, meaning that it is synchronized, so you must do before you delete the call issued by the container. Mainly used for graceful shutdown procedure to notify other systems. If the hook long run, the container can not delete, remains in Running state never reaches the failed state.

If the hook function fails, it will kill container, so as much as possible so that the hook lightweight. Run the length of time determined according to the business needs of the application.

There are two ways to implement the hook function:

  • exec: for the implementation of a particular command, which consumes resources of the container are recorded
  • http: for a particular port on the container vessel HTTP requests.

template

​ ""

apiVersion: v1
kind: Pod
metadata:
  name: hook
  labels:
    app: hook
spec:
  containers:
  - name: hook
    image: busybox
    lifecycle:
      postStart:
        exec:
          command: ["/bin/bash","-c","echo PostStart Hook > /home/message"]

Elegant delete resource object

When a user requests to delete the resource object contains pod (such as Deployment etc.), K8S For the application to close elegance (ie after the application to complete the request is being processed, and then close the software), K8S offers two information notice:

  • Default: K8S notification node execute docker stopcommands, will first vessel Docker PIDtransmission signal for the process system 1 SIGTERM, and then wait for the application terminates execution container, if the waiting time reaches the set timeout period, or a default timeout (30s), We will continue to send SIGKILLsystem signals forcibly kill off process.
  • Use pod life cycle (using the PreStopcallback function), which is performed before sending termination signal.

By default, all of the elegant exit time within 30 seconds. kubectl delete command support --grace-period=<seconds>option, this option allows the user to override the default value specified by the value of their own. The value '0' to force the removal pod. In version 1.5 and above kubectl, the execution must be specified at the same time force the deletion --force --grace-period=0.

Forcibly remove a pod is also etcd years immediately delete the pod from the cluster state. When the Pod is forced deleted, api server does not wait for confirmation from kubelet on the node where the Pod: pod has been terminated. pod will be immediately deleted in the API, on the node, pods is set immediately after the termination, forced to kill before there will be a little grace period.

In addition Hook call log event is not exposed to the Pod, you can only be obtained by calling the event describe command, if you do wrong, you see FailedPostStartHook or FailedPreStopHook this event.

Guess you like

Origin www.cnblogs.com/h-gallop/p/11780335.html