{"id":23,"date":"2025-10-21T12:00:00","date_gmt":"2025-10-21T12:00:00","guid":{"rendered":"https:\/\/rezachegini.com\/?p=23"},"modified":"2026-10-30T06:36:24","modified_gmt":"2026-10-30T06:36:24","slug":"understanding-kubernetes-pods","status":"publish","type":"post","link":"https:\/\/rezachegini.com\/?p=23","title":{"rendered":"Understanding Kubernetes Pods"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Learning Kubernetes Pods &#8211; A Complete Application Setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Today I want to share what I learned about creating a complete Kubernetes application with both a Pod and Service in one file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a Pod?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Pod is the smallest unit you can create in Kubernetes. Think of it like a wrapper that holds one or more containers. Most of the time, one Pod contains just one container. The containers in a Pod share the same network and storage, so they can talk to each other easily.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Complete Application YAML File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the complete file that creates both a Pod and Service together:<\/p>\n\n\n<pre class=\"wp-block-preformatted\"># Pod Definition\napiVersion: v1\nkind: Pod\nmetadata:\n  name: myapp-pod\n  labels:\n    app: myapp\nspec:\n  containers:\n    - name: myapp\n      image: nginx\n      ports:\n      - containerPort: 80\n\n---\n\n# Service Definition\napiVersion: v1\nkind: Service\nmetadata:\n  name: myapp-pod-nodeport-service\nspec:\n  type: NodePort\n  selector:\n    app: myapp\n  ports:\n  - name: http\n    port: 80\n    targetPort: 80\n    nodePort: 31231<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Breaking Down the Complete File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Pod Section &#8211; Detailed Explanation:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apiVersion: v1          # Uses stable Kubernetes API version\nkind: Pod              # Tells Kubernetes we want to create a Pod\nmetadata:\n  name: myapp-pod      # Unique name for this Pod in the namespace\n  labels:\n    app: myapp         # Label used to identify and group this Pod\nspec:                  # Specification of what the Pod should do\n  containers:          # List of containers (usually just one)\n    - name: myapp      # Name of the container inside the Pod\n      image: nginx  # Docker image to run\n      ports:\n      - containerPort: 80  # Port the container listens on<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">What each part does:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>apiVersion: v1 &#8211; Uses the stable Pod API that works in all Kubernetes versions<\/li><li>name: myapp-pod &#8211; This is how we reference the Pod in other commands<\/li><li>labels: app: myapp &#8211; Like a tag that helps Services find this Pod<\/li><li>image &#8211; Downloads and runs this specific nginx container<\/li><li>containerPort: 80 &#8211; Tells Kubernetes the container accepts traffic on port 80<\/li><\/ul>\n\n\n<p class=\"wp-block-paragraph\">The Service Section &#8211; Detailed Explanation:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apiVersion: v1                    # Uses stable Service API\nkind: Service                     # Creates a Service resource\nmetadata:\n  name: myapp-pod-nodeport-service  # Service name for reference\nspec:\n  type: NodePort                  # Makes service accessible from outside\n  selector:\n    app: myapp                    # Finds 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 container\n    nodePort: 31231              # External port on the node<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">What each part does:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>type: NodePort &#8211; Opens a port on every cluster node for external access<\/li><li>selector: app: myapp &#8211; Connects to Pods that have label &#8220;app: myapp&#8221;<\/li><li>port: 80 &#8211; Port that other services inside cluster use to reach this service<\/li><li>targetPort: 80 &#8211; Port on the actual Pod where traffic gets sent<\/li><li>nodePort: 31231 &#8211; External port users can access from outside the cluster<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How They Work Together:<\/strong> The Service acts like a bridge between external users and internal Pods. Here is the traffic flow:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>User accesses <a href=\"http:\/\/NODE_IP:31231\">http:\/\/NODE_IP:31231<\/a><\/li><li>Service receives traffic on nodePort 31231<\/li><li>Service looks for Pods with label &#8220;app: myapp&#8221;<\/li><li>Service forwards traffic to Pod on targetPort 80<\/li><li>Pod container receives request on containerPort 80<\/li><li>Nginx serves the web page back to user<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why We Need Both:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Pod alone &#8211; Application runs but nobody can reach it from outside<\/li><li>Service alone &#8211; Nothing to connect to, no running application<\/li><li>Together &#8211; Complete working web application that users can access<\/li><\/ul>\n\n\n<h3 class=\"wp-block-heading\">How to Deploy and Manage This Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: Deploy the Application<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl apply -f myapp-complete.yaml<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Check if Pod is Running<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl get pods\nkubectl get pod myapp-pod<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3: See Detailed Pod Information<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl describe pod myapp-pod<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4: Check the Service<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl get services\nkubectl get svc myapp-pod-nodeport-service<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 5: See Service Details<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl describe service myapp-pod-nodeport-service<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 6: Check Pod Logs<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl logs myapp-pod<\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 7: Access Your Application<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Get node IP first\nkubectl get nodes -o wide\n\n# Then access your app at: http:\/\/NODE_IP:31231\n# For example: http:\/\/192.168.1.100:31231<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 8: Test Network Connection<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Check if pod can be reached internally\nkubectl exec -it myapp-pod -- curl localhost:80\n\n# Check service endpoints\nkubectl get endpoints myapp-pod-nodeport-service<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 9: Clean Up Resources<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl delete -f myapp-complete.yaml\n# Or delete individually:\n# kubectl delete pod myapp-pod\n# kubectl delete service myapp-pod-nodeport-service<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As a junior DevOps engineer, understanding Pods is important because:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>They are the foundation of how applications run in Kubernetes<\/li><li>You need to know how to create and connect them<\/li><li>Services help make your applications available to users<\/li><li>YAML files are the standard way to define infrastructure as code<\/li><li>kubectl commands help you manage and troubleshoot your applications<\/li><\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Learning Kubernetes Pods &#8211; A Complete Application Setup Today I want to share what I learned about creating a complete Kubernetes application&hellip;<\/p>\n","protected":false},"author":2,"featured_media":25,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_rezaops_part":"1","_rezaops_linkedin_url":"https:\/\/www.linkedin.com\/pulse\/understanding-kubernetes-pods-reza-chegini-ntfhc\/","footnotes":""},"categories":[10,9],"tags":[8,5,3,7,4,6],"series":[11],"class_list":["post-23","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-kubernetes","tag-devops","tag-kubectl","tag-kubernetes","tag-kubernetes-services","tag-pods","tag-yaml","series-understanding-kubernetes"],"jetpack_featured_media_url":"https:\/\/rezachegini.com\/wp-content\/uploads\/2026\/10\/understanding-kubernetes-pods-cover.png","_links":{"self":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/23","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=23"}],"version-history":[{"count":1,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions\/24"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/media\/25"}],"wp:attachment":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}