当前位置:网站首页>Kubernetes best practice: graceful termination
Kubernetes best practice: graceful termination
2022-06-24 12:16:00 【imroc】
This article excerpts from kubernetes Learning notes
summary
Pod At the time of destruction , Will stop the process in the container , Usually we need to execute some aftercare logic during the process of stopping , For example, wait for the stock request to be processed to avoid connection interruption , Or notify relevant dependencies to clean up , So as to achieve the goal of elegant termination . This article is introduced in Kubernetes scenario , Best practices for implementing container graceful termination .
Container termination process
Let's first understand the container in Kubernetes Termination process in the environment :
- Pod Be deleted , The state is set to
Terminating
. - kube-proxy Update forwarding rules , take Pod from service Of endpoint Remove from the list , New traffic is no longer forwarded to the Pod.
- If Pod Configured with preStop Hook , Will execute .
- kubelet Yes Pod In each container send out
SIGTERM
Signal to inform the container process to start and stop . - Wait for the container process to stop completely , If in
terminationGracePeriodSeconds
Inside ( Default 30s) It hasn't completely stopped , Is sentSIGKILL
The signal forces the process to kill . - All container processes terminate , clear Pod resources .
Business code processing SIGTERM The signal
To achieve graceful termination , Be sure to handle it in the business code SIGTERM
The signal , Reference resources Handle SIGTERM Code example .
Do not let shell So that you can't get it SIGTERM The signal
If the container boot entry uses a script ( Such as CMD ["/start.sh"]
), The business process becomes shell Can be inherited by child processes. , stay Pod When stopped, the business process may not receive SIGTERM
The signal , because shell No automatic signaling to child processes . Please refer to Why can't I get my container SIGTERM The signal ?
If we solve ? Please refer to Practical skills : stay SHELL Transmit signals in .
The rational use of preStop Hook
If your business code does not handle SIGTERM
The signal , Or you can't control the use of third-party libraries or systems to increase the logic of graceful termination , You can also try for Pod Under configuration preStop, The logic of graceful termination is realized here , Example :
lifecycle: preStop: exec: command: - /clean.sh
Reference resources Kubernetes API file
In some extreme cases ,Pod Deleted for a short period of time , New connections may still be forwarded , because kubelet And kube-proxy meanwhile watch To pod Be deleted ,kubelet Are likely to kube-proxy The container has been stopped before the rules are synchronized , This may cause some new connections to be forwarded to the one being deleted Pod, And usually , When the application is SIGTERM
No new connections will be accepted , Only keep the stock connection to continue processing , So it may lead to Pod The request to delete the instantaneous part of failed .
In this case , We can also use it preStop First sleep A little , wait for kube-proxy Complete the rule synchronization and then start to stop the processes in the container :
lifecycle: preStop: exec: command: - sleep - 5s
Adjust the elegant duration
If the graceful termination time is long (preStop + The business process may stop for more than 30s), It can be customized according to the actual situation terminationGracePeriodSeconds
, Avoid being caught early SIGKILL
Kill , Example :
边栏推荐
- How to develop mRNA vaccine? 27+ pancreatic cancer antigen and immune subtype analysis to tell you the answer!
- LS-DYNA新手入门经验
- Ten thousand campus developers play AI in a fancy way. It's enough to see this picture!
- Jenkins performance test
- I'm in Shenzhen. Where can I open an account? Is it safe to open an account online now?
- [cloud based co creation] interpretation of harmonyos application and service ecology
- The latest hot spot: the use of copper death related genes for tumor prognosis typing!
- 11+! Methylation modification patterns based on m6A regulatory factors in colon cancer are characterized by different tumor microenvironment immune spectra
- Analysis and understanding of Jieba stutter word segmentation principle HMM application in Chinese word segmentation and partial code reading
- 巧妙构思-铁死亡调节因子分型预后发6+
猜你喜欢
如何开发短信通知和语音功能医院信息系统(HIS系统)
《opencv学习笔记》-- 分离颜色通道、多通道混合
"Meng Hua Lu" is about to have a grand finale. It's better to learn it first than to look ahead!
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map
How to develop hospital information system (his) with SMS notification and voice function
GTEST from getting started to getting started
Opencv learning notes - Discrete Fourier transform
AXI低功耗接口
程序员大部分时间不是写代码,而是。。。
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
随机推荐
《梦华录》要大结局了,看超前点映不如先来学学它!
Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 2)
万名校园开发者花式玩AI,亮点看这张图就够啦!
怎样申购打新债 开户是安全的吗
Single gene pan cancer + simple experiment can be published 7 points+
Introduction to C language circular statements (foe, while, do... While)
9+!通过深度学习从结直肠癌的组织学中预测淋巴结状态
Coinbase将推出首个针对零售交易员的加密衍生产品
怎么可以打新债 开户是安全的吗
What code did the full stack programmer write this month?
Variable parameter template implements max (accepts multiple parameters, two implementation methods)
Fizz gateway secondary development integration tutorial
5分+的单基因泛癌纯生信思路!
GTest从入门到入门
Opencv learning notes - cv:: mat class
广发证券靠谱吗?开证券账户安全吗?
【Go语言刷题篇】Go从0到入门4:切片的高级用法、初级复习与Map入门学习
12+!不同癌症中TMB与ICI反应之间的免疫相关因素研究
mRNA疫苗的研制怎么做?27+ 胰腺癌抗原和免疫亚型的解析来告诉你答案!
Ten thousand campus developers play AI in a fancy way. It's enough to see this picture!