{"id":936,"date":"2020-08-21T17:34:29","date_gmt":"2020-08-21T17:34:29","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=936"},"modified":"2020-08-21T17:34:30","modified_gmt":"2020-08-21T17:34:30","slug":"services-k8s","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/08\/21\/services-k8s\/","title":{"rendered":"Services: K8S"},"content":{"rendered":"\n<p>There are multiple <code>Kubernetes<\/code> objects we will be talking about in this blog. Mainly Services, Selectors and Labels.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Service is an abstraction which defines a logical set of Pods and a policy by which to access them.<\/li><li>A Service in Kubernetes is a REST object, similar to a Pod.<\/li><li>Service tags the pods, logically grouping them using selectors.<\/li><li>Pods have a lifecycle, they are born, they live, process and then they die. The pods (desired state) can be managed using deployment.<\/li><li>A label is an identifying attribute of an object, and can be defined at the creation time, and subsequently modified and added at any time.<\/li><li>Label selector is a grouping primitive in Kubernetes and allows client\/user to identify a set of objects. There are two sets of selectors<ul><li><em>equality-based<\/em> and <\/li><li><em>set-based<\/em>.<\/li><\/ul><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-black-color\"><p>An abstract way to expose an application running on a set of <a rel=\"noreferrer noopener\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/\" target=\"_blank\">Pods<\/a> as a network service.<\/p><\/blockquote><\/figure>\n\n\n\n<p>The set of Pods targeted by a Service is usually determined by a selector.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-amber-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-black-color\"><p><em>Labels<\/em> are key\/value pairs that are attached to objects, such as pods at the time of creation.<\/p><\/blockquote><\/figure>\n\n\n\n<p>Labels allow for efficient queries and watches. Let&#8217;s learn by example. I will try and crate a NGINX pod and expose the port using a service. A simple exercise?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step &#8211; 1: Define the Pod<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: Pod\nmetadata:\n  name: nginx-pod\n  labels:\n    app: my-pod\n    author: saurabh\nspec:\n  containers:\n  - name: nginx-pod-container\n    image: nginx:latest<\/code><\/pre>\n\n\n\n<p>The above definition has labels applied to the pod, which will be used while creating a service and will be used in the selector. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k describe pod nginx-pod\nName:               nginx-pod\nNamespace:          default\nPriority:           0\nPriorityClassName:  &lt;none>\nNode:               ip-10-0-1-103\/10.0.1.103\nStart Time:         Fri, 21 Aug 2020 16:49:20 +0000\nLabels:             app=my-pod\n                    author=saurabh\nAnnotations:        kubectl.kubernetes.io\/last-applied-configuration:\n                      {\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"my-pod\",\"author\":\"saurabh\"},\"name\":\"nginx-pod\",\"namespace\":\"...\nStatus:             Running\nIP:                 10.244.2.3\n<\/code><\/pre>\n\n\n\n<p>You can use the labels for selectors as under<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k get pods -lapp=my-pod\nNAME        READY   STATUS    RESTARTS   AGE\nnginx-pod   1\/1     Running   0          14m<\/code><\/pre>\n\n\n\n<p>you can combine labels as well<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k get pods -lapp=my-pod,author=saurabh\nNAME        READY   STATUS    RESTARTS   AGE\nnginx-pod   1\/1     Running   0          15m<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>k get pods -l 'app in (my-pod,my-name)'\nNAME        READY   STATUS    RESTARTS   AGE\nnginx-pod   1\/1     Running   0          16m<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-amber-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-black-color\"><p>Each object in your cluster has a <em>Name<\/em> that is unique for that type of resource. Every Kubernetes object also has a <em>UID<\/em> that is unique across your whole cluster.<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step &#8211; 2: Define the service<\/h2>\n\n\n\n<p>Let&#8217;s create the service to expose the pod.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: Service\nmetadata:\n  name: my-svc\nspec:\n  type: NodePort\n  selector:\n    app: my-pod\n  ports:\n  - protocol: TCP\n    port: 8080\n    targetPort: 80                 <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-pale-pink-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-black-color\"><p>A Service can map <em>any<\/em> incoming <code>port<\/code> to a <code>targetPort<\/code>. By default and for convenience, the <code>targetPort<\/code> is set to the same value as the <code>port<\/code> field.<\/p><\/blockquote><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>k describe svc my-svc\nName:              my-svc\nNamespace:         default\nLabels:            &lt;none>\nAnnotations:       kubectl.kubernetes.io\/last-applied-configuration:\n                     {\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{},\"name\":\"my-svc\",\"namespace\":\"default\"},\"spec\":{\"ports\":&#91;{\"port\":8080,\"tar...\nSelector:          app=my-pod\nType:              ClusterIP\nIP:                10.102.126.164\nPort:              &lt;unset>  8080\/TCP\nTargetPort:        80\/TCP\nEndpoints:         10.244.2.4:80\nSession Affinity:  None\nEvents:            &lt;none><\/code><\/pre>\n\n\n\n<p>You can validate the service for endpoints<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k get ep\nNAME         ENDPOINTS                                 AGE\nmy-svc       10.244.2.4:80                             6m37s<\/code><\/pre>\n\n\n\n<p>I can also define the type of Service<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\napiVersion: v1\nkind: Service\nmetadata:\n  name: my-svc\nspec:\n  type: NodePort\n  selector:\n    app: my-pod\n  ports:\n    - port: 8080\n      targetPort: 80<\/code><\/pre>\n\n\n\n<p>and recreate the service.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE\nmy-svc       NodePort    10.110.6.32      &lt;none>        8080:31676\/TCP   2m50s<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/kubernetes.io\/docs\/concepts\/overview\/working-with-objects\/labels\/<\/li><li>https:\/\/kubernetes.io\/docs\/concepts\/overview\/components\/#kube-apiserver<\/li><li>https:\/\/kubernetes.io\/docs\/concepts\/services-networking\/service\/#virtual-ips-and-service-proxies<\/li><li>https:\/\/kubernetes.io\/docs\/concepts\/services-networking\/service\/#publishing-services-service-types<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>There are multiple Kubernetes objects we will be talking about in this blog. Mainly Services, Selectors and Labels. Service is an abstraction which defines a logical set of Pods and a policy by which to access them. A Service in Kubernetes is a REST object, similar to a Pod. Service tags the pods, logically grouping [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":942,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[18,96],"class_list":["post-936","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-k8s","tag-service","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=936"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/936\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/942"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}