{"id":504,"date":"2020-04-07T16:58:30","date_gmt":"2020-04-07T16:58:30","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=504"},"modified":"2020-04-08T11:42:18","modified_gmt":"2020-04-08T11:42:18","slug":"kubectl","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/04\/07\/kubectl\/","title":{"rendered":"Kubectl &#8211; A revisit"},"content":{"rendered":"<p>It is always fun learning and trying out some new tech. In this blog, I will try and capture my experience of running my first pod using <code>kubectl<\/code>.<\/p>\n<p>If you wish to learn how to install a typical master, worker cluster of K8S I recommend going through this <a href=\"https:\/\/www.tecmint.com\/install-kubernetes-cluster-on-centos-7\/\">blog<\/a>.<\/p>\n<blockquote>\n<p><span style=\"color: #ff0000;\">A Kubernetes cluster can be deployed on either physical or virtual machines.<\/span><\/p>\n<\/blockquote>\n<p>My setup looks something like this.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-558 size-large\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-08-at-4.20.43-PM-1024x282.png\" alt=\"\" width=\"640\" height=\"176\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-08-at-4.20.43-PM-1024x282.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-08-at-4.20.43-PM-300x83.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-08-at-4.20.43-PM-768x211.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-08-at-4.20.43-PM.png 1504w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>The `<strong>setup`<\/strong> has 1 master and few slaves as shown in image above.<\/p>\n<h2>Point to remember<\/h2>\n<p><code>Kubectl<\/code> uses the <code>Kubernetes API<\/code> to interact with the cluster.<\/p>\n<h2>Terminologies<\/h2>\n<h3>Master<\/h3>\n<p><b>The Master is responsible for managing the cluster.<\/b> The master coordinates all activities in your cluster, such as scheduling applications, maintaining applications&#8217; desired state, scaling applications, and rolling out new updates. (from official <a href=\"https:\/\/kubernetes.io\/docs\/tutorials\/kubernetes-basics\/create-cluster\/cluster-intro\/\">documentation<\/a>)<\/p>\n<h3>Nodes<\/h3>\n<p><b>A node is a VM or a physical computer that serves as a worker machine in a Kubernetes cluster.<\/b> Each node has a <code>Kubelet<\/code>, which is an agent for managing the node and communicating with the <code>Kubernetes<\/code> master.<\/p>\n<blockquote>\n<p><span style=\"color: #ff0000;\">When you deploy applications on Kubernetes, you tell the master to start the application containers.<\/span><\/p>\n<\/blockquote>\n<ul>\n<li>https:\/\/kubernetes.io\/docs\/concepts\/overview\/kubernetes-api\/<\/li>\n<li>https:\/\/kubernetes.io\/docs\/tutorials\/kubernetes-basics\/create-cluster\/cluster-intro\/<\/li>\n<\/ul>\n<h3>Deployment<\/h3>\n<p>Declarative management for pods and replicaset. One can describe a <em>desired state<\/em> in a Deployment, and the <a class=\"glossary-tooltip\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/architecture\/controller\/\" target=\"_blank\" rel=\"noopener noreferrer\">Controller<\/a> changes the actual state to the desired state at a controlled rate.\u00a0<\/p>\n<h2><span style=\"color: #ff0000;\">Pre-requisites<\/span><\/h2>\n<blockquote>\n<p><span style=\"color: #ff0000;\">Once you have a running <code>Kubernetes<\/code> cluster, you can deploy your containerized applications on top of it.<\/span><\/p>\n<\/blockquote>\n<p>The primary way of executing commands is the <code>kubectl<\/code> which exposes multiple options that helps the developer orchestration.<\/p>\n<h2>YAML<\/h2>\n<p>(YAML a&#8217;int markup <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/reference_appendices\/YAMLSyntax.html\">language<\/a>) This is the chosen syntax to write the configuration files that will provide the necessary instructions to do the required.<\/p>\n<h2>No deep dive&#8230;<\/h2>\n<p>This blog I will not cover all the concepts of Containers, Kubernetes (K8s) resources etc. It is just a quick and dirt start to using K8S to launch a pod and see it running.<\/p>\n<h2>POD<\/h2>\n<p>Pod is essentials the smallest building block in the K8S universe, and I will define a quick pod as following.<\/p>\n<div>\u00a0<\/div>\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\"><code>apiVersion: v1\nkind: Pod\nmetadata:\n name: nginxapp\nlabels:\n app: nginx\n rel: stable\nspec:\n containers:\n  - name: nginxapp\n  image: nginx:alpine\n  ports:\n   - containerPort: 80<\/code>\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Indentation is quite important while you are writing YML&#8217;s and space is the preferred way of indenting.<\/p>\n\n\n\n<p>Once this YML is defined time to call the <code>kubectl apply<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"136\" src=\"https:\/\/www.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-07-at-10.05.51-PM-1024x136.png\" alt=\"\" class=\"wp-image-511\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-07-at-10.05.51-PM-1024x136.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-07-at-10.05.51-PM-300x40.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-07-at-10.05.51-PM-768x102.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-07-at-10.05.51-PM-1536x203.png 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-07-at-10.05.51-PM-2048x271.png 2048w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-07-at-10.05.51-PM-850x113.png 850w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Validate before you apply<\/figcaption><\/figure>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p>Once you call <code>kubectl apply -f  nginx.file.yml<\/code> you can call get pods to see the contents.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@master&gt;<strong>kubectl get pods<\/strong>\nNAME READY STATUS RESTARTS AGE\nnginxapp 0\/1 ContainerCreating 0 6s<\/pre>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<p>If I use <\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">kubectl create -f nginx.pod.yml <\/pre>\n<\/div><\/div>\n\n\n\n<p>and see the pod by calling <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl get pod nginxapp -o yaml<\/pre>\n\n\n\n<p>I can see the output as under<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@master&gt;kubectl get pod nginxapp -o yaml\napiVersion: v1\nkind: Pod\nmetadata:\ncreationTimestamp: \"2020-04-07T16:42:41Z\"\nlabels:\napp: nginx\nrel: stable\nmanagedFields:\napiVersion: v1\nfieldsType: FieldsV1\nfieldsV1:\n.................<\/pre>\n\n\n\n<p>but if I do <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ubectl create -f nginx.pod.yml --save-config<\/pre>\n\n\n\n<p>and then see the pod I will see the difference<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@master&gt;kubectl get pod nginxapp -o yaml\napiVersion: v1\nkind: Pod\nmetadata:\n<strong>annotations<\/strong>:\n<em><code>kubectl.kubernetes.io\/last-applied-configuration: |\n{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"nginx\",\"rel\":\"stable\"},\"name\":\"nginxapp\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"image\":\"nginx:alpine\",\"name\":\"nginxapp\",\"ports\":[{\"containerPort\":80}]}]}}\ncreationTimestamp: \"2020-04-07T16:43:12Z\"<\/code><\/em>\nlabels:\napp: nginx\nrel: stable\nmanagedFields:\napiVersion: v1\nfieldsType: FieldsV1\nfieldsV1:\nf:metadata:\nf:annotations:\n.........................................<\/pre>\n\n\n\n<p>A similar output can be seen if you use apply instead of create.<\/p>\n\n\n\n<p>If you want to look at the running pod and its contents we can do a SH to connect to the pod and see a directory listing<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\"><strong>root@master<\/strong>&gt;<strong>kubectl exec nginxapp -it sh<\/strong>\nkubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.\n\/ # ls\nbin etc lib mnt proc run srv tmp var\ndev home media opt root sbin sys usr\n\/ #<\/pre>\n<\/div><\/div>\n\n\n\n<h4 class=\"has-text-align-center wp-block-heading\">&#8212; THE &#8211; END &#8212;<\/h4>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is always fun learning and trying out some new tech. In this blog, I will try and capture my experience of running my first pod using kubectl. If you wish to learn how to install a typical master, worker cluster of K8S I recommend going through this blog. A Kubernetes cluster can be deployed [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":562,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[18],"class_list":["post-504","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-k8s"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/504","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=504"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/562"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}