{"id":31,"date":"2025-10-23T12:00:00","date_gmt":"2025-10-23T12:00:00","guid":{"rendered":"https:\/\/rezachegini.com\/?p=31"},"modified":"2026-10-30T06:38:34","modified_gmt":"2026-10-30T06:38:34","slug":"understanding-kubernetes-deployments","status":"publish","type":"post","link":"https:\/\/rezachegini.com\/?p=31","title":{"rendered":"Understanding Kubernetes Deployments"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Today I want to share what I learned about Kubernetes Deployments and how they provide the best way to manage applications in production.<\/p>\n\n<h3 class=\"wp-block-heading\">What is a Deployment?<\/h3>\n\n<p class=\"wp-block-paragraph\">A Deployment is the highest-level controller in Kubernetes that manages ReplicaSets and Pods. Think of it as an advanced manager that not only keeps your application running but also handles updates, rollbacks, and version management. While ReplicaSets ensure multiple Pod copies run, Deployments add intelligent update strategies and version history on top of that.<\/p>\n\n<h3 class=\"wp-block-heading\">Complete Deployment Application YAML File<\/h3>\n\n<p class=\"wp-block-paragraph\">Here is the complete file that creates both a Deployment and Service together:<\/p>\n\n<pre class=\"wp-block-code\"><code># Deployment Definition\napiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: myapp3-deployment\nspec:\n  replicas: 3\n  selector:\n    matchLabels:\n      app: myapp3\n  template:\n    metadata:\n      name: myapp3-pod\n      labels:\n        app: myapp3\n    spec:\n      containers:\n      - name: myapp3-container\n        image: nginx\n        ports:\n        - containerPort: 80\n\n---\n\n# Service Definition\napiVersion: v1\nkind: Service\nmetadata:\n  name: deployment-nodeport-service\nspec:\n  type: NodePort\n  selector:\n    app: myapp3\n  ports:\n  - name: http\n    port: 80\n    targetPort: 80\n    nodePort: 31233<\/code><\/pre>\n\n<h3 class=\"wp-block-heading\">Breaking Down the Complete File<\/h3>\n\n<p class=\"wp-block-paragraph\">The Deployment Section &#8211; Detailed Explanation:<\/p>\n\n<pre class=\"wp-block-code\"><code>apiVersion: apps\/v1           # Uses apps API group for Deployments\nkind: Deployment             # Creates a Deployment controller\nmetadata:\n  name: myapp3-deployment    # Name of the Deployment\nspec:\n  replicas: 3                # Desired number of Pod instances\n  selector:                  # How Deployment finds its Pods\n    matchLabels:\n      app: myapp3            # Manages Pods with this label\n  template:                  # Pod template for creating instances\n    metadata:\n      name: myapp3-pod       # Name pattern for Pods\n      labels:\n        app: myapp3          # Label applied to each Pod\n    spec:                    # Pod specification\n      containers:\n      - name: myapp3-container        # Container name\n        image: nginx  # Docker image version\n        ports:\n        - containerPort: 80           # Port the container listens on<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">What each part does:<\/p>\n\n<ul class=\"wp-block-list\"><li>kind: Deployment &#8211; Creates the most advanced Kubernetes controller<\/li><li>replicas: 3 &#8211; Maintains 3 Pod instances through a managed ReplicaSet<\/li><li>selector.matchLabels &#8211; Links Deployment to Pods with label &#8220;app: myapp3&#8221;<\/li><li>template &#8211; Blueprint for Pods that Deployment creates via ReplicaSet<\/li><li>image: kubenginx:3.0.0 &#8211; Uses version 3.0.0 for this deployment<\/li><li>Automatic ReplicaSet Management &#8211; Deployment creates and manages ReplicaSets automatically<\/li><\/ul>\n\n<p class=\"wp-block-paragraph\">The Service Section &#8211; Detailed Explanation:<\/p>\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1                      # Uses core API for Services\nkind: Service                       # Creates a Service resource\nmetadata:\n  name: deployment-nodeport-service # Service name for reference\nspec:\n  type: NodePort                    # Makes service accessible externally\n  selector:\n    app: myapp3                     # Connects to Pods with this label\n  ports:\n  - name: http                      # Name for this port mapping\n    port: 80                        # Port inside the cluster\n    targetPort: 80                  # Port on the Pod containers\n    nodePort: 31233                 # External port on nodes<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Key differences from ReplicaSet Service:<\/p>\n\n<ul class=\"wp-block-list\"><li>selector: app: myapp3 &#8211; Connects to all Pods managed by Deployment<\/li><li>nodePort: 31233 &#8211; Different external port to avoid conflicts<\/li><li>Works with Deployment-managed Pods &#8211; Service automatically adapts during updates<\/li><\/ul>\n\n<p class=\"wp-block-paragraph\">How They Work Together: The Deployment creates and manages ReplicaSets, which create Pods, while the Service provides stable access:<\/p>\n\n<ol class=\"wp-block-list\"><li>Deployment creates a ReplicaSet to manage 3 Pods<\/li><li>ReplicaSet ensures 3 Pods are always running<\/li><li>Service distributes traffic among all healthy Pods<\/li><li>During updates, Deployment creates new ReplicaSet gradually<\/li><li>Service automatically includes new Pods and excludes old ones<\/li><li>Zero-downtime updates happen automatically<\/li><\/ol>\n\n<p class=\"wp-block-paragraph\">Why Deployments Are Essential:<\/p>\n\n<ul class=\"wp-block-list\"><li>Rolling Updates &#8211; Update applications without downtime<\/li><li>Rollback Capability &#8211; Quickly revert to previous versions<\/li><li>Version History &#8211; Track all deployment changes<\/li><li>Update Strategies &#8211; Control how updates happen<\/li><li>Production Ready &#8211; Industry standard for application management<\/li><\/ul>\n\n<h3 class=\"wp-block-heading\">How to Deploy and Manage This Application<\/h3>\n\n<p class=\"wp-block-paragraph\">Step 1: Deploy the Application<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl apply -f deployment-complete.yaml<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 2: Check Deployment Status<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get deployments\nkubectl get deployment myapp3-deployment<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 3: See Detailed Deployment Information<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl describe deployment myapp3-deployment<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 4: Check ReplicaSet Created by Deployment<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get replicasets\nkubectl get rs -l app=myapp3<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 5: Check All Pods Created<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get pods\nkubectl get pods -l app=myapp3<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 6: Check the Service<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get services\nkubectl describe service deployment-nodeport-service<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 7: Update the Application (Rolling Update)<\/p>\n\n<pre class=\"wp-block-code\"><code># Update to new image version\nkubectl set image deployment\/myapp3-deployment myapp3-container=stacksimplify\/kubenginx:4.0.0\n\n# Watch the rolling update happen\nkubectl rollout status deployment\/myapp3-deployment<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 8: Check Rollout History<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl rollout history deployment\/myapp3-deployment<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 9: Rollback to Previous Version<\/p>\n\n<pre class=\"wp-block-code\"><code># Rollback to previous version\nkubectl rollout undo deployment\/myapp3-deployment\n\n# Rollback to specific revision\nkubectl rollout undo deployment\/myapp3-deployment --to-revision=1<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 10: Scale the Deployment<\/p>\n\n<pre class=\"wp-block-code\"><code># Scale up to 5 replicas\nkubectl scale deployment myapp3-deployment --replicas=5\n\n# Scale down to 2 replicas\nkubectl scale deployment myapp3-deployment --replicas=2<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 11: Access Your Application<\/p>\n\n<pre class=\"wp-block-code\"><code># Get node IP\nkubectl get nodes -o wide\n\n# Access your app at: http:\/\/NODE_IP:31233\n# Service works seamlessly during updates<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 12: Monitor Deployment Events<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get events --field-selector involvedObject.name=myapp3-deployment<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 13: Clean Up Resources<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl delete -f deployment-complete.yaml\n# This removes Deployment, ReplicaSet, Pods, and Service<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Understanding Deployments is critical for junior DevOps engineers because:<\/p>\n\n<ul class=\"wp-block-list\"><li>They are the standard way to run applications in production<\/li><li>They provide zero-downtime updates and reliable rollbacks<\/li><li>They demonstrate advanced Kubernetes concepts like controllers<\/li><li>They show how to manage application lifecycle in real environments<\/li><li>They are essential knowledge for any Kubernetes job interview<\/li><\/ul>","protected":false},"excerpt":{"rendered":"<p>Today I want to share what I learned about Kubernetes Deployments and how they provide the best way to manage applications in&hellip;<\/p>\n","protected":false},"author":2,"featured_media":33,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_rezaops_part":"3","_rezaops_linkedin_url":"https:\/\/www.linkedin.com\/pulse\/understanding-kubernetes-deployments-reza-chegini-digyc\/","footnotes":""},"categories":[10,9],"tags":[13,8,5,3,12,6],"series":[11],"class_list":["post-31","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-kubernetes","tag-deployments","tag-devops","tag-kubectl","tag-kubernetes","tag-replicasets","tag-yaml","series-understanding-kubernetes"],"jetpack_featured_media_url":"https:\/\/rezachegini.com\/wp-content\/uploads\/2026\/10\/understanding-kubernetes-deployments-cover.png","_links":{"self":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=31"}],"version-history":[{"count":1,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions\/32"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/media\/33"}],"wp:attachment":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=31"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}