{"id":2409,"date":"2022-07-26T06:25:46","date_gmt":"2022-07-26T06:25:46","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2409"},"modified":"2023-08-06T14:29:49","modified_gmt":"2023-08-06T14:29:49","slug":"deploying-kubernetes-cluster","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/deploying-kubernetes-cluster","title":{"rendered":"How to Deploy Containers on a Kubernetes Cluster?"},"content":{"rendered":"<h3>What is a pod?<\/h3>\n<p>A pod is smallest unit of deployment in<a href=\"https:\/\/www.checkmateq.com\/kubernetes\"> Kubernetes<\/a>,\u00a0 the containers you deploy on Kubernetes are wrapped in pods ,each pod may have more than one container , so when you want to run an application in Kubernetes you will write a yaml file typically called as a <strong>manifest <\/strong><strong> file<\/strong>\u00a0and deploy your application on the Kubernetes cluster using <strong>kubectl<\/strong> commands which will deploy your containerized application wrapped in pod(called as a <strong>deployment<\/strong>) and you can create multiple replicas of this pod by just mentioning the count in the deployment file .<\/p>\n<p><span class=\"ILfuVd\" lang=\"en\"><span class=\"hgKElc\">A <strong>D<\/strong><strong>eployment<\/strong> is responsible for keeping a set of pods running, pods are ephemeral by nature, even an if a pod fails Kubernetes can automatically create a new replica of that pod to continue operations.<\/span><\/span><\/p>\n<div id=\"exacc_SczfYvm5Jq7E3LUPqKSmuAU2\" class=\"MBtdbb\" data-ved=\"2ahUKEwi5grqXtpb5AhUuIrcAHSiSCVcQ7NUEegQIBRAD\">\n<div class=\"ymu2Hb\">\n<div class=\"t0bRye r2fjmd\" data-hveid=\"CAUQBA\" data-ved=\"2ahUKEwi5grqXtpb5AhUuIrcAHSiSCVcQu04oAHoECAUQBA\">\n<div id=\"SczfYvm5Jq7E3LUPqKSmuAU__3\">\n<div class=\"wDYxhc\" data-md=\"61\">\n<div class=\"LGOjhe\" role=\"heading\" data-attrid=\"wa:\/description\" data-hveid=\"CAQQAA\"><span class=\"ILfuVd\" lang=\"en\"><span class=\"hgKElc\">\u00a0After creating a deployment to access it you have to create\u00a0a Service<b>, <\/b>service is responsible for enabling network access <\/span><\/span>to a set of pods, there are four types of Kubernetes services Cluster IP, NodePort, Load Balancer, External Name<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div role=\"heading\" data-attrid=\"wa:\/description\" data-hveid=\"CAQQAA\"><\/div>\n<div role=\"heading\" data-attrid=\"wa:\/description\" data-hveid=\"CAQQAA\">\n<p id=\"a6b8\" class=\"vj ln jc bn lo vk vl vm ls vn vo vp lw vq vr vs lz vt vu vv mc vw vx vy mf vz gb\"><b>Cluster IP<\/b> is most common service type used, Kubernetes will assign a cluster-internal IP address to Cluster IP service, this makes the service only available within the cluster, you cannot connect to service (pods) from outside the Kubernetes cluster.<\/p>\n<p class=\"vj ln jc bn lo vk vl vm ls vn vo vp lw vq vr vs lz vt vu vv mc vw vx vy mf vz gb\"><strong>Node Port<\/strong> is an open port on every node of your cluster, Kubernetes transparently routes incoming traffic on the NodePort to your service, this is an extension of Cluster IP service. A Cluster IP Service, to which the NodePort Service routes, is automatically created, you can contact the NodePort Service, from outside the cluster, by requesting &lt;NodeIP&gt;:&lt;NodePort&gt;.<\/p>\n<p id=\"0c89\" class=\"vj ln jc bn lo vk vl vm ls vn vo vp lw vq vr vs lz vt vu vv mc vw vx vy mf vz gb\"><strong>LoadBalancer<\/strong> service is an extension of NodePort service, NodePort and Cluster IP Services are automatically created when you request for a load balancer to which the external load balancer routes traffic to, it exposes the Service externally using a cloud provider\u2019s (AWS, GCP, Azure) load balancer.<\/p>\n<\/div>\n<p>In this blog we will create a deployment (deploying nginx webserver) and a service (Node port) for that deployment using yaml configuration files, below is the deployment.yaml file which deploys 2 replicas of\u00a0 ngins web server, and service.yaml file to allow traffic from outside to access the webserver at port 32000 on the node.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2531 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer-300x249.jpg\" alt=\"\" width=\"511\" height=\"424\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer-300x249.jpg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer-768x637.jpg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer.jpg 856w\" sizes=\"(max-width: 511px) 85vw, 511px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Lets understand these configuration files line by line<\/h3>\n<pre>apiVersion: v1\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0      apiVersion: v1\r\nkind: Deployment\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0kind: Service<\/pre>\n<p>the first two lines is declaring what you want to create, here we are creating deployment\u00a0 and a service , for each component there is a\u00a0 different api version .<\/p>\n<p>&nbsp;<\/p>\n<pre>metadata:                                       metadata:\r\n  name: nginx-deployment                          name: nginx-service\r\n<\/pre>\n<p>Every configuration file in Kubernetes has 2 parts, first part is meta data of components, here the meta data is name of the component itself<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2532 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company-300x194.jpeg\" alt=\"\" width=\"580\" height=\"375\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company-300x194.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company-768x497.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company.jpeg 859w\" sizes=\"(max-width: 580px) 85vw, 580px\" \/><\/p>\n<p><strong>Specification<\/strong><\/p>\n<p>the second part in configuration file is<strong> specification<\/strong> where you basically put every kind of configuration that you want to apply for that component of deployment, and you will give the attributes that are specific to the kind of a component that you are creating service\/deployment.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"wWOJcd\" role=\"button\" aria-labelledby=\"exacc_SczfYvm5Jq7E3LUPqKSmuAU1\">\n<div class=\"r21Kzd\" data-hveid=\"CAUQAQ\" data-ved=\"2ahUKEwi5grqXtpb5AhUuIrcAHSiSCVcQuk56BAgFEAE\">\n<p><img loading=\"lazy\" class=\"wp-image-2534 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Infrastructure-support-services-1-197x300.jpeg\" alt=\"\" width=\"347\" height=\"528\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Infrastructure-support-services-1-197x300.jpeg 197w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Infrastructure-support-services-1.jpeg 465w\" sizes=\"(max-width: 347px) 85vw, 347px\" \/><\/p>\n<p>as you can see in the specification part of deployment.yaml, it has a block named\u00a0<strong> template<\/strong> and this template also has its own meta data and specification, this part of configuration under template is applied to a pod , a pod should have its own configuration in a deployment configuration file , this will be the blueprint for a pod like which <strong>image<\/strong> it should be based on which <strong>port<\/strong> it should open what will be name of the container and all other details.<\/p>\n<p>First let&#8217;s create a deployment and a service and then understand the specification part of service configuration file.<\/p>\n<h3><strong>Deploying pods on Kubernetes Cluster<\/strong><\/h3>\n<p>As we have understand how to write deployment file and a service file and how they are connected using labels and selectors, let&#8217;s deploy nginx webserver on the Kubernetes cluster, use below given commands to get the status of your cluster and nodes.<\/p>\n<pre>kubectl cluster-info\r\nkubectl get nodes<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-2495 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-Dedicated-DevOps-Engineer-3-300x50.jpeg\" alt=\"\" width=\"798\" height=\"133\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-Dedicated-DevOps-Engineer-3-300x50.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-Dedicated-DevOps-Engineer-3-1024x172.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-Dedicated-DevOps-Engineer-3-768x129.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-Dedicated-DevOps-Engineer-3-1536x258.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-Dedicated-DevOps-Engineer-3-1200x201.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-Dedicated-DevOps-Engineer-3.jpeg 1753w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Copy below syntax on your master node terminal and click enter or create a yaml file (nginx.yaml\/deployment.yaml) copy the highlighted part of below syntax in the yaml file and save it , use &#8220;<strong>kubectl apply -f\u00a0 \u00a0xxxx.yaml<\/strong>&#8221;\u00a0 \u00a0xxxx.yaml should be replaced by your yaml file name .<\/p>\n<pre>cat &lt;&lt;EOF | kubectl apply -f -\r\n<strong>apiVersion: apps\/v1\r\nkind: Deployment\r\nmetadata:\r\n  name: nginx-deployment\r\nspec:\r\n  replicas: 2 \r\n  template:\r\n    metadata:\r\n      labels:\r\n        app: nginx\r\n    spec:\r\n      containers:\r\n      - name: nginx\r\n        image: nginx:latest\r\n        ports:\r\n        - containerPort: 80<\/strong>      \r\nEOF\r\n<\/pre>\n<p>And use command &#8220;<strong>kubectl get pods<\/strong>&#8221; to check the status of the deployment\/pods.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2497 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Cloud-DevOps-Services-300x36.jpeg\" alt=\"\" width=\"892\" height=\"107\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Cloud-DevOps-Services-300x36.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Cloud-DevOps-Services-1024x123.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Cloud-DevOps-Services-768x92.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Cloud-DevOps-Services-1536x184.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Cloud-DevOps-Services-1200x144.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Cloud-DevOps-Services.jpeg 1703w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<h3>Creating a Service<\/h3>\n<p>Now let&#8217;s create a service of type NodePort, create file named<strong> service.yaml<\/strong> (you can use any name for your file need not be &#8220;service&#8221;) copy below given syntax and save it.<\/p>\n<pre>apiVersion: v1\r\nkind: Service\r\nmetadata:\r\n  name: nginx-service\r\nspec:\r\n  selector: \r\n    app: nginx\r\n  type: NodePort  \r\n  ports:\r\n    - port: 80\r\n      targetPort: 80\r\n      nodePort: 32000\r\n<\/pre>\n<p>Now use below given command to apply the configuration and\u00a0 create a NodePort\/expose deployed application for external traffic,\u00a0 for the pods you deployed in previous step.<\/p>\n<pre>kubectl apply -f service.yaml<\/pre>\n<p>replace service.yaml with your file name<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2505 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Engineer-300x24.jpeg\" alt=\"\" width=\"801\" height=\"64\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Engineer-300x24.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Engineer-1024x82.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Engineer-768x61.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Engineer-1536x122.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Engineer-1200x96.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Engineer.jpeg 1583w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now go on to the browser and give &lt;IP-address&gt;:32000 (IP address of the worker node on which pods are deployed)<\/p>\n<h3><img loading=\"lazy\" class=\" wp-image-2526 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-services-300x144.jpeg\" alt=\"\" width=\"571\" height=\"274\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-services-300x144.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-services-1024x491.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-services-768x368.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-services-1536x737.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-services-1200x576.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-services.jpeg 1920w\" sizes=\"(max-width: 571px) 85vw, 571px\" \/><\/h3>\n<h3>How service routes traffic to deployment?<\/h3>\n<p>In previous step we have created a deployment and a service, and we are able to access our deployment from the browser, but how these both are connected?<\/p>\n<p>it&#8217;s simply done using labels and selectors.<\/p>\n<p><b>Labels\u00a0are key<\/b>\/value pairs attached to objects you can label each and every object in Kubernetes<\/p>\n<p><b>Selectors are<\/b> nothing but selecting objects using the labels i.e., selecting objects with same label to make a group, you cannot have a selector without a label, so if you want to group a particular set of objects all those set of objects should have the same labels, for example if you want to select a set of pods as a backend to an application all of them should have same label.<\/p>\n<\/div>\n<p><img loading=\"lazy\" class=\" wp-image-2535 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-300x233.jpeg\" alt=\"\" width=\"543\" height=\"422\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-300x233.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-768x596.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services.jpeg 907w\" sizes=\"(max-width: 543px) 85vw, 543px\" \/><\/p>\n<p>In deployment.yaml we are labeling our pods with a key\/value pair <strong>app:nginx\u00a0<\/strong>and in the service.yaml\u00a0 a selector\u00a0 is being used and the same label is given to it as in the deployment.yaml file ( <strong>app:nginx<\/strong>) , so whenever someone access this service it will check the labels and routes the traffic to the pods with the same label.<\/p>\n<\/div>\n<h3>How to declare ports in service configuration file?<\/h3>\n<div class=\"wWOJcd\" role=\"button\" aria-labelledby=\"exacc_SczfYvm5Jq7E3LUPqKSmuAU1\">\n<p><img loading=\"lazy\" class=\" wp-image-2536 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/DevOps-support-1-300x156.jpeg\" alt=\"\" width=\"592\" height=\"308\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/DevOps-support-1-300x156.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/DevOps-support-1-768x400.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/DevOps-support-1.jpeg 830w\" sizes=\"(max-width: 592px) 85vw, 592px\" \/><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2540 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-company-300x232.jpeg\" alt=\"\" width=\"537\" height=\"415\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-company-300x232.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-company-768x595.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/it-support-company.jpeg 919w\" sizes=\"(max-width: 537px) 85vw, 537px\" \/><\/p>\n<p>As show in the above image we are exposing our application(nginx) at\u00a0 port :80\u00a0 by declaring\u00a0 \u00a0<strong>&#8211; containerPort<\/strong> as <strong>80<\/strong> under <strong>ports:\u00a0<\/strong>in deployment.yaml file, and in service.yaml file\u00a0 \u00a0<strong>targetPort: 80<\/strong> is the container port number on which we have exposed our application on, and <strong>&#8211; port: 80 <\/strong> is the external port , as mention initially in this blog we created a service of type <strong>NodePort<\/strong> and using port:32000 for this , even if you do not mention any port number ,Kubernetes will choose any random port from node-port IP range( 30,000 t0 32,767)<\/p>\n<p>&nbsp;<\/p>\n<p>Please contact our software engineering team for any Cloud <a href=\"https:\/\/www.checkmateq.com\/devops-engineering\">DevOps<\/a> infrastructure management services <a href=\"https:\/\/www.checkmateq.com\/contact-us\">website<\/a>, <a href=\"https:\/\/www.linkedin.com\/company\/checkmatglobal\/\">LinkedIn<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>What is a pod? A pod is smallest unit of deployment in Kubernetes,\u00a0 the containers you deploy on Kubernetes are wrapped in pods ,each pod may have more than one container , so when you want to run an application in Kubernetes you will write a yaml file typically called as a manifest file\u00a0and deploy &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/deploying-kubernetes-cluster\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Deploy Containers on a Kubernetes Cluster?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":2604,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,79,2,71,70,69,68,59,11,78,63,23,16,24,80,8],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2409"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2409"}],"version-history":[{"count":46,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2409\/revisions"}],"predecessor-version":[{"id":4267,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2409\/revisions\/4267"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2604"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}