{"id":478306,"date":"2022-07-27T11:04:34","date_gmt":"2022-07-27T08:04:34","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/"},"modified":"2022-07-27T11:04:34","modified_gmt":"2022-07-27T08:04:34","slug":"what-are-kubernetes-init-containers-and-when-should-you-use-them","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/","title":{"rendered":"#What Are Kubernetes Init Containers and When Should You Use Them?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a278b49ea141\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a278b49ea141\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/#%E2%80%9CWhat_Are_Kubernetes_Init_Containers_and_When_Should_You_Use_Them%E2%80%9D\" >&#8220;What Are Kubernetes Init Containers and When Should You Use Them?&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/#The_Role_of_Init_Containers\" >The Role of Init Containers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/#Adding_Init_Containers_to_a_Pod\" >Adding Init Containers to a Pod<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/#Common_Gotchas\" >Common Gotchas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/#When_to_Use_Init_Containers\" >When to Use Init Containers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CWhat_Are_Kubernetes_Init_Containers_and_When_Should_You_Use_Them%E2%80%9D\"><\/span>&#8220;What Are Kubernetes Init Containers and When Should You Use Them?&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9632\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2021\/02\/748108a6.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Kubernetes logo\" width=\"1602\" height=\"902\"\/><\/p>\n<p>Init containers are a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/init-containers\">Kubernetes mechanism for<\/a> initializing new Pods. Init containers start and exit before their Pod\u2019s main application containers, providing a chance to run bootstrap <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>s in sequential order.<\/p>\n<p>In this article we\u2019ll show how to add init containers to a Pod and consider some common use cases. Although init containers are configured similarly to regular containers, they do have some differences because of their specialized purpose.<\/p>\n<h2 id=\"the-role-of-init-containers\"><span class=\"ez-toc-section\" id=\"The_Role_of_Init_Containers\"><\/span>The Role of Init Containers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Init containers solve challenges associated with first-run initialization of applications. It\u2019s common for services to depend on the successful completion of a setup script before they can fully start up.<\/p>\n<p>In smaller systems, you could add the script to your existing application container image. However this isn\u2019t ideal as it adds another responsibility to the image. You may even have several distinct stages to complete, each with their own dependencies and relationships. Adding all these single-use operations to your main container image can quickly create bloated complexity that\u2019s hard to maintain.<\/p>\n<p>Init containers address this by letting you run specialized containers before a Pod\u2019s application containers start. Each Pod can have multiple init containers; they\u2019re guaranteed to run sequentially, only after the previous one has completed by exiting successfully.<\/p>\n<p>Kubernetes starts the Pod\u2019s regular containers once all the init containers have completed. If an init container fails, it will be restarted until it completes. When the Pod\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/pod-lifecycle\/#restart-policy\"><code>restartPolicy<\/code> is set to<\/a> <code>Never<\/code>, the Pod is marked as failed instead.<\/p>\n<h2 id=\"adding-init-containers-to-a-pod\"><span class=\"ez-toc-section\" id=\"Adding_Init_Containers_to_a_Pod\"><\/span>Adding Init Containers to a Pod<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Init containers are defined in the <code>spec.initContainers<\/code> field of a Pod\u2019s manifest. This is very similar to a regular <code>spec.containers<\/code> definition.<\/p>\n<p>Here\u2019s a example of a Pod with two init containers attached:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Pod<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>init-containers-pod<span class=\"co4\">\nspec<\/span>:<span class=\"co4\">\n  containers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>app-container<span class=\"co3\">\n      image<\/span><span class=\"sy2\">: <\/span>busybox:latest<span class=\"co3\">\n      command<\/span><span class=\"sy2\">: <\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"echo\"<\/span>, <span class=\"st0\">\"Started app\"<\/span><span class=\"br0\">]<\/span><span class=\"co4\">\n  initContainers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>first-init-container<span class=\"co3\">\n      image<\/span><span class=\"sy2\">: <\/span>busybox:latest<span class=\"co3\">\n      command<\/span><span class=\"sy2\">: <\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"echo\"<\/span>, <span class=\"st0\">\"This is the first init container\"<\/span><span class=\"br0\">]<\/span><span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>second-init-container<span class=\"co3\">\n      image<\/span><span class=\"sy2\">: <\/span>busybox:latest<span class=\"co3\">\n      command<\/span><span class=\"sy2\">: <\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"echo\"<\/span>, <span class=\"st0\">\"This is the second init container\"<\/span><span class=\"br0\">]<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Use Kubectl to add the Pod to your cluster:<\/p>\n<pre>$ kubectl apply -f pod.yaml&#13;\npod\/init-containers-pod created<\/pre>\n<p>Now retrieve the logs associated with each of the init containers to confirm they\u2019ve been run:<\/p>\n<pre>$ kubectl logs init-containers-pod -c first-init-container&#13;\nThis is the first init container&#13;\n&#13;\n$ kubectl logs init-containers-pod -c second-init-container&#13;\nThis is the second init container<\/pre>\n<p>You can use most properties available to Kubernetes <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/reference\/kubernetes-api\/workload-resources\/pod-v1\/#Container\">container manifests<\/a> within an <code>initContainers<\/code> field. These include volumes, ports, environment variables, and security contexts.<\/p>\n<p>Init containers support resource limits too but these are <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/init-containers\/#resources\">handled differently<\/a> to regular containers. The highest value of the resource limits declared by all the init containers is selected as the Pod\u2019s effective limit, unless it\u2019s lower than the sum of the limits on the Pod\u2019s application containers. This computed value will be used for scheduling purposes.<\/p>\n<p>One limitation of init containers is their lack of support for probes. You can\u2019t assign <code>livenessProbe<\/code>, <code>readinessProbe<\/code>, or <code>startupProbe<\/code> fields to container objects in the <code>initContainers<\/code> field. Init containers are a separate mechanism which you can use instead of or alongside probes attached to your main application containers.<\/p>\n<h2 id=\"common-gotchas\"><span class=\"ez-toc-section\" id=\"Common_Gotchas\"><\/span>Common Gotchas<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are a few common pitfalls when using init containers. Here are some details to remember:<\/p>\n<ul>\n<li><strong>Init containers run each time their Pod restarts.<\/strong> This means your init container operations should be idempotent so they\u2019re resilient to running twice in the same Pod. If the Pod is restarted, all its init containers will be executed again.<\/li>\n<li><strong>Changes to a Pod\u2019s <code>initContainers<\/code> field aren\u2019t supported, with one exception.<\/strong> You can modify the <code>image<\/code> field. This will cause the Pod to restart and run your new init containers.<\/li>\n<li><strong>Init container names need to be unique across all the containers in the Pod.<\/strong> This includes other init containers and your application containers. You\u2019ll see a YAML validation error in your console if you try to apply a manifest that breaks this rule.<\/li>\n<li><strong>Pods have an <code>Initialized: False<\/code> condition when init containers are running.<\/strong> This is visible under the <code>Conditions<\/code> heading when you run <code>kubectl describe my-pod<\/code>.<\/li>\n<\/ul>\n<p>You can also check whether a Pod\u2019s init containers have completed using the <code>kubectl get<\/code> command:<\/p>\n<pre>$ kubectl get init-containers-pod&#13;\nNAME                   READY     STATUS    RESTARTS    AGE&#13;\ninit-containers-pod    0\/1       Init:1\/2  0           1m<\/pre>\n<p>In this case, the <code>STATUS<\/code> column shows the Pod has two init containers, one of which has successfully completed. Once all the init containers finish, Kubernetes will start the application containers and the Pod\u2019s status will change to <code>Running<\/code>.<\/p>\n<h2 id=\"when-to-use-init-containers\"><span class=\"ez-toc-section\" id=\"When_to_Use_Init_Containers\"><\/span>When to Use Init Containers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Init containers are ideal whenever new deployments of your application need to be initialized in some way. They cater for dedicated pre-run tasks that depend on tooling outside your main container image.<\/p>\n<p>Here are a few situations where you might use init containers:<\/p>\n<ul>\n<li>Generating configuration files from environment variables.<\/li>\n<li>Pre-populating caches used by your application.<\/li>\n<li>Migrating and seeding a database instance.<\/li>\n<li>Downloading and installing application plugins into a volume.<\/li>\n<li>Blocking application start-up until dependencies are available (such as databases or external APIs).<\/li>\n<\/ul>\n<p>Another way to achieve some of these tasks is with a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/tasks\/configure-pod-container\/configure-liveness-readiness-startup-probes\">readiness or startup probe<\/a>. However there is a difference in intent: probes are primarily designed to communicate a container\u2019s state to Kubernetes whereas init containers are designated as a way to perform <em>actions<\/em> during Pod initialization.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Init containers are a way to perform first run initialization routines within a Kubernetes Pod. They can be used to block or delay application container startup while you wait for dependencies to become available or bootstrap scripts to complete.<\/p>\n<p>The functionality of init containers sometimes overlaps startup and readiness probes. You can use a probe when the activity you want to perform mostly concerns blocking application startup until a condition is met. They rely on your script already existing in your application\u2019s container image. An init container is a better choice when you want to perform specialist actions without bloating your main image with single-use utilities.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/devops\/what-are-kubernetes-init-containers-and-when-should-you-use-them\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;What Are Kubernetes Init Containers and When Should You Use Them?&#8221; Init containers are a Kubernetes mechanism for initializing new Pods. Init containers start and exit before their Pod\u2019s main application containers, providing a chance to run bootstrap scripts in sequential order. In this article we\u2019ll show how to add init containers to a Pod&#8230;<\/p>\n","protected":false},"author":1,"featured_media":478307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2021\/02\/748108a6.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-478306","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/478306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=478306"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/478306\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/478307"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=478306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=478306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=478306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}