In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Original: https://i4t.com/4424.html
First of all, let's briefly analyze "stop Pod gracefully".
The term "elegant Graceful shutdown" comes from the operating system. For example, our windows shutdown system will first exit the software and then step by step to the shutdown, while the opposite is hard termination (Hard shutdown). The simple understanding is to directly unplug the power supply.
When it comes to microservices, the gateway distributes traffic to each Pod node, such as when we go online to update Pod
If we kill Pod directly, this part of the traffic will not be handled correctly, which will affect some users. Usually, the gateway or registration center will keep our service with a heartbeat, and after the heartbeat timeout, our service will be removed automatically, but one problem is that the timeout may be 30 seconds or 60 seconds, although it will not affect our system, but it will cause users to jitter slightly. If we execute a command before stopping to notify the gateway or registry that the host is offline, then the registry will mark that the host has gone offline. Without traffic forwarding, the user will not have any impact. This is elegant stop, minimizing the impact of rolling updates on Pod Hook.
Pod Hook is initiated by kubelet and runs before the process in the container starts or terminates, which is included in the lifecycle of the container. We can configure hook for all containers in Pod at the same time
In K8s, the ideal state is the graceful release of pod and the generation of a new Pod. But not every Pod will be so smooth.
Pod is stuck, can not handle elegant exit command or operation elegant exit logic has BUG, trapped in an endless loop code problem, resulting in the execution of commands have no effect
For the above problems, there is also a "maximum tolerable time" in the Pod termination process of K8s, that is, grace period (defined in the .spec.terminationGracePerforming seconds field of pod). This value defaults to 30 seconds. When we execute kubectl delete, we can also override the configuration in Pod by specifying an elegant exit time through-grace-period parameter display. If the configured grace period exceeds the time, K8s can only choose to force kill Pod.
Kubernetes provides us with two hook functions:
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, because there are no parameters passed to the handler. Mainly used for resource deployment, environmental preparation and so on. It is important to note, however, that if the hook takes too long and does not work or hangs, the container will not be able to reach the Running state. PreStop: the 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 starts. Mainly used to gracefully close applications, notify other systems, and so on. If the hook hangs during execution, the Pod phase will remain in the Running state and will not reach the failed state
If the PostStart or PreStop hook fails, it kills the container. So we should make the hook function as light as possible. Of course, in some cases, it makes sense to run the command for a long time, such as preserving the state before stopping the container.
Here's a little bit about the process of terminating Pod.
The user sends the command to delete Pod,Pod to enter Terminating state service to remove the Pod node when kubelet sees that Pod has been marked to terminate, starts to execute preStop hook, if the running time of preStop hook exceeds grace period,kubelet, it will send SIGTERM and wait 2 seconds.
Introduction of official documents
There are two ways of Exec and HTTP in Pod Hook hook function.
Exec-used to execute a specific command, but note that the trumpet resources of the command are counted in the container HTTP-execute HTTP requests for specific endpoints on the container based on the PostStart command demonstration
First of all, let's demonstrate two ways of PostStart
The first Exec
We append a paragraph of echo to / tmp/message to operate before Pod starts
Cat > > exec_test.yaml
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.