{"id":4087,"date":"2025-02-27T16:47:36","date_gmt":"2025-02-27T16:47:36","guid":{"rendered":"https:\/\/code2deploy.com\/blog\/?p=4087"},"modified":"2025-02-23T17:53:57","modified_gmt":"2025-02-23T17:53:57","slug":"draining-a-node-for-maintenance-in-kubernetes","status":"publish","type":"post","link":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/","title":{"rendered":"Draining a Node for Maintenance in Kubernetes"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Performing maintenance on a Kubernetes node requires safely evicting running pods without causing service disruptions. Kubernetes provides the <code>kubectl drain<\/code> command to gracefully remove a node from the cluster while ensuring workload continuity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Drain a Node?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Applying OS or Kubernetes updates<\/li>\n\n\n\n<li>Performing hardware upgrades or troubleshooting<\/li>\n\n\n\n<li>Scaling down nodes in a cluster<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Draining a Node Safely<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following command to safely drain a node:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl drain &lt;node-name&gt; --ignore-daemonsets --delete-emptydir-data<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of Flags:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--ignore-daemonsets<\/code>: Ensures that daemonset-managed pods (like logging or monitoring agents) are not evicted.<\/li>\n\n\n\n<li><code>--delete-emptydir-data<\/code>: Ensures that data stored in <code>emptyDir<\/code> volumes is deleted.<\/li>\n\n\n\n<li><code>--force<\/code>: Can be used to force eviction when pods have no associated controllers (use with caution).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Node Draining<\/h2>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Check Running Pods Before Draining<\/strong><code>kubectl get pods --all-namespaces --field-selector spec.nodeName=&lt;node-name><\/code>This helps identify which workloads will be affected.<\/li>\n\n\n\n<li><strong>Ensure Pod Disruption Budgets (PDBs) are Configured<\/strong><code>apiVersion: policy\/v1 kind: PodDisruptionBudget metadata: name: my-app-pdb spec: minAvailable: 1 selector: matchLabels: app: my-app<\/code>This prevents more than the allowed number of replicas from being evicted simultaneously.<\/li>\n\n\n\n<li><strong>Cordon the Node Before Draining<\/strong><code>kubectl cordon &lt;node-name><\/code>This prevents new pods from being scheduled on the node before draining.<\/li>\n\n\n\n<li><strong>Monitor Pod Evictions<\/strong><code>kubectl get pods -o wide --watch<\/code>Ensures all evictions occur without errors before proceeding.<\/li>\n\n\n\n<li><strong>Uncordon the Node After Maintenance<\/strong><code>kubectl uncordon &lt;node-name><\/code>This allows the node to accept new pods again.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Alternative Solutions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using Node Taints<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of draining, apply a taint to prevent scheduling while allowing existing pods to run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl taint nodes &lt;node-name&gt; key=value:NoSchedule<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using Cluster Autoscaler<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For managed Kubernetes services like EKS, GKE, and AKS, cluster autoscaler can handle node removal and rebalancing automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using <code>kubectl drain<\/code> is a safe way to prepare a Kubernetes node for maintenance without disrupting applications. Following best practices ensures minimal downtime and workload continuity.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Performing maintenance on a Kubernetes node requires safely evicting running pods without causing service disruptions. Kubernetes provides the kubectl drain command to gracefully remove a node from the cluster while ensuring workload continuity. Why Drain a Node? Draining a Node Safely Use the following command to safely drain a node: Explanation of Flags: Best [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,216,1],"tags":[545,10,546],"class_list":["post-4087","post","type-post","status-publish","format-standard","hentry","category-kubernetes","category-kubernetes-basic","category-uncategorized","tag-drain-a-node","tag-kubernetes","tag-maintenance-in-kubernetes"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Draining a Node for Maintenance in Kubernetes - code2deploy.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Draining a Node for Maintenance in Kubernetes - code2deploy.com\" \/>\n<meta property=\"og:description\" content=\"Introduction Performing maintenance on a Kubernetes node requires safely evicting running pods without causing service disruptions. Kubernetes provides the kubectl drain command to gracefully remove a node from the cluster while ensuring workload continuity. Why Drain a Node? Draining a Node Safely Use the following command to safely drain a node: Explanation of Flags: Best [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/\" \/>\n<meta property=\"og:site_name\" content=\"code2deploy.com\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-27T16:47:36+00:00\" \/>\n<meta name=\"author\" content=\"enam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"enam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/\"},\"author\":{\"name\":\"enam\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"headline\":\"Draining a Node for Maintenance in Kubernetes\",\"datePublished\":\"2025-02-27T16:47:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/\"},\"wordCount\":251,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"keywords\":[\"Drain a Node\",\"kubernetes\",\"Maintenance in Kubernetes\"],\"articleSection\":[\"Kubernetes\",\"Kubernetes Basic\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/\",\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/\",\"name\":\"Draining a Node for Maintenance in Kubernetes - code2deploy.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#website\"},\"datePublished\":\"2025-02-27T16:47:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/draining-a-node-for-maintenance-in-kubernetes\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Draining a Node for Maintenance in Kubernetes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/\",\"name\":\"code2deploy.com\\\/blog\",\"description\":\"TechOps\",\"publisher\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\",\"name\":\"enam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"caption\":\"enam\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\\\/\\\/code2deploy.com\\\/blog\"],\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/author\\\/enam\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Draining a Node for Maintenance in Kubernetes - code2deploy.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/","og_locale":"en_US","og_type":"article","og_title":"Draining a Node for Maintenance in Kubernetes - code2deploy.com","og_description":"Introduction Performing maintenance on a Kubernetes node requires safely evicting running pods without causing service disruptions. Kubernetes provides the kubectl drain command to gracefully remove a node from the cluster while ensuring workload continuity. Why Drain a Node? Draining a Node Safely Use the following command to safely drain a node: Explanation of Flags: Best [&hellip;]","og_url":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/","og_site_name":"code2deploy.com","article_published_time":"2025-02-27T16:47:36+00:00","author":"enam","twitter_card":"summary_large_image","twitter_misc":{"Written by":"enam","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/#article","isPartOf":{"@id":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/"},"author":{"name":"enam","@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"headline":"Draining a Node for Maintenance in Kubernetes","datePublished":"2025-02-27T16:47:36+00:00","mainEntityOfPage":{"@id":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/"},"wordCount":251,"commentCount":0,"publisher":{"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"keywords":["Drain a Node","kubernetes","Maintenance in Kubernetes"],"articleSection":["Kubernetes","Kubernetes Basic"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/","url":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/","name":"Draining a Node for Maintenance in Kubernetes - code2deploy.com","isPartOf":{"@id":"https:\/\/code2deploy.com\/blog\/#website"},"datePublished":"2025-02-27T16:47:36+00:00","breadcrumb":{"@id":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/code2deploy.com\/blog\/draining-a-node-for-maintenance-in-kubernetes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code2deploy.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Draining a Node for Maintenance in Kubernetes"}]},{"@type":"WebSite","@id":"https:\/\/code2deploy.com\/blog\/#website","url":"https:\/\/code2deploy.com\/blog\/","name":"code2deploy.com\/blog","description":"TechOps","publisher":{"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code2deploy.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b","name":"enam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","caption":"enam"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g"},"sameAs":["https:\/\/code2deploy.com\/blog"],"url":"https:\/\/code2deploy.com\/blog\/author\/enam\/"}]}},"_links":{"self":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/comments?post=4087"}],"version-history":[{"count":1,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4087\/revisions"}],"predecessor-version":[{"id":4088,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4087\/revisions\/4088"}],"wp:attachment":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/media?parent=4087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/categories?post=4087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/tags?post=4087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}