{"id":82913,"date":"2020-05-26T13:42:32","date_gmt":"2020-05-26T11:42:32","guid":{"rendered":"https:\/\/prohoster.info\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-korrektnoe-otklyuchenie-terminate"},"modified":"2020-05-26T13:42:32","modified_gmt":"2020-05-26T11:42:32","slug":"luchshie-praktiki-kubernetes-korrektnoe-otklyuchenie-terminate","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-korrektnoe-otklyuchenie-terminate","title":{"rendered":"Kubernetes Best Practices. Proper Shutdown Terminate.","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p><noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/502052\/\">Best Practices for Kubernetes. Creating Small Containers<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/502320\/\">Best Practices for Kubernetes. Organizing Kubernetes with Namespaces<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/502430\/\">Best Practices for Kubernetes. Checking Kubernetes Health with Readiness and Liveness Tests<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/502614\/\">Best Practices for Kubernetes: Configuring Resource Requests and Limits<\/a><\/noindex><\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices. Proper Shutdown Terminate.\" src=\"\/wp-content\/uploads\/2020\/05\/4c3f50a0968007bbfa99d167b43d0a5a.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nAn important aspect of distributed systems is failure handling. Kubernetes helps with this by using controllers that monitor the state of your system and restart services that have stopped working. However, Kubernetes can also forcibly stop your applications to ensure the overall viability of the system. In this series, we will explore how to help Kubernetes perform its functions more efficiently and reduce application downtime. <noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<p>Before the advent of containers, most applications ran on virtual or physical machines. If an application crashed or hung, it took a long time to terminate the running task and reload the program. In the worst cases, someone had to resolve the issue manually at inconvenient hours. If an important task was handled by only 1-2 worker machines, such a failure was simply unacceptable. <br \/>\nTherefore, instead of manual restarts, process monitoring began to be used for automatically restarting applications in case of crashes. If a program failed, the monitoring process captures the exit code and restarts the server. With the advent of systems like Kubernetes, this type of system failure response was simply integrated into the infrastructure.<\/p>\n<p>Kubernetes uses an event loop of 'watching \u2013 diffing \u2013 acting' to ensure that resources remain operational along the path from containers to the nodes themselves.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices. Proper Shutdown Terminate.\" src=\"\/wp-content\/uploads\/2020\/05\/fe255d31824d2e570492a882ea7e54c7.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nThis means you no longer need to manually run process monitoring. If a resource fails a Health Check, Kubernetes will simply automatically provide a replacement. In this process, Kubernetes does much more than just watch for application failures. It can create additional copies of the application to run on multiple machines, update the application, or even run several versions of your app simultaneously.<br \/>\nThere are many reasons why Kubernetes might terminate a perfectly healthy container. For example, if you are updating your deployment, Kubernetes will gradually stop the old pods while simultaneously starting new ones. If you shut down a node, Kubernetes will stop all pods running on that node. Lastly, if a node runs out of resources, Kubernetes will shut down all pods to free up those resources.<\/p>\n<p>It is therefore crucial for your application to shut down with minimal impact on the end user and minimal recovery time. This means that before shutting down, it should save all necessary data, close all network connections, finish remaining tasks, and complete any other urgent actions.<\/p>\n<p>In practice, this means that your application must be able to handle the SIGTERM message \u2013 a termination signal that is the default for the kill utility in Unix-like operating systems. Upon receiving this message, the application should shut down.<\/p>\n<p>Once Kubernetes decides to terminate a pod, a series of events occurs. Let's take a look at each step Kubernetes takes when shutting down a container or pod.<\/p>\n<p>Suppose we want to terminate one of the pods. At this point, it will stop receiving new traffic \u2013 the containers running in the pod will not be affected, but all new traffic will be blocked.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices. Proper Shutdown Terminate.\" src=\"\/wp-content\/uploads\/2020\/05\/d89c31e39bfdc16e5a8db3b4e22f8dca.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nLet's examine the preStop hook \u2014 a special command or HTTP request sent to the containers in the pod. If your application does not shut down properly upon receiving SIGTERM, you can use preStop for a graceful shutdown. <\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices. Proper Shutdown Terminate.\" src=\"\/wp-content\/uploads\/2020\/05\/9d45a209daf3b5698266481680b45da2.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nMost programs shut down correctly upon receiving the SIGTERM signal, but if you are using third-party code or a system that you cannot fully control, the preStop hook serves as an excellent way to trigger a graceful shutdown without altering the application.<\/p>\n<p>After executing this hook, Kubernetes will send a SIGTERM signal to the containers in the pod, informing them that they will soon be shut down. Upon receiving this signal, your code will initiate the shutdown process. This process may involve stopping any long-lived connections, such as database or WebSocket connections, saving the current state, and so on.<\/p>\n<p>Even if you use the preStop hook, it\u2019s crucial to monitor what happens to your application when you send it a SIGTERM signal, how it behaves during this time, so that events or system changes caused by the pod's shutdown do not catch you off guard.<\/p>\n<p>At this moment, before taking further actions, Kubernetes will wait for a specified time, known as terminationGracePeriodSecond, or the grace period for proper shutdown upon receiving the SIGTERM signal.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices. Proper Shutdown Terminate.\" src=\"\/wp-content\/uploads\/2020\/05\/6f74959ff74252bb1ad2dd9c011c177c.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nBy default, this period is set to 30 seconds. It's important to note that it runs concurrently with the preStop hook and the SIGTERM signal. Kubernetes will not wait for the preStop hook and SIGTERM to complete \u2014 if your application finishes before the terminationGracePeriod ends, Kubernetes will immediately move on to the next step. Therefore, ensure that the value of this period in seconds is no less than the time required for a proper pod shutdown, and if it exceeds 30 seconds, increase the period to the required amount in YAML. In the example given, it is set to 60 seconds.<\/p>\n<p>Finally, the last step \u2014 if the containers are still running after the terminationGracePeriod, they will be sent a SIGKILL signal and forcibly removed. At this point, Kubernetes will also clean up any other pod objects.<\/p>\n<p><img decoding=\"async\" alt=\"Kubernetes Best Practices. Proper Shutdown Terminate.\" src=\"\/wp-content\/uploads\/2020\/05\/cb71079ffd166c955474738e5af6301a.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nKubernetes terminates pods for many reasons, so ensure that your application will be properly shut down in any case to maintain service stability. <\/p>\n<p><noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/503612\/\">Kubernetes Best Practices: Mapping External Services <\/a><\/noindex><\/p>\n<p><center><div class=\"youtube-placeholder\" data-id=\"Z_l_kE1MDTc\" onclick=\"loadVideo(this)\">\r\n        <img decoding=\"async\" src=\"https:\/\/img.youtube.com\/vi\/Z_l_kE1MDTc\/hqdefault.jpg\" alt=\"Play video\" loading=\"lazy\" width=\"480\" height=\"360\" style=\"width:100%;height:auto;\">\r\n        <div class=\"play-button\"><\/div>\r\n    <\/div><\/center><\/p>\n<h3>A little advertisement \ud83d\ude42<\/h3>\n<p>\nThank you for staying with us. Do you enjoy our articles? Want to see more interesting content? Support us by placing an order or recommending us to your friends, <noindex><a rel=\"nofollow\" href=\"https:\/\/ua-hosting.company\/cloudvps\/nl\">cloud VPS for developers starting at $4.99<\/a><\/noindex>, <b>a unique entry-level server alternative that we have created for you:<\/b> <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/company\/ua-hosting\/blog\/347386\/\">The whole truth about VPS (KVM) E5-2697 v3 (6 Cores) 10GB DDR4 480GB SSD 1Gbps from $19 or how to properly share a server?<\/a><\/noindex> (options available with RAID1 and RAID10, up to 24 cores and up to 40GB DDR4).<\/p>\n<p><b>Dell R730xd at half the price in the Equinix Tier IV data center in Amsterdam?<\/b> Only with us <b><noindex><a rel=\"nofollow\" href=\"https:\/\/ua-hosting.company\/serversnl\">2 x Intel TetraDeca-Core Xeon 2x E5-2697v3 2.6GHz 14C 64GB DDR4 4x960GB SSD 1Gbps 100TB starting at $199<\/a><\/noindex> in the Netherlands! <b>Dell R420 \u2014 2x E5-2430 2.2GHz 6C 128GB DDR3 2x960GB SSD 1Gbps 100TB \u2014 from $99!<\/b><\/b> Read about how <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/company\/ua-hosting\/blog\/329618\/\">To build a corporate-class infrastructure using Dell R730xd E5-2650 v4 servers costing 9000 euros for peanuts?<\/a><\/noindex><br \/>\n<br \/>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ua-hosting\/blog\/503488\/\">habr.com<\/a> <\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f Kubernetes \u0441 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\u043c \u0438\u043c\u0435\u043d \u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0436\u0438\u0437\u043d\u0435\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 Kubernetes \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0435\u0441\u0442\u043e\u0432 Readiness \u0438 Liveness \u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043b\u0438\u043c\u0438\u0442\u043e\u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0412\u0430\u0436\u043d\u044b\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u043c \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0441\u0431\u043e\u0435\u0432. Kubernetes \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0432 \u044d\u0442\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043b\u0435\u0434\u044f\u0442 \u0437\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0432\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":82914,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-82913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-korrektnoe-otklyuchenie-terminate\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 Terminate | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-korrektnoe-otklyuchenie-terminate\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-05-26T11:42:32+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-05-26T11:42:32+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47Best Practices for Kubernetes. Proper Shutdown Terminate | ProHoster","description":"Best practices for Kubernetes.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-korrektnoe-otklyuchenie-terminate","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes. \u041a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 Terminate | ProHoster","og:description":"\u041b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 Kubernetes.","og:url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/luchshie-praktiki-kubernetes-korrektnoe-otklyuchenie-terminate","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2020-05-26T11:42:32+00:00","article:modified_time":"2020-05-26T11:42:32+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"82913","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":null,"breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 15:28:37","updated":"2022-09-29 17:02:01","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/82913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=82913"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/82913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media\/82914"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=82913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=82913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=82913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}