Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use k8s container hooks to ensure a safe exit of the service

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use the K8s container hook to ensure the safe exit of the service". The content is easy to understand and clear, hoping to help you solve your doubts. Let the editor lead you to study and learn the article "how to use the K8s container hook to ensure the safe exit of the service".

Kubernetes provides lifecycle hooks for containers.

Hooks make the container aware of events in its lifecycle and run the specified code when the corresponding lifecycle hook is called.

@ [TOC]

Hooks for the life cycle of containers

Kubernetes provides lifecycle hooks for containers.

Hooks make the container aware of events in its lifecycle and run the specified code when the corresponding lifecycle hook is called.

Container hooks are divided into two types of trigger points: PostStart after container creation and PreStop before container termination.

PostStart

This hook is executed immediately after the container is created.

However, there is no guarantee that the hook will run before the container ENTRYPOINT.

No parameters are passed to the handler.

Container ENTRYPOINT and hook execution are asynchronous operations.

If the hook takes too long for the container to run or hang, the container will not be able to reach the running state

PreStop

This hook is called immediately before the container terminates.

It is blocked, which means it is synchronous, so it must be done before the call to delete the container is made

If the hook hangs during execution, the Pod phase stays in the running state and never reaches the failed state.

If the PostStart or PreStop hook fails, the container will be kill.

Users should make their hook handlers as light as possible.

Implementation of hook handler

The container can access the hook by implementing and registering the handler for the hook.

You can implement two types of hook handlers for containers:

Exec-executes a specific command, such as pre-stop.sh, within the container's cgroups and namespace.

The resources consumed by this command are counted in the container.

HTTP-performs an HTTP request on a specific endpoint on the container.

There is no log of hook handlers in the events of Pod. If a handler fails for some reason, it broadcasts an event.

For PostStart, this is the FailedPostStartHook event, and for PreStop, this is the FailedPreStopHook event.

You can view these events by running kubectl describe pod.

Define pre-start and pre-end event actions

Next we will create a Pod that contains a container, and we will set up pre-start and pre-close operations for this container.

Https://raw.githubusercontent.com/kubernetes/website/master/docs/tasks/configure-pod-container/lifecycle-events.yaml

ApiVersion: v1kind: Podmetadata: name: lifecycle-demospec: 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: ["/ usr/sbin/nginx", "- s", "quit"] use prestop hook to ensure the safe exit of the service

Using the spring framework in the actual production environment, because in the process of service update, the service container is terminated directly, and some requests are still distributed to the terminated container, resulting in 500 errors, which account for less request data and can be ignored.

Consider adding elegant terminations to minimize error requests until there are no errors.

Here is the service discovery component of spring cloud:

Eureka is a REST-based service that acts as a service registry and is used to locate services for load balancing and failover of middle-tier servers.

When each service starts, it registers its own information (IP, port, service information, etc.) with Eureka Server, and Eureka Server stores this information.

After the microservice starts, it sends a heartbeat to Eureka Server periodically (default is 30 seconds) to renew its "lease", and you can obtain the address information of other microservices from eureka and execute the relevant logic.

Consider that eureka server modifies the status of the registered instance, suspends the service (InstanceStatus.OUT_OF_SERVICE), and retains the service for a period of time before deleting the service.

Disable a service:

Curl-X PUT "http://admin:admin@192.168.101.100:8761/eureka/apps/{appName}/{instanceId}/status?value=OUT_OF_SERVICE"

Description: admin:admin is the login name and password of eureka. If not, delete the previous paragraph directly.

InstanceId is the tag content in the list of services shown in the link opened above, such as: myapp:192.168.1.100:8080

Specific operations in k8s:

ApiVersion: extensions/v1beta1kind: Deploymentmetadata: name: NAME-service-deploymentspec: replicas: 3 selector: matchLabels: app: NAME-service template: metadata: labels: app: NAME-service spec: containers:-name: NAME-service lifecycle: preStop: exec: command:-"/ bin/sh" -"- c"-">

Delete useless information and focus on lifecycle

First of all, the environment variables of the service name and port are defined, and this part is taken as a separate variable to facilitate the modification of different services.

Use curl PUT to eureka to configure the status as OUT_OF_SERVICE.

Configure a sleep time as the service stop buffer time.

The above is all the contents of the article "how to use the K8s container hook to ensure the safe exit of the service". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report