Configure lifecycle hooks and probes

更新时间:
复制 MD 格式

When you deploy containerized applications, you need to run custom logic during container startup and shutdown, and continuously verify that containers are healthy and ready to serve traffic. Enterprise Distributed Application Service (EDAS) integrates Kubernetes lifecycle hooks and container probes so you can manage these behaviors directly from the EDAS console.

How lifecycle hooks work

Kubernetes provides two lifecycle hooks for containers:

HookWhen it runsTypical use case
PostStartImmediately after a container is created, concurrently with the container ENTRYPOINTLoad resources, register the service with a discovery system, or send a startup notification
PreStopBefore a container is terminated; the hook handler must execute before the request to delete the container is sent to the Docker daemonDeregister the service, drain connections, or save state before shutdown

Hook behaviors:

  • PostStart runs concurrently with ENTRYPOINT. The container process may not be fully initialized when the PostStart hook runs. Do not rely on the application being ready inside PostStart logic. You do not need to pass parameters to the hook handler.

  • PreStop blocks termination. The hook handler must be executed before the request that is initiated to delete the container is sent to the Docker daemon. The Docker daemon sends a SIGTERM signal to delete the container, regardless of the execution result of the hook handler. If the hook exceeds the TerminationGracePeriodSeconds timeout, Kubernetes force-kills the container.

  • At-least-once delivery. A hook may be called multiple times for a single event. Design hook handlers to be idempotent.

  • If a hook handler fails, the container is restarted based on its restart policy. Check the application change records in EDAS for failure details.

For more information, see Container Lifecycle Hooks in the Kubernetes documentation.

How container probes work

EDAS supports two Kubernetes container probes:

ProbePurposeOn failure
LivenessChecks whether the container is still running and healthyKubernetes terminates and restarts the container based on the restart policy
ReadinessChecks whether the container is ready to receive trafficKubernetes removes the pod from service endpoints until the probe succeeds
Note

If no liveness probe is configured, Kubernetes assumes the container is always healthy. If no readiness probe is configured, Kubernetes considers the container ready as soon as it starts.

For more information, see Pod Lifecycle in the Kubernetes documentation.

Probe modes

Both liveness and readiness probes support the following check modes:

ModeHow it worksSuccess condition
ExecRuns a command inside the containerExit code is 0
HttpGetSends an HTTP GET request to a container endpointResponse status code is 200--399
TcpSocketOpens a TCP connection to a specified port on the containerThe port is reachable

HttpGet parameters

When you use HttpGet mode, configure the following parameters:

ParameterDescriptionDefault
HostIP address or hostname for the requestPod IP address
PathURL path for the request/
PortPort number for the request(Required)
SchemeProtocol: HTTP or HTTPSHTTP
HttpHeadersCustom HTTP request headersNone

Probe timing parameters

These parameters control when and how often probes run:

ParameterDescriptionValid valuesDefault
InitialDelaySecondsSeconds to wait after the container starts before the first probe runs1--10000
PeriodSecondsSeconds between consecutive probe checks1--100010
TimeoutSecondsSeconds before a single probe check times out1--10001
SuccessThresholdConsecutive successes required after a failure before the probe is considered successful. For liveness probes, this value must be 1.1--10001
FailureThresholdConsecutive failures required after a success before the probe is considered failed1--10003

Prerequisites

Before you begin, ensure that you have:

  • An EDAS account with access to the EDAS console

  • A Kubernetes cluster configured in EDAS

  • An application deployed or ready to deploy in a Kubernetes cluster

