{"id":1708,"date":"2024-08-31T03:03:03","date_gmt":"2024-08-31T03:03:03","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1708"},"modified":"2025-05-23T17:55:06","modified_gmt":"2025-05-23T17:55:06","slug":"kubernetes-service-nodeport","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1708","title":{"rendered":"Kubernetes: Service : NodePort"},"content":{"rendered":"<p>The\u00a0NodePort is one of the three services in Kubernetes.<\/p>\n<p>When a pod is deployed with a container, the worker node in which the pod gets deployed has an IP\u00a0accessible from the master node, etc and the pod also has a private IP that can be accessed once you are inside the worker node.<\/p>\n<p>If the pod hosts a container that runs httpd, the site can be accessed from within the worker node, by default via the pod&#8217;s private IP, but not from anywhere outside the worker node. This is were one of the &#8220;service&#8221; by Kubernetes comes to play.<\/p>\n<p>The Kubernetes service that <em>connects<\/em> the pod&#8217;s port, which may be TCP 80 to a newly assigned TCP port of 30008 of the worker is the <strong>NodePort<\/strong> service. We are talking about 3 ports here:<\/p>\n<p><strong>Node Port:<\/strong> The port of the worker node. This has to be within the range of <strong>30,000<\/strong> to <strong>32,767<\/strong><\/p>\n<p><strong>Pod Port:<\/strong> The port used by the Pod<\/p>\n<p><strong>Port:<\/strong> The port in the &#8220;service&#8221; that connects to Pod Port.<\/p>\n<p>As every <em>pod<\/em> got an IP, every\u00a0<em>service<\/em> too have an IP.<\/p>\n<p>WorkerNode with IP <span style=\"color: #339966;\">192.168.146.130<\/span> (listening tcp\/<span style=\"color: #0000ff;\">30001<\/span>) <span style=\"color: #ff6600;\">&#8211;&gt;<\/span> Service with IP\u00a010.98.33.154 (connecting port tcp\/80) <span style=\"color: #ff6600;\">&#8211;&gt;<\/span> Pod with IP\u00a0172.16.135.84 (listening to port 80)<\/p>\n<p>Below is a configuration file for the service.<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong> cat mynodeport.yml<\/p>\n<p><em>===== ===<\/em><br \/>\n<em>apiVersion: v1<\/em><br \/>\n<em>kind: Service<\/em><br \/>\n<em>metadata:<\/em><br \/>\n<em>\u00a0 \u00a0 name: mynodeportservice<\/em><br \/>\n<em>spec:<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0type: NodePort<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0ports:<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0&#8211; targetPort: 80<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0port: 80<\/em><br \/>\n<em> \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 nodePort: 30001<\/em><br \/>\n<span style=\"color: #ff6600;\"><em> \u00a0 \u00a0\u00a0selector:<\/em><\/span><br \/>\n<span style=\"color: #ff6600;\"> <em> \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0theApp: sv1<\/em><\/span><\/p>\n<p><em>===== ===<\/em><\/p>\n<p>Now create a pod named &#8220;mypod&#8221; using a manifest file in yaml format and deploy the pod. Ensure there is a label attached to the pod that reads &#8220;<em>theApp: sv1<\/em>&#8220;. The image can be <em>httpd<\/em>.<\/p>\n<p><strong>[root@kubmaster01 ~]#<\/strong>\u00a0kubectl create -f mynodeport.yml<\/p>\n<p>This will spin-up a &#8220;service&#8221; and work with any running pod were the label is &#8220;<em>theApp: sv1<\/em>&#8220;.<\/p>\n<p>The below command from your laptop, or even from the\u00a0<em>master<\/em> node should display the default webpage hosted by\u00a0<em>mypod<\/em>.<\/p>\n<ul>\n<li>httpd:\/\/<span style=\"color: #339966;\">192.168.146.130<\/span>:<span style=\"color: #0000ff;\">30001<\/span><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The\u00a0NodePort is one of the three services in Kubernetes. When a pod is deployed with a container, the worker node in which the pod gets <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1708\" title=\"Kubernetes: Service : NodePort\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1784,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,34],"tags":[],"class_list":["post-1708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-kubernetes"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1708"}],"version-history":[{"count":14,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1708\/revisions"}],"predecessor-version":[{"id":1729,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1708\/revisions\/1729"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/1784"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1708"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}