{"id":922,"date":"2023-08-21T10:08:06","date_gmt":"2023-08-21T04:38:06","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=922"},"modified":"2025-02-04T12:10:08","modified_gmt":"2025-02-04T06:40:08","slug":"services","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=922","title":{"rendered":"Services"},"content":{"rendered":"<p>In this lab we will cover following services:<\/p>\n<ul>\n<li>Cluster IP type<\/li>\n<li>Nodeport<\/li>\n<li>Service Without Selectors (endpoints)<\/li>\n<li>Headless services<\/li>\n<li>LB type services<\/li>\n<\/ul>\n<p>Lab1: Cluster\u00a0 IP type service<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clustersvc.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-923\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clustersvc-300x178.jpg\" alt=\"\" width=\"300\" height=\"178\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clustersvc-300x178.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clustersvc.jpg 567w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<ul>\n<li><code>apiVersion<\/code>: The version of the Kubernetes API being used. In this case, it&#8217;s <code>v1<\/code>, which is the basic version of the API.<\/li>\n<li><code>kind<\/code>: The type of Kubernetes resource being defined. Here, it&#8217;s a <code>Service<\/code>.<\/li>\n<li><code>metadata<\/code>: Metadata about the service resource, including its <code>name<\/code>.<\/li>\n<li><code>spec<\/code>: The specification for the service, which includes:\n<ul>\n<li><code>selector<\/code>: A set of labels that are used to identify the pods to which this service should route traffic. In this case, it&#8217;s selecting pods with the label <code>app.kubernetes.io\/name: proxy<\/code>.<\/li>\n<li><code>ports<\/code>: The ports that the service should expose. In this case, there&#8217;s one port defined:\n<ul>\n<li><code>name<\/code>: A name for the port.<\/li>\n<li><code>protocol<\/code>: The protocol used for the port, in this case, <code>TCP<\/code>.<\/li>\n<li><code>port<\/code>: The port number as exposed by the service.<\/li>\n<li><code>targetPort<\/code>: The port on the pods to which the traffic will be forwarded. <code>http-web-svc<\/code> should be a name or a number representing the port used by the pods.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/svcpod.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-924\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/svcpod-300x165.jpg\" alt=\"\" width=\"300\" height=\"165\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/svcpod-300x165.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/svcpod.jpg 640w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The <code>labels<\/code> in the pod&#8217;s metadata match the <code>selector<\/code> in the previously defined Service. This means that the Service you defined earlier (<code>nginx-service<\/code>) would route traffic to this pod based on the label <code>app.kubernetes.io\/name: proxy.<\/code><\/p>\n<p>git clone <a href=\"https:\/\/github.com\/amitopenwriteup\/k8s.git\">https:\/\/github.com\/amitopenwriteup\/k8s.git<\/a><\/p>\n<pre>kubectl create -f svc.yaml\r\nkubectl get svc\r\nkubectl get ep\r\nkubectl create -f dep.yaml\r\nkubectl get pods\r\nkubectl get ep\r\n\r\n##Delete the service\r\nkubectl delete -f svc.yaml\r\nkubectl detete -f dep.yaml<\/pre>\n<p>Lab 2 : NodePort<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Nodeport.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-926\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Nodeport-300x104.jpg\" alt=\"\" width=\"531\" height=\"184\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Nodeport-300x104.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Nodeport-700x244.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Nodeport-768x267.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Nodeport.jpg 1325w\" sizes=\"auto, (max-width: 531px) 100vw, 531px\" \/><\/a><\/p>\n<ul>\n<li><code>selector<\/code>: A set of labels that are used to identify the pods to which this service should route traffic. In this case, it&#8217;s selecting pods with the label <code>app.kubernetes.io\/name: proxy<\/code>.<\/li>\n<li><code>ports<\/code>: The ports that the service should expose. In this case, there&#8217;s one port defined:\n<ul>\n<li><code>port<\/code>: The port number as exposed by the service.<\/li>\n<li><code>targetPort<\/code>: The port on the pods to which the traffic will be forwarded. This is set to <code>80<\/code> to match the port the pods are listening on.<\/li>\n<li><code>nodePort<\/code>: An optional field that allows you to specify a specific port number on the nodes. If not specified, Kubernetes will allocate a port from the default range (typically <code>30000<\/code> to <code>32767<\/code>).<\/li>\n<li><code>nodePort<\/code> (e.g., <code>http:\/\/&lt;Node_IP&gt;:30007<\/code>).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>kubectl create -f dep.yaml\r\nkubectl create -f Nodeport.yaml\r\nkubectl get svc\r\nkubectl get ep\r\n\r\n#Delete the nodeport service\r\nkubectl delete svc my-service<\/pre>\n<p>Lab3: Service without Selectors<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/withoutsel.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-927\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/withoutsel-300x186.jpg\" alt=\"\" width=\"300\" height=\"186\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/withoutsel-300x186.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/withoutsel.jpg 401w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This service definition does not include a <code>selector<\/code>, which means it doesn&#8217;t specify which pods the service should route traffic to. Without a selector, the service will not be associated with any pods, and it won&#8217;t have any effect in terms of routing traffic. We need to mention the endpoint kind object for this service.<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/ep.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-928\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/ep-300x147.jpg\" alt=\"\" width=\"300\" height=\"147\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/ep-300x147.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/ep.jpg 482w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This Endpoints definition manually specifies that the Service named <code>my-svc<\/code> should route traffic to the IP address <code>192.168.40.131<\/code> on port <code>80<\/code>. This can be useful if you need to route traffic to specific IP addresses and ports that are not automatically managed by Kubernetes Service discovery.<\/p>\n<pre> kubectl create -f svcwithoutsel.yaml\r\nkubectl get svc\r\nkubectl get ep\r\nkubectl create -f ep.yaml\r\nkubectl get ep<\/pre>\n<p>Lab4: Headless service<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/headless.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-929\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/headless-300x201.jpg\" alt=\"\" width=\"300\" height=\"201\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/headless-300x201.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/headless.jpg 469w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><code>clusterIP: None<\/code> specifies that no cluster IP should be assigned, making it a Headless Service.When you create a Headless Service, DNS records are automatically created for each pod that matches the selector. These DNS records can be used to directly resolve the IP addresses of individual pods in the service. Use case: statefulsets<\/p>\n<pre> kubectl create -f headless.yaml\r\nkubectl get svc\r\n#Delete service\r\nkubeclt delete -f headless.yaml<\/pre>\n<p>Lab5: Loadbalancer Type Service:<\/p>\n<p>Setup the MetalLB is a load balancer implementation<\/p>\n<p>Install LB<\/p>\n<pre>kubectl apply <span class=\"hljs-_\">-f<\/span> https:\/\/raw.githubusercontent.com\/metallb\/metallb\/v0.14.3\/config\/manifests\/metallb-native.yaml<\/pre>\n<p>setting up ip range for lb<\/p>\n<pre>kubectl get pods -n metallb-system<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/lb.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-931\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/lb-300x200.jpg\" alt=\"\" width=\"300\" height=\"200\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/lb-300x200.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/lb.jpg 509w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><code>type<\/code>: The service type. In this case, it&#8217;s <code>LoadBalancer<\/code>, which requests a load balancer from the cloud provider (if available) and directs external traffic to the service. Please note that the availability of load balancers depends on your Kubernetes environment and cloud provider. In some cases, you might need to configure the cloud provider integration to enable automatic load balancer provisioning.<\/p>\n<pre>kubectl create -f lb.yaml\r\nkubectl get svc<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lab we will cover following services: Cluster IP type Nodeport Service Without Selectors (endpoints) Headless services LB type services Lab1: Cluster\u00a0 IP type service apiVersion: The version of the Kubernetes API being used. In this case, it&#8217;s v1, which is the basic version of the API. kind: The type of Kubernetes resource being [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-922","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=922"}],"version-history":[{"count":9,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/922\/revisions"}],"predecessor-version":[{"id":1483,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/922\/revisions\/1483"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}