Configure hooks and probes during application creation

  1. Log on to the EDAS console.

  2. In the left-side navigation pane, choose Application Management > Applications.

  3. In the top navigation bar, select a region. In the upper part of the page, select a namespace.

  4. In the upper-left corner, click Create Application.

  5. In the Basic Information step, set the following parameters and click Next. The following table describes the Java runtime sub-options:

    ParameterDescription
    Cluster TypeSelect Kubernetes Clusters
    Application Runtime EnvironmentSelect the runtime environment for your application. Options: Java (Custom, Java, Tomcat, or EDAS-Container (HSF)), PHP, or Node.js, C++, Go, and Other Languages
    OptionWhen to useConfigurable parameters
    CustomDeploy with a custom container imageNone
    JavaDeploy a Dubbo or Spring Boot application with a JAR packageJava Environment
    TomcatDeploy a Dubbo or Spring application with a WAR packageJava Environment, Container Version
    EDAS-Container (HSF)Deploy an HSF application with a WAR or FatJar packageJava Environment, Pandora Version, Ali-Tomcat Version
  6. In the Configurations step, configure the environment information, basic information, deployment method, and resource parameters. Click Next.

  7. In the Advanced Settings step, expand the Application Life Cycle Management section. Configure the lifecycle hooks and probes as described in Set up lifecycle hooks and Set up liveness and readiness probes.

  8. Click Create Application.

  9. On the Creation Completed page, review the Basic Information, Configurations, and Advanced Settings sections. Click Create Application to confirm.

After the deployment finishes, go to the Application Overview page and click View Details to open the Change List page. Monitor the deployment progress there. When the pod status shows as running in the Basic Information section, the application is deployed.

Configure hooks and probes for an existing application

  1. Log on to the EDAS console.

  2. In the left-side navigation pane, choose Application Management > Applications.

  3. In the top navigation bar, select a region. In the upper part of the page, select a namespace.

  4. From the Cluster Type drop-down list, select Container Service or Serverless Kubernetes Cluster. Find the target application and click its name.

  5. On the Overview or Basic information page, choose Deploy > Deploy in the upper-right corner.

  6. On the Select Deployment Mode page, select a deployment mode and click Start Deployment.

  7. Configure the deployment parameters, then expand the Application Life Cycle Management section. Configure the lifecycle hooks and probes as described in Set up lifecycle hooks and Set up liveness and readiness probes.

  8. Click OK.

Important

Clicking OK restarts the application. Perform this operation during off-peak hours.

Set up lifecycle hooks

PostStart and PreStop hooks support two configuration modes: Exec and HttpGet.

Exec mode

Exec mode runs a command inside the container to perform tasks such as running scripts or writing startup flags.

Example: Run echo "container started" after a container is created.

Enter the command using one of these methods:

  • Split parameters: Enter each part of the command as a separate argument. Best for short commands with few arguments.

    PostStart Exec mode - split parameters

  • Shell wrapper: Start a shell process and pass the full command as a string. Best for long commands or commands with multiple arguments.

    PostStart Exec mode - shell wrapper

HttpGet mode

HttpGet mode sends an HTTP GET request to a specified endpoint when the lifecycle event fires.

Example: Send a GET request to http://example.com:8080/healthz after the container starts.

PostStart HttpGet mode

TerminationGracePeriod

Set the TerminationGracePeriod configuration to control how long Kubernetes waits for a pod to shut down gracefully. The PreStop hook must complete and the application must process the SIGTERM signal within this timeout. If the timeout expires, Kubernetes force-kills the container.

Set up liveness and readiness probes

Example: Liveness probe with TcpSocket

The following screenshot shows a liveness probe configured in TcpSocket mode:

Liveness probe - TcpSocket mode

This configuration behaves as follows:

ParameterValueEffect
Host(empty)Uses the pod IP address
Port18081Probes TCP port 18081
InitialDelaySeconds60Waits 60 seconds after container startup before the first check
PeriodSeconds10Runs a check every 10 seconds
SuccessThreshold1One successful check marks the pod as alive
TimeoutSeconds5Each check times out after 5 seconds
FailureThreshold3Three consecutive failures trigger a container restart

Verify the configuration

After deploying an application with lifecycle hooks, verify that the hooks ran correctly.

Example: If you configured a PostStart hook to run echo "Hello from the postStart handler" > /usr/share/message, verify the result:

  1. Open the web shell for the application instance in the EDAS console.

  2. Run the following command:

       cat /usr/share/message
  3. Confirm that the output contains Hello from the postStart handler.

    Lifecycle hook verification

Support

If you have questions or suggestions about using Container Service for Kubernetes (ACK) and Serverless Kubernetes clusters in EDAS, submit a . You can also join the DingTalk group 23197114 for community support.

References