{"id":22099,"date":"2021-07-08T09:32:03","date_gmt":"2021-07-08T09:32:03","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=22099"},"modified":"2025-07-03T11:46:11","modified_gmt":"2025-07-03T11:46:11","slug":"kubernetes-deployment-strategy-explained-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/kubernetes-deployment-strategy-explained-with-example\/","title":{"rendered":"kubernetes deployment strategy explained with example"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Fearture of Kubernetes Deployment<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Replication<\/li>\n\n\n\n<li>Controller<\/li>\n\n\n\n<li>Versioning<\/li>\n\n\n\n<li>Rollout<\/li>\n\n\n\n<li>Rollback<\/li>\n<\/ul>\n\n\n\n<p>Note:<br>ReplicaSets = Replication+Controller in the Deployment<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n<p>$ kubectl explain deploy.spec.strategy.type<br \/>\nKIND:     Deployment<br \/>\nVERSION:  apps\/v1<\/p>\n<p>FIELD:    type <string><\/p>\n<p>DESCRIPTION:<br \/>\n     Type of deployment. Can be &#8220;Recreate&#8221; or &#8220;RollingUpdate&#8221;. Default is<br \/>\n     RollingUpdate.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Kubernetes Deployement Strategy<\/h2>\n\n\n\n<p>Type of deployment<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Recreate<\/li>\n\n\n\n<li>RollingUpdate<\/li>\n<\/ul>\n\n\n\n<p>.spec.strategy specifies the strategy used to replace old Pods by new ones. .spec.strategy.type can be &#8220;Recreate&#8221; or &#8220;RollingUpdate&#8221;. &#8220;RollingUpdate&#8221; is the default value<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recreate Deployment<\/h2>\n\n\n\n<p>All existing Pods are killed before new ones are created when .spec.strategy.type==Recreate.<\/p>\n\n\n\n<p>This will only guarantee Pod termination previous to creation for upgrades. If you upgrade a Deployment, all Pods of the old revision will be terminated immediately. Successful removal is awaited before any Pod of the new revision is created. If you manually delete a Pod, the lifecycle is controlled by the ReplicaSet and the replacement will be created immediately (even if the old Pod is still in a Terminating state). If you need an &#8220;at most&#8221; guarantee for your Pods, you should consider using a StatefulSet.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"617\" height=\"480\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-1.png\" alt=\"\" class=\"wp-image-22107\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-1.png 617w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-1-300x233.png 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"613\" height=\"520\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-2.jpg\" alt=\"\" class=\"wp-image-22108\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-2.jpg 613w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-2-300x254.jpg 300w\" sizes=\"auto, (max-width: 613px) 100vw, 613px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"747\" height=\"517\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-3.jpg\" alt=\"\" class=\"wp-image-22109\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-3.jpg 747w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Recreate-3-300x208.jpg 300w\" sizes=\"auto, (max-width: 747px) 100vw, 747px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Rolling Update Deployment<\/h2>\n\n\n\n<p>The advantage of the rolling update strategy is that the update is applied Pod-by-Pod so the greater system can remain active.<\/p>\n\n\n\n<p>There is a minor performance reduction during this update process because the system is consistently one active Pod short of the desired number of Pods. This is often much preferred to a full system deactivation.<\/p>\n\n\n\n<p>The rolling update strategy is used as the default update strategy but isn\u2019t suited for all situations.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"634\" height=\"537\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Rollingupdate.jpg\" alt=\"\" class=\"wp-image-22111\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Rollingupdate.jpg 634w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Rollingupdate-300x254.jpg 300w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"522\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Rollingupdate2.jpg\" alt=\"\" class=\"wp-image-22112\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Rollingupdate2.jpg 445w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2021\/07\/kubernetes-deployment-strategy-example-Rollingupdate2-256x300.jpg 256w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">The Deployment updates Pods in a rolling update fashion when .spec.strategy.type==RollingUpdate. You can specify maxUnavailable <span class=\"hljs-keyword\">and<\/span> maxSurge to control the rolling update process.\n\n   maxSurge     &lt;string&gt;\n     The maximum number of pods that can be scheduled above the desired number\n     of pods. Value can be an absolute number (ex: <span class=\"hljs-number\">5<\/span>) <span class=\"hljs-keyword\">or<\/span> a percentage of desired\n     pods (ex: <span class=\"hljs-number\">10<\/span>%). This can not be <span class=\"hljs-number\">0<\/span> <span class=\"hljs-keyword\">if<\/span> MaxUnavailable is <span class=\"hljs-number\">0.<\/span> Absolute number\n     is calculated from percentage by rounding up. Defaults to <span class=\"hljs-number\">25<\/span>%. Example:\n     when this is set to <span class=\"hljs-number\">30<\/span>%, the <span class=\"hljs-keyword\">new<\/span> ReplicaSet can be scaled up immediately\n     when the rolling update starts, such that the total number of old <span class=\"hljs-keyword\">and<\/span> <span class=\"hljs-keyword\">new<\/span>\n     pods <span class=\"hljs-keyword\">do<\/span> not exceed <span class=\"hljs-number\">130<\/span>% of desired pods. Once old pods have been killed,\n     <span class=\"hljs-keyword\">new<\/span> ReplicaSet can be scaled up further, ensuring that total number of pods\n     running at any time during the update is at most <span class=\"hljs-number\">130<\/span>% of desired pods.\n\n   maxUnavailable       &lt;string&gt;\n     The maximum number of pods that can be unavailable during the update. Value\n     can be an absolute number (ex: <span class=\"hljs-number\">5<\/span>) <span class=\"hljs-keyword\">or<\/span> a percentage of desired pods (ex:\n     <span class=\"hljs-number\">10<\/span>%). Absolute number is calculated from percentage by rounding down. This\n     can not be <span class=\"hljs-number\">0<\/span> <span class=\"hljs-keyword\">if<\/span> MaxSurge is <span class=\"hljs-number\">0.<\/span> Defaults to <span class=\"hljs-number\">25<\/span>%. Example: when this is set\n     to <span class=\"hljs-number\">30<\/span>%, the old ReplicaSet can be scaled down to <span class=\"hljs-number\">70<\/span>% of desired pods\n     immediately when the rolling update starts. Once <span class=\"hljs-keyword\">new<\/span> pods are ready, old\n     ReplicaSet can be scaled down further, followed by scaling up the <span class=\"hljs-keyword\">new<\/span>\n     ReplicaSet, ensuring that the total number of pods available at all times\n     during the update is at least <span class=\"hljs-number\">70<\/span>% of desired pods.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">kubernetes deployment strategy example: Recreate<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: apps\/v1  \nkind: Deployment\nmetadata:\n  name: hello-deploy\nspec:\n  replicas: 10\n  selector:\n    matchLabels:\n      app: hello-world\n  minReadySeconds: 10\n  strategy:\n    type: Recreate\n    rollingUpdate:\n      maxUnavailable: 1\n      maxSurge: 1\n  template:\n    metadata:\n      labels:\n        app: hello-world\n    spec:\n      containers:\n      - name: hello-Pod\n        image: scmgalaxy\/nginx-devopsschoolv1\n        ports:\n        - containerPort: 80<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">kubernetes deployment strategy example: Rollingupdate<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: helloworld\nspec:\n  replicas: 3\n  strategy:\n    type: RollingUpdate\n    rollingUpdate:\n      maxSurge: 1\n      maxUnavailable: 1\n  minReadySeconds: 5\n  selector:\n    matchLabels:\n      app: helloworld\n  template:\n    metadata:\n      labels:\n        app: helloworld\n        track: stable\n    spec:\n      containers:\n      - name: helloworld\n        image: scmgalaxy\/nginx-devopsschoolv1\n        ports:\n        - containerPort: 80\n        resources:\n          requests:\n            cpu: 50m\n          limits:\n            cpu: 100m\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/c21dd36b14782f1e6b6a6937b4e09ea0.js\"><\/script>\n\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_84605\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_84174\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&autoplay=0&cc_load_policy=0&cc_lang_pref=&iv_load_policy=1&loop=0&rel=1&fs=1&playsinline=0&autohide=2&theme=dark&color=red&controls=1&disablekb=0&\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_84605\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Fearture of Kubernetes Deployment Note:ReplicaSets = Replication+Controller in the Deployment Kubernetes Deployement Strategy Type of deployment .spec.strategy specifies the strategy used to replace old Pods by new ones. .spec.strategy.type can be &#8220;Recreate&#8221; or &#8220;RollingUpdate&#8221;. &#8220;RollingUpdate&#8221; is the default value Recreate Deployment All existing Pods are killed before new ones are created when .spec.strategy.type==Recreate. This will&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[4859],"tags":[],"class_list":["post-22099","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/22099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=22099"}],"version-history":[{"count":8,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/22099\/revisions"}],"predecessor-version":[{"id":49937,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/22099\/revisions\/49937"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=22099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=22099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=22099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}