Life cycle events kubernetes of the container to add the processors

Brief introduction

This article describes how to add a statement to the processor cycle time container, Kubernetes support postStartand preStopevents. Kubernetes sent immediately after the container start postStartevent, sent immediately before the container is terminated preStopevent.

NOTE: This document is to be understood with reference to their official documents, and. If misleading, please criticism.

And a processor preStop defined postStart

Create a container of Pod, container there postStartand preStopevents. file name:lifecycle-events.yaml

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
      preStop:
        exec:
          command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]

In the configuration file, postStartthe command writes a messagefile to the container /usr/share/messagedirectory. preStopCommand smooth stop nginx. If the container is terminated due to a failure, it would be helpful.

# kubectl apply -f /root/k8s-example/pods/lifecycle-events.yaml

Pod confirm the vessel is running

# kubectl get pod lifecycle-demo
NAME             READY   STATUS    RESTARTS   AGE
lifecycle-demo   1/1     Running   0          26h

Into the vessel shell

# kubectl exec -it lifecycle-demo -- /bin/bash

Confirm the shell postStartprocessor creates a messagefile exists

root@lifecycle-demo:/#  cat /usr/share/message
Hello from the postStart handler

After you create a container, Kubernetes sent immediately postStartevent. However, no guarantee that the calling Container entrypointbefore the first call postStarthandler. postStartHandlers run asynchronously with respect to the code Container, but Kubernetes management of container blocks until postStartthe handler is completed. In the postStartprior processing program is completed, the state is not set to the container RUNNING.

Guess you like

Origin www.cnblogs.com/mcsiberiawolf/p/12227852.html